Sha256: f02fc9b25d3898f8d703dfb1ea396e14c32549c13a61694b78b2efa70ba06d75

Contents?: true

Size: 1.53 KB

Versions: 14

Compression:

Stored size: 1.53 KB

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

      # Creates a Date instance from a Hash
      #
      # Valid keys are: :year, :month, :day, :hour
      #
      # @param [Hash] value
      #
      # @return [Date]
      #
      # @api private
      def 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 to_datetime(value)
        ::DateTime.new(*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

14 entries across 12 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/hash.rb
coercible-1.0.0 lib/coercible/coercer/hash.rb
coercible-0.2.0 lib/coercible/coercer/hash.rb
coercible-0.1.0 lib/coercible/coercer/hash.rb
coercible-0.0.2 lib/coercible/coercer/hash.rb
coercible-0.0.1 lib/coercible/coercer/hash.rb