Sha256: a93b25369d5af67f22382ba4363a0958df00891d2436592ce3b9ba5236ac8ab3

Contents?: true

Size: 805 Bytes

Versions: 10

Compression:

Stored size: 805 Bytes

Contents

module Flipper
  module Expressions
    class Duration
      SECONDS_PER = {
        "second" => 1,
        "minute" => 60,
        "hour" => 3600,
        "day" => 86400,
        "week" => 604_800,
        "month" => 2_629_746,  # 1/12 of a gregorian year
        "year" => 31_556_952 # length of a gregorian year (365.2425 days)
      }.freeze

      def self.call(scalar, unit = 'second')
        unit = unit.to_s.downcase.chomp("s")

        unless scalar.is_a?(Numeric)
          raise ArgumentError.new("Duration value must be a number but was #{scalar.inspect}")
        end
        unless SECONDS_PER[unit]
          raise ArgumentError.new("Duration unit #{unit.inspect} must be one of: #{SECONDS_PER.keys.join(', ')}")
        end

        scalar * SECONDS_PER[unit]
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
flipper-1.3.2 lib/flipper/expressions/duration.rb
flipper-1.3.1 lib/flipper/expressions/duration.rb
flipper-1.3.0 lib/flipper/expressions/duration.rb
flipper-1.3.0.pre lib/flipper/expressions/duration.rb
flipper-1.2.2 lib/flipper/expressions/duration.rb
flipper-1.2.1 lib/flipper/expressions/duration.rb
flipper-1.2.0 lib/flipper/expressions/duration.rb
flipper-1.1.2 lib/flipper/expressions/duration.rb
flipper-1.1.1 lib/flipper/expressions/duration.rb
flipper-1.1.0 lib/flipper/expressions/duration.rb