Class: TimeLog
- Inherits:
-
Object
- Object
- TimeLog
- Defined in:
- lib/timesheet/time_log.rb
Instance Method Summary collapse
- #add(entry) ⇒ Object
- #count ⇒ Object
- #delete(record_number) ⇒ Object
- #extract_entries(start_time, end_time) ⇒ Object
- #find(record_number) ⇒ Object
-
#initialize(store) ⇒ TimeLog
constructor
A new instance of TimeLog.
- #update(record_number, properties) ⇒ Object
Constructor Details
#initialize(store) ⇒ TimeLog
Returns a new instance of TimeLog.
3 4 5 6 7 8 9 |
# File 'lib/timesheet/time_log.rb', line 3 def initialize(store) @store = store @store.transaction do @store[:entries] ||= [] @store[:entry_record_number] ||= 0 end end |
Instance Method Details
#add(entry) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/timesheet/time_log.rb', line 17 def add(entry) @store.transaction do raise ArgumentError.new("Cannot add an entry which conflicts with existing entries.") if @store[:entries].any? { |e| e.conflict? entry } @store[:entry_record_number] += 1 entry.record_number = @store[:entry_record_number] @store[:entries] << entry end end |
#count ⇒ Object
11 12 13 14 15 |
# File 'lib/timesheet/time_log.rb', line 11 def count @store.transaction do @store[:entries].count end end |
#delete(record_number) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/timesheet/time_log.rb', line 60 def delete(record_number) @store.transaction do found_entries = @store[:entries].select{|e| e.record_number == record_number} raise "Record number #{record_number} is not unique in the database." if found_entries.count > 1 raise ArgumentError.new("Cannot find an entry with record number #{record_number} to delete.") if found_entries.count == 0 @store[:entries].delete(found_entries.first) end end |
#extract_entries(start_time, end_time) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/timesheet/time_log.rb', line 36 def extract_entries(start_time, end_time) covered_entries = [] @store.transaction do span_to_cover = Range.new(start_time, end_time, true) covered_entries = @store[:entries].select{|e| span_to_cover.overlap? e.to_range } end covered_entries end |
#find(record_number) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/timesheet/time_log.rb', line 26 def find(record_number) @store.transaction do found_entries = @store[:entries].select{|e| e.record_number == record_number} raise "Record number #{record_number} is not unique in the database." if found_entries.count > 1 raise ArgumentError.new("Cannot find an entry with record number #{record_number} to edit") if found_entries.count == 0 found_entries.first end end |
#update(record_number, properties) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/timesheet/time_log.rb', line 45 def update(record_number, properties) @store.transaction do found_entries = @store[:entries].select{|e| e.record_number == record_number} raise "Record number #{record_number} is not unique in the database." if found_entries.count > 1 raise ArgumentError.new("Cannot find an entry with record number #{record_number} to edit") if found_entries.count == 0 entry = found_entries.first entry.project = properties[:project] if properties[:project] entry.start_time = properties[:start] if properties[:start] entry.end_time = properties[:end] if properties[:end] entry.comment = properties[:comment] if properties[:comment] raise ArgumentError.new("The new times for this entry conflict with existing entries.") if @store[:entries].any? { |e| e != entry && e.conflict?(entry) } end end |