Sha256: 2d51300ac6e2784fbb2ee3c9e4002ca9e070517344d1b4b98f0c163de0bc5e8f

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Teasy
  module AmbiguousTimeHandling
    UnknownAmbiguousTimeHandler = Class.new(StandardError)
    def self.included(base)
      base.extend(ClassMethods)
    end

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

      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

      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

      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
teasy-0.2.4 lib/teasy/ambiguous_time_handling.rb
teasy-0.2.3 lib/teasy/ambiguous_time_handling.rb
teasy-0.2.2 lib/teasy/ambiguous_time_handling.rb
teasy-0.2.1 lib/teasy/ambiguous_time_handling.rb
teasy-0.2.0 lib/teasy/ambiguous_time_handling.rb