class Period::FreePeriod

Public Class Methods

new(range) click to toggle source
Calls superclass method
# File lib/period/free_period.rb, line 17
def initialize(range)
  raise ::ArgumentError, I18n.t(:param_must_be_a_range, scope: %i[period free_period]) unless range.class.ancestors.include?(Range)
  from = range.first
  to = range.last

  from = time_parse(range.first, I18n.t(:start_date_is_invalid, scope: %i[period free_period])).beginning_of_day
  to = time_parse(range.last, I18n.t(:end_date_is_invalid, scope: %i[period free_period])).end_of_day
  to = to.prev_day if range.exclude_end?
  raise ::ArgumentError, I18n.t(:start_is_greater_than_end, scope: %i[period free_period]) if from > to

  super(from, to)
end

Public Instance Methods

+(duration) click to toggle source
# File lib/period/free_period.rb, line 75
def +(duration)
  self.class.new((from + duration)..(to + duration))
end
-(duration) click to toggle source
# File lib/period/free_period.rb, line 71
def -(duration)
  self.class.new((from - duration)..(to - duration))
end
<=>(other) click to toggle source
# File lib/period/free_period.rb, line 57
def <=>(other)
  if other.is_a?(ActiveSupport::Duration) || other.is_a?(Numeric)
    to_i <=> other.to_i
  elsif self.class != other.class
    raise ArgumentError, I18n.t(:incomparable_error, scope: :free_period)
  else
    (from <=> other)
  end
end
==(other) click to toggle source
# File lib/period/free_period.rb, line 79
def ==(other)
  raise ArgumentError unless other.class.ancestors.include?(Period::FreePeriod)

  from == other.from && to == other.to
end
i18n() { |from, to| ... } click to toggle source
# File lib/period/free_period.rb, line 96
def i18n(&block)
  return yield(from, to) if block.present?

  to_s
end
include?(other) click to toggle source
Calls superclass method
# File lib/period/free_period.rb, line 45
def include?(other)
  if other.class.in?([DateTime, Time, ActiveSupport::TimeWithZone])
    from.to_i <= other.to_i && other.to_i <= to.to_i
  elsif other.is_a? Date
    super(Period::Day.new(other))
  elsif other.class.ancestors.include?(Period::FreePeriod)
    super(other)
  else
    raise ArgumentError, I18n.t(:incomparable_error, scope: :free_period)
  end
end
next() click to toggle source
# File lib/period/free_period.rb, line 36
def next
  raise NotImplementedError
end
Also aliased as: succ
prev() click to toggle source
# File lib/period/free_period.rb, line 41
def prev
  raise NotImplementedError
end
strftime(format) click to toggle source
# File lib/period/free_period.rb, line 85
def strftime(format)
  to_s(format: format)
end
succ()
Alias for: next
to_i() click to toggle source
# File lib/period/free_period.rb, line 67
def to_i
  days.count.days
end
to_s(format: '%d %B %Y') click to toggle source
# File lib/period/free_period.rb, line 89
def to_s(format: '%d %B %Y')
  I18n.t(:default_format,
         scope: %i[period free_period],
         from:  I18n.l(from, format: format),
         to:    I18n.l(to, format: format))
end

Private Instance Methods

time_parse(time, msg) click to toggle source
# File lib/period/free_period.rb, line 104
def time_parse(time, msg)
  if time.class.in? [String, Date]
    Period.env_time.parse(time.to_s)
  elsif time.class.in? [Time, ActiveSupport::TimeWithZone]
    time
  else
    raise ::ArgumentError, msg
  end
rescue StandardError
  raise ::ArgumentError, msg
end