Class: StoreModel::Types::OnePolymorphic
- Inherits:
-
OneBase
- Object
- ActiveModel::Type::Value
- OneBase
- StoreModel::Types::OnePolymorphic
- Includes:
- PolymorphicHelper
- Defined in:
- lib/store_model/types/one_polymorphic.rb
Overview
Implements ActiveModel::Type::Value type for handling an instance of StoreModel::Model
Instance Method Summary collapse
-
#cast_value(value) ⇒ Object
Casts
value
from DB or user to StoreModel::Model instance. -
#initialize(model_wrapper) ⇒ StoreModel::Types::OnePolymorphic
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.
Methods included from PolymorphicHelper
#implements_model?, #raise_extract_wrapper_error
Methods inherited from OneBase
Constructor Details
#initialize(model_wrapper) ⇒ StoreModel::Types::OnePolymorphic
Initializes type for model class
16 17 18 |
# File 'lib/store_model/types/one_polymorphic.rb', line 16 def initialize(model_wrapper) @model_wrapper = model_wrapper end |
Instance Method Details
#cast_value(value) ⇒ Object
Casts value
from DB or user to StoreModel::Model instance
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/store_model/types/one_polymorphic.rb', line 32 def cast_value(value) case value when String then decode_and_initialize(value) when Hash then extract_model_klass(value).new(value) when nil then value else raise_cast_error(value) unless value.class.ancestors.include?(StoreModel::Model) value end rescue ActiveModel::UnknownAttributeError => e handle_unknown_attribute(value, e) end |
#serialize(value) ⇒ String
Casts a value from the ruby type to a type that the database knows how to understand.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/store_model/types/one_polymorphic.rb', line 52 def serialize(value) case value when Hash ActiveSupport::JSON.encode(value) else return ActiveSupport::JSON.encode(value) if implements_model?(value.class) super end end |
#type ⇒ Symbol
Returns type
23 24 25 |
# File 'lib/store_model/types/one_polymorphic.rb', line 23 def type :polymorphic end |