Module: Teasy::AmbiguousTimeHandling::ClassMethods

Defined in:
lib/teasy/ambiguous_time_handling.rb

Constant Summary collapse

HANDLER =
{
  # By returning nil TZInfo will raise TZInfo::AmbigousTime. It'd be
  # better to raise our own error, but that would break the API that's out
  # there. So that will have to wait for a 1.x release.
  raise: ->(_time, _periods) {},
  daylight_savings_time: ->(_time, periods) { periods.select(&:dst?) },
  standard_time: ->(_time, periods) { periods.reject(&:dst?) }
}.freeze

Instance Method Summary collapse

Instance Method Details

#ambiguous_time_handlerObject



11
12
13
# File 'lib/teasy/ambiguous_time_handling.rb', line 11

def ambiguous_time_handler
  Thread.current[:teasy_ambiguous_time_handler] ||= HANDLER[:raise]
end

#ambiguous_time_handler=(name_or_callable) ⇒ Object



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

def ambiguous_time_handler=(name_or_callable)
  if name_or_callable.respond_to?(:call)
    Thread.current[:teasy_ambiguous_time_handler] = name_or_callable
  else
    Thread.current[:teasy_ambiguous_time_handler] = HANDLER.fetch(
      name_or_callable.to_sym
    ) do |key|
      raise UnknownAmbiguousTimeHandler,
            "Don't know an ambiguous time handler `#{key}`."
    end
  end
end

#with_ambiguous_time_handler(handler) ⇒ Object



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

def with_ambiguous_time_handler(handler)
  old_handler = ambiguous_time_handler
  self.ambiguous_time_handler = handler
  yield
ensure
  self.ambiguous_time_handler = old_handler
end