Sha256: d09f009266b25a86b7b9b82d45d8891b5a626f8f34afa2836fd24345d59e2530

Contents?: true

Size: 983 Bytes

Versions: 1

Compression:

Stored size: 983 Bytes

Contents

module Coercible
  class Coercer

    # Coerce Hash values
    class Hash < Object
      primitive ::Hash

      TIME_SEGMENTS = [ :year, :month, :day, :hour, :min, :sec ].freeze

      # Creates a Time instance from a Hash
      #
      # Valid keys are: :year, :month, :day, :hour, :min, :sec
      #
      # @param [Hash] value
      #
      # @return [Time]
      #
      # @api private
      def to_time(value)
        ::Time.local(*extract(value))
      end

      private

      # Extracts the given args from a Hash
      #
      # If a value does not exist, it uses the value of Time.now
      #
      # @param [Hash] value
      #
      # @return [Array]
      #
      # @api private
      def extract(value)
        now = ::Time.now

        TIME_SEGMENTS.map do |segment|
          val = value.fetch(segment, now.public_send(segment))
          coercers[val.class].to_integer(val)
        end
      end

    end # class Hash

  end # class Coercer
end # module Coercible

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion_coercible-0.2.0 lib/project/coercer/hash.rb