Class: Plog::ObjectFile

Inherits:
File
  • Object
show all
Defined in:
lib/object_file.rb

Constant Summary collapse

DEFAULT_VALUES =
{
  :@total_hits => 0,
  :@total_time => 0,
  :@view_time => 0,
  :@db_time => 0,
  :@simplified_url => 'default'
}
KEY_ORDER =
[
  :@total_hits,
  :@total_time,
  :@view_time,
  :@db_time,
  :@simplified_url
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ObjectFile

Returns a new instance of ObjectFile.



24
25
26
27
28
# File 'lib/object_file.rb', line 24

def initialize(*args)
  raw_data(args.first)
  super(*args)
  setup!
end

Instance Attribute Details

#db_timeObject (readonly)

Returns the value of attribute db_time.



22
23
24
# File 'lib/object_file.rb', line 22

def db_time
  @db_time
end

#simplified_urlObject

Returns the value of attribute simplified_url.



21
22
23
# File 'lib/object_file.rb', line 21

def simplified_url
  @simplified_url
end

#total_hitsObject (readonly)

Returns the value of attribute total_hits.



22
23
24
# File 'lib/object_file.rb', line 22

def total_hits
  @total_hits
end

#total_timeObject (readonly)

Returns the value of attribute total_time.



22
23
24
# File 'lib/object_file.rb', line 22

def total_time
  @total_time
end

#view_timeObject (readonly)

Returns the value of attribute view_time.



22
23
24
# File 'lib/object_file.rb', line 22

def view_time
  @view_time
end

Class Method Details

.export_header_settingsObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/object_file.rb', line 99

def self.export_header_settings
  [
    ['Hits', [:left, 10]],
    ['Time', [:left, 10]],
    ['Avg-Time', [:left, 10]],
    ['DbTime', [:left, 10]],
    ['Avg-DB', [:left, 10]],
    ['View', [:left, 10]],
    ['Avg-View', [:left, 10]],
    ['Url', [:left, 40]],
  ]
end

.formated_headersObject



112
113
114
115
116
117
118
119
120
# File 'lib/object_file.rb', line 112

def self.formated_headers
  export_header_settings.collect do |value, setting|
    if setting.first == :left
      value.to_s.ljust(setting[1])
    else
      value.to_s.rjust(setting[1])
    end
  end.join('') + "\n"
end

Instance Method Details

#append_db_time(value) ⇒ Object



45
46
47
# File 'lib/object_file.rb', line 45

def append_db_time(value)
  @db_time += value.to_i
end

#append_hits(value) ⇒ Object



53
54
55
# File 'lib/object_file.rb', line 53

def append_hits(value)
  @total_hits += value
end

#append_total_time(value) ⇒ Object



49
50
51
# File 'lib/object_file.rb', line 49

def append_total_time(value)
  @total_time += value
end

#append_view_time(value) ⇒ Object



41
42
43
# File 'lib/object_file.rb', line 41

def append_view_time(value)
  @view_time += value.to_i
end

#avg_db_timeObject



146
147
148
149
# File 'lib/object_file.rb', line 146

def avg_db_time
  return 0 if @db_time == 0
  (@db_time / @total_hits / 1000.0)
end

#avg_total_timeObject



136
137
138
139
# File 'lib/object_file.rb', line 136

def avg_total_time
  return 0 if @total_time == 0
  (@total_time / @total_hits / 1000.0)
end

#avg_view_timeObject



141
142
143
144
# File 'lib/object_file.rb', line 141

def avg_view_time
  return 0 if @view_time == 0
  (@view_time / @total_hits / 1000.0)
end

#exportObject



126
127
128
129
130
131
132
133
134
# File 'lib/object_file.rb', line 126

def export
  export_settings.collect do |value, setting|
    if setting.first == :left
      value.to_s.ljust(setting[1])
    else
      value.to_s.rjust(setting[1])
    end
  end.join('')
end

#export_headersObject



122
123
124
# File 'lib/object_file.rb', line 122

def export_headers
  export_header_settings.collect { |header| header.ljust(20) }.join('')
end

#export_settingsObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/object_file.rb', line 86

def export_settings
  [
    [@total_hits, [:left, 10]],
    [@total_time, [:left, 10]],
    [avg_total_time, [:left, 10]],
    [@db_time, [:left, 10]],
    [avg_db_time, [:left, 10]],
    [@view_time, [:left, 10]],
    [avg_view_time, [:left, 10]],
    [simplified_url, [:left, 40]],
  ]
end

#intify!Object



71
72
73
74
75
76
# File 'lib/object_file.rb', line 71

def intify!
  @total_hits = @total_hits.to_i
  @total_time = @total_time.to_i
  @db_time = @db_time.to_i
  @view_time = @view_time.to_i
end

#load_changes!Object



65
66
67
68
69
# File 'lib/object_file.rb', line 65

def load_changes!
  return if @raw_data == ''
  @total_hits, @total_time, @view_time, @db_time, @simplified_url = @raw_data.split(',')
  intify!
end

#raw_data(some_string) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/object_file.rb', line 78

def raw_data(some_string)
  if File.exist? some_string
    @raw_data = File.new(some_string).read
  else
    @raw_data = ''
  end
end

#save_changes!Object



57
58
59
# File 'lib/object_file.rb', line 57

def save_changes!
  puts serialize_changes
end

#serialize_changesObject



61
62
63
# File 'lib/object_file.rb', line 61

def serialize_changes
  KEY_ORDER.collect { |key| instance_variable_get key }.join(',')
end

#setup!Object



30
31
32
33
# File 'lib/object_file.rb', line 30

def setup!
  unpack_default_values if ObjectFile.zero? path
  load_changes!
end

#unpack_default_valuesObject



35
36
37
38
39
# File 'lib/object_file.rb', line 35

def unpack_default_values
  DEFAULT_VALUES.each do |k, v|
    instance_variable_set k, v
  end
end