Class: MonthRange::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/month_range/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_rangesObject (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

Raises:



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

Raises:



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_aObject



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