Sha256: 5bab3b5379957a0eae02f81b2e0664ae43a173e401fa7d5461353bbcac6c5a99

Contents?: true

Size: 1.57 KB

Versions: 10

Compression:

Stored size: 1.57 KB

Contents

module Virtus
  class Coercion

    # 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 self.to_time(value)
        ::Time.local(*extract(value))
      end

      # Creates a Date instance from a Hash
      #
      # Valid keys are: :year, :month, :day, :hour
      #
      # @param [Hash] value
      #
      # @return [Date]
      #
      # @api private
      def self.to_date(value)
        ::Date.new(*extract(value).first(3))
      end

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

      # 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 self.extract(value)
        now = ::Time.now

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

      private_class_method :extract

    end # class Hash
  end # class Coercion
end # module Virtus

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
virtus-0.5.5 lib/virtus/coercion/hash.rb
virtus-0.5.4 lib/virtus/coercion/hash.rb
virtus-0.5.3 lib/virtus/coercion/hash.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/virtus-0.5.2/lib/virtus/coercion/hash.rb
virtus-0.5.2 lib/virtus/coercion/hash.rb
virtus-0.5.1 lib/virtus/coercion/hash.rb
virtus-0.5.0 lib/virtus/coercion/hash.rb
virtus-0.4.2 lib/virtus/coercion/hash.rb
virtus-0.4.1 lib/virtus/coercion/hash.rb
virtus-0.4.0 lib/virtus/coercion/hash.rb