Class: ArrayOfObjectType
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- ArrayOfObjectType
- Defined in:
- app/types/array_of_object_type.rb
Overview
Custom ActiveRecord/ActiveModel attribute type that hydrates a JSON
array column into an array of value objects of the given class
(e.g. plain ActiveModel::Model data classes or Data.define'd
records). Use via attribute :foo, ArrayOfObjectType.new(SomeClass).
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #deserialize(value) ⇒ Object
-
#initialize(klass) ⇒ ArrayOfObjectType
constructor
A new instance of ArrayOfObjectType.
- #serialize(value) ⇒ Object
Constructor Details
#initialize(klass) ⇒ ArrayOfObjectType
Returns a new instance of ArrayOfObjectType.
7 8 9 |
# File 'app/types/array_of_object_type.rb', line 7 def initialize(klass) @klass = klass end |
Instance Method Details
#cast(value) ⇒ Object
11 12 13 14 15 |
# File 'app/types/array_of_object_type.rb', line 11 def cast(value) return [] if value.blank? value.map { |v| @klass.new(v) } end |
#deserialize(value) ⇒ Object
21 22 23 |
# File 'app/types/array_of_object_type.rb', line 21 def deserialize(value) cast(value) end |
#serialize(value) ⇒ Object
17 18 19 |
# File 'app/types/array_of_object_type.rb', line 17 def serialize(value) value.map(&:attributes) end |