Class: Virtus::Attributes::Time

Inherits:
Object show all
Includes:
Virtus::Attributes::Typecast::Time
Defined in:
lib/virtus/attributes/time.rb

Constant Summary

Constants inherited from Attribute

Attribute::DEFAULT_ACCESSOR, Attribute::OPTIONS

Instance Attribute Summary

Attributes inherited from Attribute

#instance_variable_name, #model, #name, #options, #reader_visibility, #writer_visibility

Instance Method Summary collapse

Methods included from Virtus::Attributes::Typecast::Time

#extract_time

Methods included from Virtus::Attributes::Typecast::Numeric

#typecast_to_numeric

Methods inherited from Attribute

#_create_reader, #_create_writer, accept_options, accepted_options, #complex?, descendants, #get, #get!, inherited, #initialize, options, #primitive?, #set, #set!, #typecast

Constructor Details

This class inherits a constructor from Virtus::Attributes::Attribute

Instance Method Details

#typecast_hash_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 with keys :year, :month, :day, :hour, :min, :sec

Parameters:

  • value (Hash, #to_mash)

    value to be typecast

Returns:

  • (Time)

    Time constructed from hash



40
41
42
# File 'lib/virtus/attributes/time.rb', line 40

def typecast_hash_to_time(value)
  ::Time.local(*extract_time(value))
end

#typecast_to_primitive(value, model = nil) ⇒ 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.

Typecasts an arbitrary value to a Time Handles both Hashes and Time instances.

Parameters:

  • value (Hash, #to_mash, #to_s)

    value to be typecast

Returns:

  • (Time)

    Time constructed from value



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/virtus/attributes/time.rb', line 18

def typecast_to_primitive(value, model = nil)
  if value.respond_to?(:to_time)
    value.to_time
  elsif value.is_a?(::Hash)
    typecast_hash_to_time(value)
  else
    ::Time.parse(value.to_s)
  end
rescue ArgumentError
  value
end