Class: Tempo::Model::TimeRecord
- Defined in:
- lib/tempo/models/time_record.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#project ⇒ Object
Returns the value of attribute project.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Attributes inherited from Log
Attributes inherited from Base
Class Method Summary collapse
-
.current ⇒ Object
Only one record can be running at any given time.
- .current=(instance) ⇒ Object
Instance Method Summary collapse
- #duration ⇒ Object
- #freeze_dry ⇒ Object
-
#initialize(options = {}) ⇒ TimeRecord
constructor
A new instance of TimeRecord.
-
#next_record ⇒ Object
Returns the next record in time from the current record Remember, only records loaded from files will be available to compare against, so it is important to use the following methods defined in Log first to assure accuracy: * load_day_record * load_days_records * load_last_day.
- #project_title ⇒ Object
- #running! ⇒ Object
- #running? ⇒ Boolean
- #tag(tags) ⇒ Object
- #to_s ⇒ Object
- #untag(tags) ⇒ Object
-
#update_times(start_time, end_time) ⇒ Object
method for updating both times at once, necessary if it would cause a conflict to do them individually.
-
#valid_end_time?(time) ⇒ Boolean
Public method to access verify end time, determine if an error will be raised.
-
#valid_start_time?(time) ⇒ Boolean
Public method to access verify start time, determine if an error will be raised.
Methods inherited from Log
add_id, add_to_days_index, clear_all, d_id_from_file, date_symbol, day_id, day_id_to_time, days_index, delete, delete_day_record, dir, file, find_by_id, id_counter, ids, increase_id_counter, last_day, last_record, load_day_record, load_days_records, load_last_day, next_id, read_from_file, record_d_ids, records, save_to_file
Methods inherited from Base
#delete, delete, file, find, find_by_id, id_counter, ids, index, instances_have_attributes?, method_missing, read_from_file, respond_to?, run_find_by_method, run_sort_by_method, save_to_file
Constructor Details
#initialize(options = {}) ⇒ TimeRecord
Returns a new instance of TimeRecord.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tempo/models/time_record.rb', line 33 def initialize(={}) # declare these first for model organization when sent to YAML @project_title = nil @description = .fetch :description, "" @start_time = nil # verify both start time and end time before sending to super # super handles start_time, not end time [:start_time] ||= Time.now @end_time = .fetch :end_time, :running if ! [:exact_time] [:start_time] = [:start_time].round unless [:start_time] == :running @end_time = @end_time.round unless @end_time == :running end verify_times [:start_time], @end_time super project = .fetch :project, Tempo::Model::Project.current @project = project.kind_of?(Integer) ? project : project.id @tags = [] tag .fetch(:tags, []) leave_only_one_running end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
11 12 13 |
# File 'lib/tempo/models/time_record.rb', line 11 def description @description end |
#end_time ⇒ Object
Returns the value of attribute end_time.
12 13 14 |
# File 'lib/tempo/models/time_record.rb', line 12 def end_time @end_time end |
#project ⇒ Object
Returns the value of attribute project.
11 12 13 |
# File 'lib/tempo/models/time_record.rb', line 11 def project @project end |
#start_time ⇒ Object
Returns the value of attribute start_time.
12 13 14 |
# File 'lib/tempo/models/time_record.rb', line 12 def start_time @start_time end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
12 13 14 |
# File 'lib/tempo/models/time_record.rb', line 12 def @tags end |
Class Method Details
.current ⇒ Object
Only one record can be running at any given time. This record is the class current, and has and end time of :running
19 20 21 22 |
# File 'lib/tempo/models/time_record.rb', line 19 def current return @current if @current && @current.end_time == :running @current = nil end |
.current=(instance) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/tempo/models/time_record.rb', line 24 def current=instance if instance.class == self @current = instance else raise ArgumentError end end |
Instance Method Details
#duration ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/tempo/models/time_record.rb', line 151 def duration if @end_time.kind_of? Time end_time = @end_time else end_time = Time.now().round end end_time.to_i - @start_time.to_i end |
#freeze_dry ⇒ Object
169 170 171 172 173 |
# File 'lib/tempo/models/time_record.rb', line 169 def freeze_dry record = super record[:project_title] = project_title record end |
#next_record ⇒ Object
Returns the next record in time from the current record Remember, only records loaded from files will be available to compare against, so it is important to use the following methods defined in Log first to assure accuracy:
* load_day_record
* load_days_records
* load_last_day
uses start_time if end time is :running
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/tempo/models/time_record.rb', line 133 def next_record next_one = nil end_time = ( @end_time.kind_of? Time ) ? @end_time : @start_time self.class.index.each do |record| next if record == self if next_one == nil && record.start_time >= end_time next_one = record elsif record.start_time >= end_time && record.start_time < next_one.start_time next_one = record end end next_one end |
#project_title ⇒ Object
147 148 149 |
# File 'lib/tempo/models/time_record.rb', line 147 def project_title Project.find_by_id( @project ).title if @project end |
#running! ⇒ Object
164 165 166 167 |
# File 'lib/tempo/models/time_record.rb', line 164 def running! raise "only the most recent record can be reopened" unless self == self.class.last_record @end_time = :running end |
#running? ⇒ Boolean
160 161 162 |
# File 'lib/tempo/models/time_record.rb', line 160 def running? @end_time == :running end |
#tag(tags) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/tempo/models/time_record.rb', line 175 def tag() return unless and .kind_of? Array .each do |tag| tag.split.each {|t| @tags << t if ! @tags.include? t } end @tags.sort! end |
#to_s ⇒ Object
190 191 192 |
# File 'lib/tempo/models/time_record.rb', line 190 def to_s "#{@start_time} - #{@end_time}, #{project_title}: #{@description}" end |
#untag(tags) ⇒ Object
183 184 185 186 187 188 |
# File 'lib/tempo/models/time_record.rb', line 183 def untag() return unless and .kind_of? Array .each do |tag| tag.split.each {|t| @tags.delete t } end end |
#update_times(start_time, end_time) ⇒ Object
method for updating both times at once, necessary if it would cause a conflict to do them individually
84 85 86 87 88 89 90 91 |
# File 'lib/tempo/models/time_record.rb', line 84 def update_times(start_time, end_time) raise ArgumentError if !start_time.kind_of? Time raise ArgumentError if !end_time.kind_of? Time verify_times start_time, end_time @start_time = start_time @end_time = end_time leave_only_one_running end |
#valid_end_time?(time) ⇒ Boolean
Public method to access verify end time, determine if an error will be raised
113 114 115 116 117 118 119 120 121 |
# File 'lib/tempo/models/time_record.rb', line 113 def valid_end_time?(time) return false if !time.kind_of? Time begin verify_times self.start_time, time rescue ArgumentError => e return false end true end |
#valid_start_time?(time) ⇒ Boolean
Public method to access verify start time, determine if an error will be raised
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tempo/models/time_record.rb', line 96 def valid_start_time?(time) return false if !time.kind_of? Time begin if @end_time != :running verify_times time, @end_time else verify_start_time time end rescue ArgumentError => e return false end true end |