Class: StoreModel::Types::ArrayType
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- StoreModel::Types::ArrayType
- Defined in:
- lib/store_model/types/array_type.rb
Overview
Implements ActiveModel::Type::Value type for handling an array of StoreModel::Model
Instance Method Summary collapse
-
#cast_value(value) ⇒ Object
Casts
valuefrom DB or user to StoreModel::Model instance. -
#changed_in_place?(raw_old_value, new_value) ⇒ Boolean
Determines whether the mutable value has been modified since it was read.
-
#initialize(model_klass) ⇒ StoreModel::Types::ArrayType
constructor
Initializes type for model class.
-
#serialize(value) ⇒ String
Casts a value from the ruby type to a type that the database knows how to understand.
-
#type ⇒ Symbol
Returns type.
Constructor Details
#initialize(model_klass) ⇒ StoreModel::Types::ArrayType
Initializes type for model class
15 16 17 |
# File 'lib/store_model/types/array_type.rb', line 15 def initialize(model_klass) @model_klass = model_klass end |
Instance Method Details
#cast_value(value) ⇒ Object
Casts value from DB or user to StoreModel::Model instance
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/store_model/types/array_type.rb', line 31 def cast_value(value) case value when String then decode_and_initialize(value) when Array then ensure_model_class(value) when nil then value else raise StoreModel::Types::CastError, "failed casting #{value.inspect}, only String or Array instances are allowed" end end |
#changed_in_place?(raw_old_value, new_value) ⇒ Boolean
Determines whether the mutable value has been modified since it was read
63 64 65 |
# File 'lib/store_model/types/array_type.rb', line 63 def changed_in_place?(raw_old_value, new_value) cast_value(raw_old_value) != new_value end |
#serialize(value) ⇒ String
Casts a value from the ruby type to a type that the database knows how to understand.
48 49 50 51 52 53 54 55 |
# File 'lib/store_model/types/array_type.rb', line 48 def serialize(value) case value when Array ActiveSupport::JSON.encode(value) else super end end |
#type ⇒ Symbol
Returns type
22 23 24 |
# File 'lib/store_model/types/array_type.rb', line 22 def type :array end |