Sha256: 00e467ac74b936426c609a8eb682b1febd41486c592808180b78165f9cbbf379

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Teasy
  module PeriodNotFoundHandling
    UnknownPeriodNotFoundHandler = Class.new(StandardError)

    def self.included(base)
      base.extend(ClassMethods)
    end

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

      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

      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

      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.at.to_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.at.to_time + period.utc_total_offset]
        end
      }.freeze
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
teasy-0.2.8 lib/teasy/period_not_found_handling.rb
teasy-0.2.7 lib/teasy/period_not_found_handling.rb
teasy-0.2.6 lib/teasy/period_not_found_handling.rb