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, 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_record, load_day_record, load_days_records, load_last_day, next_id, read_from_file, 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 |
# 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 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
145 146 147 148 149 150 151 152 |
# File 'lib/tempo/models/time_record.rb', line 145 def duration if @end_time.kind_of? Time end_time = @end_time else end_time = Time.now() end end_time.to_i - @start_time.to_i end |
#freeze_dry ⇒ Object
163 164 165 166 167 |
# File 'lib/tempo/models/time_record.rb', line 163 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
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/tempo/models/time_record.rb', line 127 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
141 142 143 |
# File 'lib/tempo/models/time_record.rb', line 141 def project_title Project.find_by_id( @project ).title if @project end |
#running! ⇒ Object
158 159 160 161 |
# File 'lib/tempo/models/time_record.rb', line 158 def running! raise "only the most recent record can be reopened" unless self == self.class.last_record @end_time = :running end |
#running? ⇒ Boolean
154 155 156 |
# File 'lib/tempo/models/time_record.rb', line 154 def running? @end_time == :running end |
#tag(tags) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/tempo/models/time_record.rb', line 169 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
184 185 186 |
# File 'lib/tempo/models/time_record.rb', line 184 def to_s "#{@start_time} - #{@end_time}, #{project_title}: #{@description}" end |
#untag(tags) ⇒ Object
177 178 179 180 181 182 |
# File 'lib/tempo/models/time_record.rb', line 177 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
78 79 80 81 82 83 84 85 |
# File 'lib/tempo/models/time_record.rb', line 78 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
107 108 109 110 111 112 113 114 115 |
# File 'lib/tempo/models/time_record.rb', line 107 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
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/tempo/models/time_record.rb', line 90 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 |