class ActivePeriod::FreePeriod

Public Instance Methods

+(duration) click to toggle source

Shift a period to the future @params see api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-2B @return [self] A new period of the same kind

# File lib/active_period/free_period.rb, line 34
def +(duration)
  self.class.new((from + duration)..(to + duration))
end
-(duration) click to toggle source

Shift a period to the past @params see api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-2B @return [self] A new period of the same kind

# File lib/active_period/free_period.rb, line 27
def -(duration)
  self.class.new((from - duration)..(to - duration))
end
bounding_format() click to toggle source
# File lib/active_period/free_period.rb, line 54
def bounding_format
  if boundless?
    :boundless_format
  elsif beginless?
    :beginless_format
  elsif endless?
    :endless_format
  else
    :default_format
  end
end
ending() click to toggle source
# File lib/active_period/free_period.rb, line 66
def ending
  if exclude_end?
    :excluded
  else
    :included
  end
end
i18n() { |from, to, exclude_end?| ... } click to toggle source

If no block given, it's an alias to to_s For a block {|from,to| … } @yieldparam from [DateTime|Nil] the start of the period @yieldparam to [DateTime|Nil] the end of the period @yieldparam exclude_end? [Boolean] is the ending of the period excluded

# File lib/active_period/free_period.rb, line 79
def i18n(&block)
  return yield(from, to, exclude_end?) if block.present?

  to_s
end
strftime(format) click to toggle source

@param format [String] A valid format for I18n.l @return [String] Formated string

# File lib/active_period/free_period.rb, line 40
def strftime(format)
  to_s(format: format)
end
to_i() click to toggle source

Don't return an Integer. ActiveSupport::Duration is a better numeric representation a in time manipulation context @return [ActiveSupport::Duration] Number of day

# File lib/active_period/free_period.rb, line 19
def to_i
  return Float::INFINITY if infinite?
  days.count.days
end
to_s(format: '%d %B %Y') click to toggle source

@param format [String] A valid format for I18n.l @return [String] Formated string

# File lib/active_period/free_period.rb, line 46
def to_s(format: '%d %B %Y')
  I18n.t(bounding_format,
         scope: %i[active_period free_period],
         from:  I18n.l(self.begin, format: format, default: nil),
         to:    I18n.l(self.end,   format: format, default: nil),
         ending: I18n.t(ending, scope: %i[active_period]))
end