Class: Virtus::Coercion::Hash

Inherits:
Object show all
Defined in:
lib/virtus/coercion/hash.rb

Overview

Coerce Hash values

Constant Summary collapse

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

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Class Method Summary collapse

Methods inherited from Object

method_missing

Methods inherited from Virtus::Coercion

[]

Methods included from DescendantsTracker

#add_descendant, #descendants

Methods included from TypeLookup

#determine_type, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Class Method Details

.to_array(value) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an Array instance from a Hash

Parameters:

Returns:

  • (Array)


17
18
19
# File 'lib/virtus/coercion/hash.rb', line 17

def self.to_array(value)
  value.to_a
end

.to_date(value) ⇒ Date

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a Date instance from a Hash

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

Parameters:

Returns:



43
44
45
# File 'lib/virtus/coercion/hash.rb', line 43

def self.to_date(value)
  ::Date.new(*extract(value).first(3))
end

.to_datetime(value) ⇒ DateTime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a DateTime instance from a Hash

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

Parameters:

Returns:



56
57
58
# File 'lib/virtus/coercion/hash.rb', line 56

def self.to_datetime(value)
  ::DateTime.new(*extract(value))
end

.to_time(value) ⇒ Time

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a Time instance from a Hash

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

Parameters:

Returns:



30
31
32
# File 'lib/virtus/coercion/hash.rb', line 30

def self.to_time(value)
  ::Time.local(*extract(value))
end