class Garbanzo::Interval

Attributes

length[R]
unit[R]

Public Class Methods

new(length = 1, unit = :month) click to toggle source
# File lib/garbanzo/interval.rb, line 5
def initialize(length = 1, unit = :month)
  @length = length.to_i
  @unit = normalize_unit unit.to_sym
end

Public Instance Methods

to_h() click to toggle source
# File lib/garbanzo/interval.rb, line 10
def to_h
  {
    length: length,
    unit: unit
  }
end
valid?() click to toggle source
# File lib/garbanzo/interval.rb, line 17
def valid?
  unit_valid? && length_valid?
end

Private Instance Methods

days?() click to toggle source
# File lib/garbanzo/interval.rb, line 41
def days?
  unit == :days
end
length_valid?() click to toggle source
# File lib/garbanzo/interval.rb, line 31
def length_valid?
  fail NotImplementedError unless unit_valid?
  return length.between?(1, 12) if months?
  return length.between?(7, 365) if days?
end
months?() click to toggle source
# File lib/garbanzo/interval.rb, line 37
def months?
  unit == :months
end
normalize_unit(unit) click to toggle source
# File lib/garbanzo/interval.rb, line 23
def normalize_unit(unit)
  unit == :month ? :months : unit
end
unit_valid?() click to toggle source
# File lib/garbanzo/interval.rb, line 27
def unit_valid?
  months? || days?
end