Module: Teasy::PeriodNotFoundHandling::ClassMethods

Defined in:
lib/teasy/period_not_found_handling.rb

Constant Summary collapse

HANDLER =
{
  raise: ->(_time, _zone) { raise },
  # the biggest change in offsets known to me is when Samoa went from -11
  # to +13 (a full day!) so hopefully we're sure to leave the unknown
  # period by adding/subtracting 3 days
  next_period: lambda do |time, zone|
    period = zone.period_for_local(time + 3 * 86_400)
    [period, period.start_transition.time + period.utc_total_offset]
  end,
  previous_period: lambda do |time, zone|
    period = zone.period_for_local(time - 3 * 86_400)
    [period, period.end_transition.time + period.utc_total_offset]
  end
}.freeze

Instance Method Summary collapse

Instance Method Details

#period_not_found_handlerObject



12
13
14
# File 'lib/teasy/period_not_found_handling.rb', line 12

def period_not_found_handler
  Thread.current[:teasy_period_not_found_handler] ||= HANDLER[:raise]
end

#period_not_found_handler=(name_or_callable) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/teasy/period_not_found_handling.rb', line 16

def period_not_found_handler=(name_or_callable)
  if name_or_callable.respond_to?(:call)
    Thread.current[:teasy_period_not_found_handler] = name_or_callable
  else
    Thread.current[:teasy_period_not_found_handler] = HANDLER.fetch(
      name_or_callable.to_sym
    ) do |key|
      raise UnknownPeriodNotFoundHandler,
            "Don't know a PeriodNotFound handler `#{key}`."
    end
  end
end

#with_period_not_found_handler(handler) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/teasy/period_not_found_handling.rb', line 29

def with_period_not_found_handler(handler)
  old_handler = period_not_found_handler
  self.period_not_found_handler = handler
  yield
ensure
  self.period_not_found_handler = old_handler
end