Class: Tempo::Model::Log
- Inherits:
-
Base
- Object
- Base
- Tempo::Model::Log
show all
- Defined in:
- lib/tempo/models/log.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#id
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
add_to_index, #delete, find, index, method_missing, run_find_by_method, run_sort_by_method
Constructor Details
#initialize(options = {}) ⇒ Log
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/tempo/models/log.rb', line 132
def initialize( options={} )
@start_time = options.fetch(:start_time, Time.now )
@start_time = Time.new(@start_time) if @start_time.kind_of? String
self.class.load_day_record(@start_time)
@d_id = self.class.day_id @start_time
id_candidate = options[:id]
if !id_candidate
@id = self.class.next_id @start_time
elsif self.class.ids( @start_time ).include? id_candidate
raise IdentityConflictError, "Id #{id_candidate} already exists"
else
@id = id_candidate
end
self.class.add_id @start_time, @id
self.class.add_to_index self
self.class.add_to_days_index self
end
|
Instance Attribute Details
#d_id ⇒ Object
Returns the value of attribute d_id.
5
6
7
|
# File 'lib/tempo/models/log.rb', line 5
def d_id
@d_id
end
|
#start_time ⇒ Object
Returns the value of attribute start_time.
4
5
6
|
# File 'lib/tempo/models/log.rb', line 4
def start_time
@start_time
end
|
Class Method Details
.add_id(time, id) ⇒ Object
171
172
173
174
175
176
177
|
# File 'lib/tempo/models/log.rb', line 171
def add_id time, id
dsym = date_symbol time
@ids = {} unless @ids.kind_of? Hash
@ids[dsym] ||= []
@ids[dsym] << id
@ids[dsym].sort!
end
|
.add_to_days_index(member) ⇒ Object
163
164
165
166
167
168
169
|
# File 'lib/tempo/models/log.rb', line 163
def add_to_days_index member
@days_index = {} unless @days_index.kind_of? Hash
dsym = date_symbol member.start_time
@days_index[dsym] ||= []
@days_index[dsym] << member
@days_index[dsym].sort! { |a,b| a.start_time <=> b.start_time }
end
|
.date_symbol(time) ⇒ Object
179
180
181
|
# File 'lib/tempo/models/log.rb', line 179
def date_symbol time
day_id( time ).to_sym
end
|
.day_id(time) ⇒ Object
day_ids can be run through without change Time will be converted into “YYYYmmdd” ex: 1-1-2014 => “20140101”
110
111
112
113
114
115
116
|
# File 'lib/tempo/models/log.rb', line 110
def day_id time
if time.kind_of? String
return time if time =~ /^\d{8}$/
end
raise ArgumentError, "Invalid Time" if not time.kind_of? Time
time.strftime("%Y%m%d")
end
|
.day_id_to_time(d_id) ⇒ Object
118
119
120
|
# File 'lib/tempo/models/log.rb', line 118
def day_id_to_time d_id
time = Time.new(d_id[0..3].to_i, d_id[4..5].to_i, d_id[6..7].to_i)
end
|
.days_index ⇒ Object
all instances are saved in the index inherited from base. Additionally, the days index organizes all instances into arrays by day. This is used for saving to file.
31
32
33
34
|
# File 'lib/tempo/models/log.rb', line 31
def days_index
@days_index = {} unless @days_index.kind_of? Hash
@days_index
end
|
.delete(instance) ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/tempo/models/log.rb', line 122
def delete instance
id = instance.id
dsym = date_symbol instance.d_id
index.delete instance
days_index[dsym].delete instance
@ids[dsym].delete id
end
|
.dir ⇒ Object
40
41
42
|
# File 'lib/tempo/models/log.rb', line 40
def dir
FileRecord::Record.log_dirname( self )
end
|
.file(time) ⇒ Object
36
37
38
|
# File 'lib/tempo/models/log.rb', line 36
def file time
FileRecord::Record.log_filename( self, time )
end
|
.find_by_id(id, time) ⇒ Object
takes and integer, and time or day_id and returns the instance that matches both the id and d_id
97
98
99
100
101
102
103
104
105
|
# File 'lib/tempo/models/log.rb', line 97
def find_by_id id, time
time = day_id time
ids = find "id", id
d_ids = find "d_id", time
(ids & d_ids)[0]
end
|
.id_counter(time) ⇒ Object
Maintain arrays of unique ids for each day. days are represented as symbols in the hash, for example Jan 1, 2013 would be :“130101” id counter is managed through the private methods increase_id_counter and next_id below
15
16
17
18
19
|
# File 'lib/tempo/models/log.rb', line 15
def id_counter time
dsym = date_symbol time
@id_counter = {} unless @id_counter.kind_of? Hash
@id_counter[ dsym ] ||= 1
end
|
.ids(time) ⇒ Object
21
22
23
24
25
|
# File 'lib/tempo/models/log.rb', line 21
def ids time
dsym = date_symbol time
@ids = {} unless @ids.kind_of? Hash
@ids[dsym] ||= []
end
|
.increase_id_counter(time) ⇒ Object
183
184
185
186
187
188
|
# File 'lib/tempo/models/log.rb', line 183
def increase_id_counter time
dsym = date_symbol time
@id_counter = {} unless @id_counter.kind_of? Hash
@id_counter[ dsym ] ||= 0
@id_counter[ dsym ] = @id_counter[ dsym ].next
end
|
.load_day_record(time) ⇒ Object
load all the records for a single day
62
63
64
65
66
67
68
|
# File 'lib/tempo/models/log.rb', line 62
def load_day_record time
dsym = date_symbol time
if not days_index.has_key? dsym
@days_index[ dsym ] = []
read_from_file time
end
end
|
.load_days_records(time_1, time_2) ⇒ Object
load the records for each day from time 1 to time 2
72
73
74
75
76
77
78
79
80
|
# File 'lib/tempo/models/log.rb', line 72
def load_days_records time_1, time_2
return if time_1.nil? || time_2.nil?
days = ( time_2.to_date - time_1.to_date ).to_i
return if days < 0
(days + 1).times { |i| load_day_record( time_1.add_days( i ))}
end
|
.load_last_day ⇒ Object
load the records for the most recently recorded day
84
85
86
87
88
89
90
91
92
|
# File 'lib/tempo/models/log.rb', line 84
def load_last_day
reg = /(\d+)\.yaml/
if records.last
d_id = reg.match(records.last)[1] if records.last
time = day_id_to_time d_id if d_id
load_day_record time
return time
end
end
|
.next_id(time) ⇒ Object
190
191
192
193
194
195
|
# File 'lib/tempo/models/log.rb', line 190
def next_id time
while ids(time).include? id_counter time
increase_id_counter time
end
id_counter time
end
|
.read_from_file(time) ⇒ Object
54
55
56
57
58
|
# File 'lib/tempo/models/log.rb', line 54
def read_from_file time
dsym = date_symbol time
@days_index[ dsym ] = [] if not days_index.has_key? dsym
FileRecord::Record.read_log( self, time )
end
|
.records ⇒ Object
44
45
46
47
|
# File 'lib/tempo/models/log.rb', line 44
def records
path = FileRecord::Record.log_dir( self )
Dir[path + "/*.yaml"]
end
|
.save_to_file ⇒ Object
49
50
51
|
# File 'lib/tempo/models/log.rb', line 49
def save_to_file
FileRecord::Record.save_log( self )
end
|
Instance Method Details
#freeze_dry ⇒ Object
153
154
155
156
157
|
# File 'lib/tempo/models/log.rb', line 153
def freeze_dry
record = super
record.delete(:d_id)
record
end
|