Class: Monthly::Splitter

Inherits:
Object
  • Object
show all
Defined in:
lib/monthly/splitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Splitter



5
6
7
8
9
# File 'lib/monthly/splitter.rb', line 5

def initialize(from, to)
  @from, @to = from, to
  @current_from = self.from
  @current_to = last_day_of_month(self.from)
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



3
4
5
# File 'lib/monthly/splitter.rb', line 3

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



3
4
5
# File 'lib/monthly/splitter.rb', line 3

def to
  @to
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/monthly/splitter.rb', line 11

def execute
  [].tap do |out|
    out << {from: self.from, to: self.to} if out_of_range?(@current_to)
    while in_range?(@current_to)
      out << {from: @current_from, to: @current_to}
      out << {from: next_from, to: self.to} if last_month_is_partial?
      set_next_from_and_to
    end
  end
end