Class: Tempo::Model::Log
Direct Known Subclasses
Instance Attribute Summary collapse
-
#d_id ⇒ Object
readonly
Returns the value of attribute d_id.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
Attributes inherited from Base
Class Method Summary collapse
- .add_id(time, id) ⇒ Object
- .add_to_days_index(member) ⇒ Object
- .date_symbol(time) ⇒ Object
-
.day_id(time) ⇒ Object
day_ids can be run through without change Time will be converted into “YYYYmmdd” ex: 1-1-2014 => “20140101”.
- .day_id_to_time(d_id) ⇒ Object
-
.days_index ⇒ Object
all instances are saved in the index inherited from base.
- .delete(instance) ⇒ Object
-
.delete_day_record(time, options = {}) ⇒ Object
delete the file for a single day this is necessary for removing a single entry on a day since updates will skip over days with no entries.
-
.dir ⇒ Object
Returns the immediate directory for the log Tempo::Model::MessageLog => tempo_message_logs.
-
.file(time) ⇒ Object
Passthrough function, returns the log filename for a given date.
-
.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.
-
.id_counter(time) ⇒ Object
Maintain arrays of unique ids for each day.
-
.ids(time) ⇒ Object
Returns an array of ids for the given day.
- .increase_id_counter(time) ⇒ Object
-
.last_record(options = {}) ⇒ Object
returns the loaded record with the latest start time Only loads records if options is true.
-
.load_day_record(time, options = {}) ⇒ Object
load all the records for a single day.
-
.load_days_records(time_1, time_2, options = {}) ⇒ Object
load the records for each day from time 1 to time 2.
-
.load_last_day(options = {}) ⇒ Object
load the records for the most recently recorded day.
- .next_id(time) ⇒ Object
-
.read_from_file(time, options = {}) ⇒ Object
send alternate directory through options.
-
.records(options = {}) ⇒ Object
Load all records from a directory into an array send alternate directory through options.
-
.save_to_file(options = {}) ⇒ Object
send alternate directory through options.
Instance Method Summary collapse
- #freeze_dry ⇒ Object
-
#initialize(options = {}) ⇒ Log
constructor
A new instance of Log.
Methods inherited from Base
#delete, find, index, instances_have_attributes?, method_missing, respond_to?, run_find_by_method, run_sort_by_method
Constructor Details
#initialize(options = {}) ⇒ Log
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/tempo/models/log.rb', line 162 def initialize(={}) @start_time = .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 = [: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 (readonly)
Returns the value of attribute d_id.
11 12 13 |
# File 'lib/tempo/models/log.rb', line 11 def d_id @d_id end |
#start_time ⇒ Object
Returns the value of attribute start_time.
10 11 12 |
# File 'lib/tempo/models/log.rb', line 10 def start_time @start_time end |
Class Method Details
.add_id(time, id) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/tempo/models/log.rb', line 201 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
193 194 195 196 197 198 199 |
# File 'lib/tempo/models/log.rb', line 193 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
209 210 211 |
# File 'lib/tempo/models/log.rb', line 209 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”
141 142 143 144 145 146 |
# File 'lib/tempo/models/log.rb', line 141 def day_id(time) return time if time.to_s =~ /^\d{8}$/ raise ArgumentError, "Invalid Time" if not time.kind_of? Time time.strftime("%Y%m%d") end |
.day_id_to_time(d_id) ⇒ Object
148 149 150 |
# File 'lib/tempo/models/log.rb', line 148 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.
38 39 40 41 |
# File 'lib/tempo/models/log.rb', line 38 def days_index @days_index = {} unless @days_index.kind_of? Hash @days_index end |
.delete(instance) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/tempo/models/log.rb', line 152 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 |
.delete_day_record(time, options = {}) ⇒ Object
delete the file for a single day this is necessary for removing a single entry on a day since updates will skip over days with no entries
119 120 121 122 123 |
# File 'lib/tempo/models/log.rb', line 119 def delete_day_record(time, ={}) [:time] = time [:destroy] = true FileRecord::FileUtility.new(self, ).file_path end |
.dir ⇒ Object
Returns the immediate directory for the log Tempo::Model::MessageLog => tempo_message_logs
51 52 53 |
# File 'lib/tempo/models/log.rb', line 51 def dir FileRecord::FileUtility.new(self).log_directory end |
.file(time) ⇒ Object
Passthrough function, returns the log filename for a given date
45 46 47 |
# File 'lib/tempo/models/log.rb', line 45 def file(time) FileRecord::FileUtility.new(self, {time: time}).filename 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
128 129 130 131 132 133 134 135 136 |
# File 'lib/tempo/models/log.rb', line 128 def find_by_id(id, time) time = day_id time ids = find "id", id d_ids = find "d_id", time #return the first and only match in the union #of the arrays (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
21 22 23 24 25 |
# File 'lib/tempo/models/log.rb', line 21 def id_counter(time) dsym = date_symbol time @id_counter = {} unless @id_counter.kind_of? Hash @id_counter[ dsym ] ||= 1 end |
.ids(time) ⇒ Object
Returns an array of ids for the given day
28 29 30 31 32 |
# File 'lib/tempo/models/log.rb', line 28 def ids(time) dsym = date_symbol time @ids = {} unless @ids.kind_of? Hash @ids[dsym] ||= [] end |
.increase_id_counter(time) ⇒ Object
213 214 215 216 217 218 |
# File 'lib/tempo/models/log.rb', line 213 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 |
.last_record(options = {}) ⇒ Object
returns the loaded record with the latest start time Only loads records if options is true
63 64 65 66 |
# File 'lib/tempo/models/log.rb', line 63 def last_record(={}) load_last_day() if [:load] sort_by_start_time.last end |
.load_day_record(time, options = {}) ⇒ Object
load all the records for a single day
82 83 84 85 86 87 88 |
# File 'lib/tempo/models/log.rb', line 82 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, options = {}) ⇒ Object
load the records for each day from time 1 to time 2
92 93 94 95 96 97 98 99 100 |
# File 'lib/tempo/models/log.rb', line 92 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(options = {}) ⇒ Object
load the records for the most recently recorded day
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/tempo/models/log.rb', line 104 def load_last_day(={}) reg = /(\d+)\.yaml/ recs = records() if recs.last d_id = reg.match(recs.last)[1] time = day_id_to_time d_id if d_id load_day_record time, return time end end |
.next_id(time) ⇒ Object
220 221 222 223 224 225 |
# File 'lib/tempo/models/log.rb', line 220 def next_id(time) while ids(time).include? id_counter time increase_id_counter time end id_counter time end |
.read_from_file(time, options = {}) ⇒ Object
send alternate directory through options
74 75 76 77 78 |
# File 'lib/tempo/models/log.rb', line 74 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(options = {}) ⇒ Object
Load all records from a directory into an array send alternate directory through options
57 58 59 |
# File 'lib/tempo/models/log.rb', line 57 def records(={}) FileRecord::FileUtility.new(self, ).log_records end |
.save_to_file(options = {}) ⇒ Object
send alternate directory through options
69 70 71 |
# File 'lib/tempo/models/log.rb', line 69 def save_to_file(={}) FileRecord::Record.save_log(self, ) end |
Instance Method Details
#freeze_dry ⇒ Object
183 184 185 186 187 |
# File 'lib/tempo/models/log.rb', line 183 def freeze_dry record = super record.delete(:d_id) record end |