Class: Jiff::DateRange
- Inherits:
-
Object
- Object
- Jiff::DateRange
- Defined in:
- lib/jiff/date_range.rb,
lib/jiff/date_range/version.rb
Overview
Implementation of date range relying in ruby dates (no rails helpers/add-ons)
Constant Summary collapse
- VERSION =
'0.0.1'.freeze
Instance Attribute Summary collapse
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
Instance Method Summary collapse
- #by_month ⇒ Object
- #include?(date) ⇒ Boolean
-
#initialize(start_date, end_date) ⇒ DateRange
constructor
A new instance of DateRange.
-
#overlap(other_range) ⇒ Object
NOTE: Thank you @chrismytton for the implementation suggestion.
- #overlap?(other_range) ⇒ Boolean
- #to_a ⇒ Object
Constructor Details
#initialize(start_date, end_date) ⇒ DateRange
Returns a new instance of DateRange.
12 13 14 15 |
# File 'lib/jiff/date_range.rb', line 12 def initialize(start_date, end_date) @start_date = start_date @end_date = end_date end |
Instance Attribute Details
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
10 11 12 |
# File 'lib/jiff/date_range.rb', line 10 def end_date @end_date end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
9 10 11 |
# File 'lib/jiff/date_range.rb', line 9 def start_date @start_date end |
Instance Method Details
#by_month ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jiff/date_range.rb', line 17 def by_month return month_ends_in_range if date_is_end_of_month(start_date) dates = [] current_date = start_date while current_date <= end_date dates << current_date current_date = next_date(current_date, dates) end dates end |
#include?(date) ⇒ Boolean
33 34 35 36 37 38 39 |
# File 'lib/jiff/date_range.rb', line 33 def include?(date) if date.is_a?(Jiff::DateRange) include?(date.start_date) || include?(date.end_date) else date >= start_date && date <= end_date end end |
#overlap(other_range) ⇒ Object
NOTE: Thank you @chrismytton for the implementation suggestion
49 50 51 52 53 54 |
# File 'lib/jiff/date_range.rb', line 49 def overlap(other_range) return unless overlap?(other_range) dates = [start_date, end_date, other_range.start_date, other_range.end_date].sort[1, 2] Jiff::DateRange.new(*dates) end |
#overlap?(other_range) ⇒ Boolean
41 42 43 44 45 46 |
# File 'lib/jiff/date_range.rb', line 41 def overlap?(other_range) # TODO: Probably should raise 'unsupported type' return unless other_range.is_a? Jiff::DateRange other_range.include?(start_date) || other_range.include?(end_date) || include?(other_range) end |
#to_a ⇒ Object
29 30 31 |
# File 'lib/jiff/date_range.rb', line 29 def to_a date_range.to_a end |