Class: TimeKeeper::TimeRangeCollection
- Inherits:
-
Object
- Object
- TimeKeeper::TimeRangeCollection
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/time_keeper/time_range_collection.rb
Constant Summary collapse
- TIME_FORMAT_REGEX =
/^(\s*(to|-)\s*)+$/
Instance Attribute Summary collapse
-
#time_ranges ⇒ Object
Returns the value of attribute time_ranges.
-
#time_ranges_str ⇒ Object
Returns the value of attribute time_ranges_str.
Instance Method Summary collapse
-
#initialize(time_ranges_str) ⇒ TimeRangeCollection
constructor
A new instance of TimeRangeCollection.
- #parse ⇒ Object
Constructor Details
#initialize(time_ranges_str) ⇒ TimeRangeCollection
Returns a new instance of TimeRangeCollection.
10 11 12 |
# File 'lib/time_keeper/time_range_collection.rb', line 10 def initialize(time_ranges_str) @time_ranges_str = time_ranges_str.strip.gsub(/\s*(-|to)\s*/, "-") end |
Instance Attribute Details
#time_ranges ⇒ Object
Returns the value of attribute time_ranges.
4 5 6 |
# File 'lib/time_keeper/time_range_collection.rb', line 4 def time_ranges @time_ranges end |
#time_ranges_str ⇒ Object
Returns the value of attribute time_ranges_str.
4 5 6 |
# File 'lib/time_keeper/time_range_collection.rb', line 4 def time_ranges_str @time_ranges_str end |
Instance Method Details
#parse ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/time_keeper/time_range_collection.rb', line 14 def parse spans = @time_ranges_str.split(/\s*\n\s*/).map(&:strip) previous_time_range = nil @time_ranges = spans.inject([]) do |result, span| time_range = TimeKeeper::TimeRange.new(span.strip).parse if previous_time_range time_range.start_day = (time_range.start_time.in_seconds < previous_time_range.end_time.in_seconds ? previous_time_range.end_day + 1 : previous_time_range.end_day) time_range.end_day = time_range.start_day + time_range.days_elapsed end result << time_range previous_time_range = time_range result end self end |