Class: Porridge::FieldSerializer
- Inherits:
-
Serializer
- Object
- Serializer
- Porridge::FieldSerializer
- Defined in:
- lib/porridge/field_serializer.rb
Overview
FieldSerializer is a serializer that adds a “field” to a hash. It does so by using a predefined “field name” as the key and evaluates an Extractor for the object for the value.
FieldSerializer is the most opinionated piece of the porridge framework. In particular, it adds to a :field_hierarchy
array in the options hash to keep track of nested fields. It also requires the use of a FieldPolicy object given in the options to determine whether a given field is allowed. Do not be afraid to subclass FieldSerializer or even create a new “field serializer” class altogether if you want to substantially change the way fields are implemented.
Instance Attribute Summary collapse
-
#extractor ⇒ Extractor, #call
readonly
private
The value extractor to use to retrieve a value from the object for which serialization is occurring.
-
#name ⇒ Object
readonly
private
The name of the field being serialized by this FieldSerializer; used as the key for the field in the hash.
Instance Method Summary collapse
-
#add_field_to_hierarchy!(options_hash) ⇒ void
private
Safely adds the current #name to the
:field_hierarchy
in the given options hash. -
#allowed?(object, options) ⇒ Boolean
private
Determines whether the given object/options, along with the current #name constitutes a valid field.
-
#call(object, hash, options) ⇒ Hash
Serializes the given input hash for the given object with the given options by adding an element to the hash with a key that is equal to the field name (#name) and a value extracted from the object using the field extractor (#extractor).
-
#hash_with_field(object, hash, options) ⇒ Hash
private
Creates a new hash from the given one with a field for the given object injected.
-
#initialize(name, extractor) ⇒ FieldSerializer
constructor
Creates a new instance of FieldSerializer with the given field name and value extractor.
Methods inherited from Serializer
Constructor Details
#initialize(name, extractor) ⇒ FieldSerializer
Creates a new instance of Porridge::FieldSerializer with the given field name and value extractor.
18 19 20 21 22 23 |
# File 'lib/porridge/field_serializer.rb', line 18 def initialize(name, extractor) @name = name @extractor = extractor Extractor.ensure_valid!(extractor) super() end |
Instance Attribute Details
#extractor ⇒ Extractor, #call (readonly, private)
The value extractor to use to retrieve a value from the object for which serialization is occurring.
86 87 88 |
# File 'lib/porridge/field_serializer.rb', line 86 def extractor @extractor end |
#name ⇒ Object (readonly, private)
The name of the field being serialized by this Porridge::FieldSerializer; used as the key for the field in the hash.
82 83 84 |
# File 'lib/porridge/field_serializer.rb', line 82 def name @name end |
Instance Method Details
#add_field_to_hierarchy!(options_hash) ⇒ void (private)
This method returns an undefined value.
Safely adds the current #name to the :field_hierarchy
in the given options hash. While the options hash itself is mutated, the field hierarchy array is first duplicated, meaning the options hash must have first been duplicated for this to be safe.
66 67 68 69 70 |
# File 'lib/porridge/field_serializer.rb', line 66 def add_field_to_hierarchy!() [:field_hierarchy] ||= [] [:field_hierarchy] = [:field_hierarchy].dup [:field_hierarchy] << name end |
#allowed?(object, options) ⇒ Boolean (private)
Determines whether the given object/options, along with the current #name constitutes a valid field. Currently, this is done by simply delegating to the field policy which should have been provided as an option.
56 57 58 59 |
# File 'lib/porridge/field_serializer.rb', line 56 def allowed?(object, ) FieldPolicy.ensure_valid!([:field_policy]) [:field_policy].allowed?(name, object, .except(:field_policy)) end |
#call(object, hash, options) ⇒ Hash
Serializes the given input hash for the given object with the given options by adding an element to the hash with a key that is equal to the field name (#name) and a value extracted from the object using the field extractor (#extractor).
37 38 39 40 41 42 43 44 45 |
# File 'lib/porridge/field_serializer.rb', line 37 def call(object, hash, ) if allowed?(object, ) = .dup add_field_to_hierarchy!() hash_with_field(object, hash, ) else hash end end |
#hash_with_field(object, hash, options) ⇒ Hash (private)
Creates a new hash from the given one with a field for the given object injected.
77 78 79 |
# File 'lib/porridge/field_serializer.rb', line 77 def hash_with_field(object, hash, ) hash.merge(name => extractor.call(object, )) end |