Class: StoreModel::JsonModelType
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- StoreModel::JsonModelType
- Defined in:
- lib/store_model/json_model_type.rb
Instance Method Summary collapse
-
#cast_value(value) ⇒ Object
rubocop:disable Style/RescueModifier.
- #changed_in_place?(raw_old_value, new_value) ⇒ Boolean
-
#initialize(model_klass) ⇒ JsonModelType
constructor
A new instance of JsonModelType.
-
#serialize(value) ⇒ Object
rubocop:enable Style/RescueModifier.
- #type ⇒ Object
Constructor Details
#initialize(model_klass) ⇒ JsonModelType
Returns a new instance of JsonModelType.
7 8 9 |
# File 'lib/store_model/json_model_type.rb', line 7 def initialize(model_klass) @model_klass = model_klass end |
Instance Method Details
#cast_value(value) ⇒ Object
rubocop:disable Style/RescueModifier
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/store_model/json_model_type.rb', line 16 def cast_value(value) case value when String decoded = ActiveSupport::JSON.decode(value) rescue nil @model_klass.new(decoded) unless decoded.nil? when Hash @model_klass.new(value) when @model_klass value end end |
#changed_in_place?(raw_old_value, new_value) ⇒ Boolean
38 39 40 |
# File 'lib/store_model/json_model_type.rb', line 38 def changed_in_place?(raw_old_value, new_value) cast_value(raw_old_value) != new_value end |
#serialize(value) ⇒ Object
rubocop:enable Style/RescueModifier
29 30 31 32 33 34 35 36 |
# File 'lib/store_model/json_model_type.rb', line 29 def serialize(value) case value when Hash, @model_klass ActiveSupport::JSON.encode(value) else super end end |
#type ⇒ Object
11 12 13 |
# File 'lib/store_model/json_model_type.rb', line 11 def type :json end |