Class: ActiveModel::Type::Date

Inherits:
Value
  • Object
show all
Includes:
Helpers::Timezone
Defined in:
lib/active_model/type/date.rb

Overview

Active Model Date Type

Attribute type for date representation. It is registered under the :date key.

class Person
  include ActiveModel::Attributes

  attribute :birthday, :date
end

person = Person.new
person.birthday = "1989-07-13"

person.birthday.class # => Date
person.birthday.year  # => 1989
person.birthday.month # => 7
person.birthday.day   # => 13

String values are parsed using the ISO 8601 date format. Any other values are cast using their to_date method, if it exists.

Instance Attribute Summary

Attributes inherited from Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods included from Helpers::Timezone

#default_timezone, #is_utc?

Methods inherited from Value

#==, #as_json, #assert_valid_value, #binary?, #cast, #changed?, #changed_in_place?, #deserialize, #force_equality?, #hash, #initialize, #map, #mutable?, #serializable?, #serialize, #serialized?, #value_constructed_by_mass_assignment?

Methods included from SerializeCastValue

included, #initialize, #itself_if_serialize_cast_value_compatible, serialize

Constructor Details

This class inherits a constructor from ActiveModel::Type::Value

Instance Method Details

#typeObject



30
31
32
# File 'lib/active_model/type/date.rb', line 30

def type
  :date
end

#type_cast_for_schema(value) ⇒ Object



34
35
36
# File 'lib/active_model/type/date.rb', line 34

def type_cast_for_schema(value)
  value.to_fs(:db).inspect
end