Class: MonthRange::Collection
- Inherits:
-
Object
- Object
- MonthRange::Collection
- Defined in:
- lib/month_range/collection.rb
Instance Attribute Summary collapse
-
#stored_m_ranges ⇒ Object
readonly
Returns the value of attribute stored_m_ranges.
Instance Method Summary collapse
- #add(m_range) ⇒ Object
-
#initialize(m_ranges = []) ⇒ Collection
constructor
A new instance of Collection.
- #subtract(m_range) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(m_ranges = []) ⇒ Collection
Returns a new instance of Collection.
6 7 8 9 |
# File 'lib/month_range/collection.rb', line 6 def initialize(m_ranges = []) @stored_m_ranges = [] m_ranges.each { |m_range| add(m_range) } end |
Instance Attribute Details
#stored_m_ranges ⇒ Object (readonly)
Returns the value of attribute stored_m_ranges.
4 5 6 |
# File 'lib/month_range/collection.rb', line 4 def stored_m_ranges @stored_m_ranges end |
Instance Method Details
#add(m_range) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/month_range/collection.rb', line 17 def add(m_range) raise MonthRange::Error, 'Need MonthRange::MRange' unless m_range.is_a?(MonthRange::MRange) return @stored_m_ranges << m_range if @stored_m_ranges.empty? update_stored_m_ranges(m_range) do |overlapped_collection_ranges| [merge_range(overlapped_collection_ranges, m_range)] end end |
#subtract(m_range) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/month_range/collection.rb', line 26 def subtract(m_range) raise MonthRange::Error, 'Need MonthRange::MRange' unless m_range.is_a?(MonthRange::MRange) return if @stored_m_ranges.empty? update_stored_m_ranges(m_range) do |overlapped_collection_ranges| subtract_range(overlapped_collection_ranges, m_range) end end |
#to_a ⇒ Object
11 12 13 14 15 |
# File 'lib/month_range/collection.rb', line 11 def to_a @stored_m_ranges.map do |collection_range| [collection_range.start_month.to_date, collection_range.end_month.to_date] end end |