class IntervalsForHumans::Interval

Public Class Methods

new(start_date, end_date) click to toggle source
# File lib/intervals_for_humans/interval.rb, line 2
def initialize(start_date, end_date)
  @start_date = start_date
  @end_date = end_date
  @date_counter = start_date
end

Public Instance Methods

components() click to toggle source
# File lib/intervals_for_humans/interval.rb, line 8
def components
  @components ||= {
    year: advance_years!,
    month: advance_months!,
    day: advance_days!,
  }
end
to_s() click to toggle source
# File lib/intervals_for_humans/interval.rb, line 16
def to_s
  @to_s ||= begin
    components
      .each_pair
      .select { |(_key, value)| value.positive? }
      .map { |(key, value)| "#{value} #{key}#{'s' if value > 1}" }
      .join(", ")
  end
end

Private Instance Methods

advance!() { |initial_date, advance_value + 1| ... } click to toggle source
# File lib/intervals_for_humans/interval.rb, line 46
def advance!
  initial_date = @date_counter
  advance_value = 0

  loop do
    previous_date = @date_counter
    @date_counter = yield(initial_date, advance_value + 1)

    if @date_counter > @end_date
      @date_counter = previous_date
      break
    end

    advance_value += 1
  end

  advance_value
end
advance_days!() click to toggle source
# File lib/intervals_for_humans/interval.rb, line 40
def advance_days!
  advance! do |date, days|
    date + days
  end
end
advance_months!() click to toggle source
# File lib/intervals_for_humans/interval.rb, line 34
def advance_months!
  advance! do |date, months|
    date >> months
  end
end
advance_years!() click to toggle source
# File lib/intervals_for_humans/interval.rb, line 28
def advance_years!
  advance! do |date, years|
    date >> years * 12
  end
end