Class: Panko::Serializer
- Inherits:
-
Object
- Object
- Panko::Serializer
- Defined in:
- lib/panko/serializer.rb
Constant Summary collapse
- SKIP =
Object.new.freeze
Class Attribute Summary collapse
-
._descriptor ⇒ Object
Returns the value of attribute _descriptor.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#serialization_context ⇒ Object
writeonly
Sets the attribute serialization_context.
Class Method Summary collapse
- .aliases(aliases = {}) ⇒ Object
- .attributes(*attrs) ⇒ Object
- .has_many(name, options = {}) ⇒ Object
- .has_one(name, options = {}) ⇒ Object
- .inherited(base) ⇒ Object
- .method_added(method) ⇒ Object
Instance Method Summary collapse
- #context ⇒ Object
-
#initialize(options = {}) ⇒ Serializer
constructor
A new instance of Serializer.
- #scope ⇒ Object
- #serialize(object) ⇒ Object
- #serialize_to_json(object) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Serializer
Returns a new instance of Serializer.
109 110 111 112 113 114 115 116 |
# File 'lib/panko/serializer.rb', line 109 def initialize( = {}) # this "_skip_init" trick is so I can create serializers from serialization descriptor return if [:_skip_init] @serialization_context = SerializationContext.create() @descriptor = Panko::SerializationDescriptor.build(self.class, , @serialization_context) @used = false end |
Class Attribute Details
._descriptor ⇒ Object
Returns the value of attribute _descriptor.
55 56 57 |
# File 'lib/panko/serializer.rb', line 55 def _descriptor @_descriptor end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
127 128 129 |
# File 'lib/panko/serializer.rb', line 127 def object @object end |
#serialization_context=(value) ⇒ Object (writeonly)
Sets the attribute serialization_context
126 127 128 |
# File 'lib/panko/serializer.rb', line 126 def serialization_context=(value) @serialization_context = value end |
Class Method Details
.aliases(aliases = {}) ⇒ Object
61 62 63 64 65 |
# File 'lib/panko/serializer.rb', line 61 def aliases(aliases = {}) aliases.each do |attr, alias_name| @_descriptor.attributes << Attribute.create(attr, alias_name: alias_name) end end |
.attributes(*attrs) ⇒ Object
57 58 59 |
# File 'lib/panko/serializer.rb', line 57 def attributes(*attrs) @_descriptor.attributes.push(*attrs.map { |attr| Attribute.create(attr) }).uniq! end |
.has_many(name, options = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/panko/serializer.rb', line 92 def has_many(name, = {}) serializer_const = [:serializer] || [:each_serializer] if serializer_const.is_a?(String) serializer_const = Panko::SerializerResolver.resolve(serializer_const, self) end serializer_const ||= Panko::SerializerResolver.resolve(name.to_s, self) raise "Can't find serializer for #{self.name}.#{name} has_many relationship." if serializer_const.nil? @_descriptor.has_many_associations << Panko::Association.new( name, .fetch(:name, name).to_s, Panko::SerializationDescriptor.build(serializer_const, ) ) end |
.has_one(name, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/panko/serializer.rb', line 76 def has_one(name, = {}) serializer_const = [:serializer] if serializer_const.is_a?(String) serializer_const = Panko::SerializerResolver.resolve(serializer_const, self) end serializer_const ||= Panko::SerializerResolver.resolve(name.to_s, self) raise "Can't find serializer for #{self.name}.#{name} has_one relationship." if serializer_const.nil? @_descriptor.has_one_associations << Panko::Association.new( name, .fetch(:name, name).to_s, Panko::SerializationDescriptor.build(serializer_const, ) ) end |
.inherited(base) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/panko/serializer.rb', line 38 def inherited(base) if _descriptor.nil? base._descriptor = Panko::SerializationDescriptor.new base._descriptor.attributes = [] base._descriptor.aliases = {} base._descriptor.method_fields = [] base._descriptor.has_many_associations = [] base._descriptor.has_one_associations = [] else base._descriptor = Panko::SerializationDescriptor.duplicate(_descriptor) end base._descriptor.type = base end |
.method_added(method) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/panko/serializer.rb', line 67 def method_added(method) super return if @_descriptor.nil? deleted_attr = @_descriptor.attributes.delete(method) @_descriptor.method_fields << Attribute.create(deleted_attr.name, alias_name: deleted_attr.alias_name) unless deleted_attr.nil? end |
Instance Method Details
#context ⇒ Object
118 119 120 |
# File 'lib/panko/serializer.rb', line 118 def context @serialization_context.context end |
#scope ⇒ Object
122 123 124 |
# File 'lib/panko/serializer.rb', line 122 def scope @serialization_context.scope end |
#serialize(object) ⇒ Object
129 130 131 |
# File 'lib/panko/serializer.rb', line 129 def serialize(object) serialize_with_writer(object, Panko::ObjectWriter.new).output end |
#serialize_to_json(object) ⇒ Object
133 134 135 |
# File 'lib/panko/serializer.rb', line 133 def serialize_to_json(object) serialize_with_writer(object, Oj::StringWriter.new(mode: :rails)).to_s end |