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| ... } ⇒ Object
Define an association to be serialized.
-
.associations(*association_names) ⇒ Object
(also: belongs_to, has_one, has_many)
Shorthand for defining multiple associations.
-
.attribute(attribute_name) {|object| ... } ⇒ 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
cache, #lookup_serializer, #namespace, #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| ... } ⇒ Object
Note:
By default, the serializer for the association is looked up in the same namespace as the serializer
Define an association to be serialized
81 82 83 84 85 86 87 88 89 |
# File 'lib/transmutation/serializer.rb', line 81 def association(association_name, namespace: nil, serializer: nil, &custom_block) block = lambda do association_instance = custom_block ? instance_exec(&custom_block) : object.send(association_name) serialize(association_instance, namespace:, serializer:, depth: @depth + 1, max_depth: @max_depth) end attributes_config[association_name] = { block:, association: true } end |
.associations(*association_names) ⇒ Object Also known as: belongs_to, has_one, has_many
Shorthand for defining multiple associations
113 114 115 116 117 |
# File 'lib/transmutation/serializer.rb', line 113 def associations(*association_names, **, &) association_names.each do |association_name| association(association_name, **, &) end end |
.attribute(attribute_name) {|object| ... } ⇒ 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
99 100 101 102 103 |
# File 'lib/transmutation/serializer.rb', line 99 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 |