Class: MonthRange::Service

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

Class Method Summary collapse

Class Method Details

.add(range_array, from_range_arrays) ⇒ Array of MonthRange::MRange

Union range

Examples:

from_range_arrays

from_range_arrays = [
  [Date.parse('2020-01-01'), Date.parse('2020-04-01')],
  [Date.parse('2020-07-01'), Date.parse('2020-10-01')],
  [Date.parse('2020-12-01'), nil]
]

Simple

range_array = [Date.parse('2020-03-01'), Date.parse('2020-05-01')]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-05-01 ((2458971j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]

複数期間にまたがる

range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]

隣り合った期間を連結

range_array = [Date.parse('2020-05-01'), Date.parse('2020-06-01')]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]

終端なし

range_array = [Date.parse('2020-05-01'), nil]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, nil]]

Parameters:

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/month_range/service.rb', line 81

def self.add(range_array, from_range_arrays) # rubocop:disable Metrics/MethodLength
  m_ranges = from_range_arrays.map do |range|
    MonthRange::MRange.new(
      MonthRange::Month.create(range[0]),
      MonthRange::Month.create(range[1])
    )
  end
  collection = MonthRange::Collection.new(m_ranges)

  collection.add(MonthRange::MRange.new(
                   MonthRange::Month.create(range_array[0]),
                   MonthRange::Month.create(range_array[1])
                 ))
  collection.to_a
end

.subtraction(range_array, from_range_arrays) ⇒ Object

Subtract range

Examples:

from_range_arrays

from_range_arrays = [
  [Date.parse('2020-01-01'), Date.parse('2020-04-01')],
  [Date.parse('2020-07-01'), Date.parse('2020-10-01')],
  [Date.parse('2020-12-01'), nil]
]

Simple

range_array = [Date.parse('2020-02-01'), Date.parse('2020-03-01')]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>], [#<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]

複数期間にまたがる

range_array = [Date.parse('2020-03-01'), Date.parse('2020-08-01')]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-02-01 ((2458881j,0s,0n),+0s,2299161j)>], [#<Date: 2020-09-01 ((2459094j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]

期間内

range_array = [Date.parse('2020-02-01'), Date.parse('2020-03-01')]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>], [#<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>], [#<Date: 2020-07-01 ((2459032j,0s,0n),+0s,2299161j)>, #<Date: 2020-10-01 ((2459124j,0s,0n),+0s,2299161j)>], [#<Date: 2020-12-01 ((2459185j,0s,0n),+0s,2299161j)>, nil]]

終端なし

range_array = [Date.parse('2020-05-01'), nil]
MonthRange::Service.add(range_array, from_range_arrays)

#=> [[#<Date: 2020-01-01 ((2458850j,0s,0n),+0s,2299161j)>, #<Date: 2020-04-01 ((2458941j,0s,0n),+0s,2299161j)>]]

Parameters:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/month_range/service.rb', line 33

def self.subtraction(range_array, from_range_arrays) # rubocop:disable Metrics/MethodLength:
  m_ranges = from_range_arrays.map do |range|
    MonthRange::MRange.new(
      MonthRange::Month.create(range[0]),
      MonthRange::Month.create(range[1])
    )
  end
  collection = MonthRange::Collection.new(m_ranges)

  collection.subtract(
    MonthRange::MRange.new(
      MonthRange::Month.create(range_array[0]),
      MonthRange::Month.create(range_array[1])
    )
  )
  collection.to_a
end