Class: Spree::Metafields::Json
- Inherits:
-
Spree::Metafield
- Object
- Spree::Metafield
- Spree::Metafields::Json
- Defined in:
- app/models/spree/metafields/json.rb
Constant Summary
Constants inherited from Spree::Metafield
Spree::Metafield::FIELD_TYPE_TOKENS, Spree::Metafield::TYPE_CLASS_TO_TOKEN, Spree::Metafield::TYPE_TOKENS
Instance Method Summary collapse
- #csv_value ⇒ Object
- #serialize_value ⇒ Object
-
#value=(raw) ⇒ Object
Accept either a JSON-serialized String (from CSV / Admin UI text input) or a raw Hash / Array (from API callers that ship parsed objects).
Methods inherited from Spree::Metafield
Instance Method Details
#csv_value ⇒ Object
22 23 24 |
# File 'app/models/spree/metafields/json.rb', line 22 def csv_value value.to_s end |
#serialize_value ⇒ Object
16 17 18 19 20 |
# File 'app/models/spree/metafields/json.rb', line 16 def serialize_value JSON.parse(value) rescue JSON::ParserError value end |
#value=(raw) ⇒ Object
Accept either a JSON-serialized String (from CSV / Admin UI text input) or a raw Hash / Array (from API callers that ship parsed objects). Non-String inputs get JSON-serialized so the underlying text column always holds canonical JSON.
12 13 14 |
# File 'app/models/spree/metafields/json.rb', line 12 def value=(raw) super(raw.is_a?(Hash) || raw.is_a?(Array) ? raw.to_json : raw) end |