Class: Transmutation::Serializer
- Inherits:
-
Object
- Object
- Transmutation::Serializer
- Extended by:
- ClassAttributes
- Includes:
- Serialization
- Defined in:
- lib/transmutation/serializer.rb
Overview
Base class for your serializers.
Direct Known Subclasses
Class Method Summary collapse
-
.association(association_name, namespace: nil, serializer: nil) ⇒ Object
(also: belongs_to, has_one, has_many)
Define an association to be serialized.
-
.associations(*association_names) ⇒ Object
Shorthand for defining multiple associations.
-
.attribute(attribute_name, &block) ⇒ Object
Define an attribute to be serialized.
-
.attributes(*attribute_names) ⇒ Object
Shorthand for defining multiple attributes.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#initialize(object, depth: 0, max_depth: 1) ⇒ Serializer
constructor
A new instance of Serializer.
- #to_json(options = {}) ⇒ Object
Methods included from ClassAttributes
class_attribute, class_attribute_reader, class_attribute_writer
Methods included from Serialization
#lookup_serializer, #serialize
Constructor Details
#initialize(object, depth: 0, max_depth: 1) ⇒ Serializer
Returns a new instance of Serializer.
23 24 25 26 27 |
# File 'lib/transmutation/serializer.rb', line 23 def initialize(object, depth: 0, max_depth: 1) @object = object @depth = depth @max_depth = max_depth end |
Class Method Details
.association(association_name, namespace: nil, serializer: nil) ⇒ Object Also known as: belongs_to, has_one, has_many
Note:
By default, the serializer for the association is looked up in the same namespace as the serializer
Define an association to be serialized
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/transmutation/serializer.rb', line 75 def association(association_name, namespace: nil, serializer: nil) block = lambda do serialize( object.send(association_name), namespace:, serializer:, depth: @depth + 1, max_depth: @max_depth ) end attributes_config[association_name] = { block:, association: true } end |
.associations(*association_names) ⇒ Object
Shorthand for defining multiple associations
115 116 117 118 119 |
# File 'lib/transmutation/serializer.rb', line 115 def associations(*association_names) association_names.each do |association_name| association(association_name) end end |
.attribute(attribute_name, &block) ⇒ Object
Define an attribute to be serialized
58 59 60 |
# File 'lib/transmutation/serializer.rb', line 58 def attribute(attribute_name, &block) attributes_config[attribute_name] = { block: } end |
.attributes(*attribute_names) ⇒ Object
Shorthand for defining multiple attributes
101 102 103 104 105 |
# File 'lib/transmutation/serializer.rb', line 101 def attributes(*attribute_names) attribute_names.each do |attribute_name| attribute(attribute_name) end end |
Instance Method Details
#as_json(options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/transmutation/serializer.rb', line 33 def as_json( = {}) attributes_config.each_with_object({}) do |(attr_name, ), hash| if [:association] hash[attr_name.to_s] = instance_exec(&[:block]).as_json() if @depth + 1 <= @max_depth else hash[attr_name.to_s] = [:block] ? instance_exec(&[:block]) : object.send(attr_name) end end end |
#to_json(options = {}) ⇒ Object
29 30 31 |
# File 'lib/transmutation/serializer.rb', line 29 def to_json( = {}) as_json().to_json end |