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
Instance Attribute Summary collapse
- #context ⇒ Object, ... readonly
- #depth ⇒ Object, ... readonly
- #max_depth ⇒ Object, ... readonly
- #object ⇒ Object, ... readonly
Class Method Summary collapse
-
.association(association_name, namespace: nil, serializer: nil, **options) {|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, **options) {|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, context: nil) ⇒ 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, context: nil) ⇒ Serializer
Returns a new instance of Serializer.
23 24 25 26 27 28 |
# File 'lib/transmutation/serializer.rb', line 23 def initialize(object, depth: 0, max_depth: 1, context: nil) @object = object @depth = depth @max_depth = max_depth @context = context end |
Instance Attribute Details
#context ⇒ Object, ... (readonly)
126 127 128 |
# File 'lib/transmutation/serializer.rb', line 126 def context @context end |
#depth ⇒ Object, ... (readonly)
126 127 128 |
# File 'lib/transmutation/serializer.rb', line 126 def depth @depth end |
#max_depth ⇒ Object, ... (readonly)
126 127 128 |
# File 'lib/transmutation/serializer.rb', line 126 def max_depth @max_depth end |
#object ⇒ Object, ... (readonly)
126 127 128 |
# File 'lib/transmutation/serializer.rb', line 126 def object @object end |
Class Method Details
.association(association_name, namespace: nil, serializer: nil, **options) {|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
85 86 87 |
# File 'lib/transmutation/serializer.rb', line 85 def association(association_name, namespace: nil, serializer: nil, **, &block) fields[association_name] = Association.new(association_name, namespace:, serializer:, **, &block) end |
.associations(*association_names) ⇒ Object Also known as: belongs_to, has_one, has_many
Shorthand for defining multiple associations
111 112 113 114 115 |
# File 'lib/transmutation/serializer.rb', line 111 def associations(*association_names, **, &) association_names.each do |association_name| association(association_name, **, &) end end |
.attribute(attribute_name, **options) {|object| ... } ⇒ Object
Define an attribute to be serialized
60 61 62 |
# File 'lib/transmutation/serializer.rb', line 60 def attribute(attribute_name, **, &block) fields[attribute_name] = Attribute.new(attribute_name, **, &block) end |
.attributes(*attribute_names) ⇒ Object
Shorthand for defining multiple attributes
97 98 99 100 101 |
# File 'lib/transmutation/serializer.rb', line 97 def attributes(*attribute_names, **, &) attribute_names.each do |attribute_name| attribute(attribute_name, **, &) end end |
Instance Method Details
#as_json(options = {}) ⇒ Object
34 35 36 37 38 |
# File 'lib/transmutation/serializer.rb', line 34 def as_json( = {}) fields.each_with_object({}) do |field, hash| hash[field.key] = field.value(self, ) if field.render?(self) end end |
#to_json(options = {}) ⇒ Object
30 31 32 |
# File 'lib/transmutation/serializer.rb', line 30 def to_json( = {}) as_json().to_json end |