Class: Virtus::Attributes::Date
- Includes:
- Typecast::Time
- Defined in:
- lib/virtus/attributes/date.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
-
#typecast_hash_to_date(value) ⇒ Date
private
Creates a Date instance from a Hash with keys :year, :month, :day.
-
#typecast_to_primitive(value) ⇒ Date
private
Typecasts an arbitrary value to a Date Handles both Hashes and Date instances.
Methods included from Typecast::Time
Methods included from Typecast::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_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 with keys :year, :month, :day
39 40 41 |
# File 'lib/virtus/attributes/date.rb', line 39 def typecast_hash_to_date(value) ::Date.new(*extract_time(value)[0, 3]) end |
#typecast_to_primitive(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.
Typecasts an arbitrary value to a Date Handles both Hashes and Date instances.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/virtus/attributes/date.rb', line 18 def typecast_to_primitive(value) if value.respond_to?(:to_date) value.to_date elsif value.is_a?(::Hash) typecast_hash_to_date(value) else ::Date.parse(value.to_s) end rescue ArgumentError value end |