class Coercible::Coercer::Hash

Coerce Hash values

Constants

TIME_SEGMENTS

Public Instance Methods

to_date(value) click to toggle source

Creates a Date instance from a Hash

Valid keys are: :year, :month, :day, :hour

@param [Hash] value

@return [Date]

@api private

# File lib/coercible/coercer/hash.rb, line 32
def to_date(value)
  ::Date.new(*extract(value).first(3))
end
to_datetime(value) click to toggle source

Creates a DateTime instance from a Hash

Valid keys are: :year, :month, :day, :hour, :min, :sec

@param [Hash] value

@return [DateTime]

@api private

# File lib/coercible/coercer/hash.rb, line 45
def to_datetime(value)
  ::DateTime.new(*extract(value))
end
to_time(value) click to toggle source

Creates a Time instance from a Hash

Valid keys are: :year, :month, :day, :hour, :min, :sec

@param [Hash] value

@return [Time]

@api private

# File lib/coercible/coercer/hash.rb, line 19
def to_time(value)
  ::Time.local(*extract(value))
end

Private Instance Methods

extract(value) click to toggle source

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

# File lib/coercible/coercer/hash.rb, line 60
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