Class: Tempo::Model::TimeRecord

Inherits:
Log
  • Object
show all
Defined in:
lib/tempo/models/time_record.rb

Instance Attribute Summary collapse

Attributes inherited from Log

#d_id

Attributes inherited from Base

#id

Class Method Summary collapse

Instance Method Summary collapse

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(options={})

  # declare these first for model organization when sent to YAML
  @project_title = nil
  @description = options.fetch :description, ""
  @start_time = nil

  # verify both start time and end time before sending to super
  # super handles start_time, not end time
  options[:start_time] ||= Time.now
  @end_time = options.fetch :end_time, :running

  if ! options[:exact_time]
    options[:start_time] = options[:start_time].round unless options[:start_time] == :running
    @end_time = @end_time.round unless @end_time == :running
  end

  verify_times options[:start_time], @end_time

  super options

  project = options.fetch :project, Tempo::Model::Project.current
  @project = project.kind_of?(Integer) ? project : project.id

  @tags = []
  tag options.fetch(:tags, [])

  leave_only_one_running
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/tempo/models/time_record.rb', line 11

def description
  @description
end

#end_timeObject

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

#projectObject

Returns the value of attribute project.



11
12
13
# File 'lib/tempo/models/time_record.rb', line 11

def project
  @project
end

#start_timeObject

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

#tagsObject (readonly)

Returns the value of attribute tags.



12
13
14
# File 'lib/tempo/models/time_record.rb', line 12

def tags
  @tags
end

Class Method Details

.currentObject

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

#durationObject



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_dryObject



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_recordObject

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_titleObject



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

Returns:

  • (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(tags)
  return unless tags and tags.kind_of? Array
  tags.each do |tag|
    tag.split.each {|t| @tags << t if ! @tags.include? t }
  end
  @tags.sort!
end

#to_sObject



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(tags)
  return unless tags and tags.kind_of? Array
  tags.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

Raises:

  • (ArgumentError)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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