Class: ActiveRecord::TypedStore::Column
- Inherits:
-
BaseColumn
- Object
- ActiveRecord::TypedStore::Column
- Defined in:
- lib/active_record/typed_store/column.rb,
lib/active_record/typed_store/column.rb
Constant Summary collapse
- CAST_TYPES =
{ boolean: ::ActiveRecord::Type::Boolean, integer: ::ActiveRecord::Type::Integer, string: ::ActiveRecord::Type::String, float: ::ActiveRecord::Type::Float, date: ::ActiveRecord::Type::Date, datetime: ::ActiveRecord::Type::DateTime, decimal: DecimalType, any: ::ActiveRecord::Type::Value, }
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#blank ⇒ Object
readonly
Returns the value of attribute blank.
Instance Method Summary collapse
- #accessor? ⇒ Boolean
- #cast(value) ⇒ Object
- #extract_default(value) ⇒ Object
-
#initialize(_, type) ⇒ Column
constructor
A new instance of Column.
- #type_cast(value, map = true) ⇒ Object
Constructor Details
#initialize(_, type) ⇒ Column
Returns a new instance of Column.
6 7 8 9 10 11 12 13 14 |
# File 'lib/active_record/typed_store/column.rb', line 6 def initialize(name, type, ={}) @name = name @type = type @array = .fetch(:array, false) @default = extract_default(.fetch(:default, nil)) @null = .fetch(:null, true) @blank = .fetch(:blank, true) @accessor = .fetch(:accessor, true) end |
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
4 5 6 |
# File 'lib/active_record/typed_store/column.rb', line 4 def array @array end |
#blank ⇒ Object (readonly)
Returns the value of attribute blank.
4 5 6 |
# File 'lib/active_record/typed_store/column.rb', line 4 def blank @blank end |
Instance Method Details
#accessor? ⇒ Boolean
16 17 18 |
# File 'lib/active_record/typed_store/column.rb', line 16 def accessor? @accessor end |
#cast(value) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/active_record/typed_store/column.rb', line 20 def cast(value) casted_value = type_cast(value) if !blank casted_value = default if casted_value.blank? elsif !null casted_value = default if casted_value.nil? end casted_value end |
#extract_default(value) ⇒ Object
30 31 32 33 34 |
# File 'lib/active_record/typed_store/column.rb', line 30 def extract_default(value) return value if (type == :string || type == :text) && value.nil? type_cast(value) end |
#type_cast(value, map = true) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/active_record/typed_store/column.rb', line 36 def type_cast(value, map=true) if array && (map || value.is_a?(Array)) return [] if map && !value.is_a?(Array) return value.map{ |v| type_cast(v, false) } end if type == :string || type == :text return value.to_s unless value.nil? && (null || array) end if IS_AR_3_2 && type == :datetime && value.is_a?(DateTime) return super(value.iso8601) end defined?(super) ? super(value) : type_cast_from_database(value) end |