Class: Dionysus::Producer::ModelSerializer
- Inherits:
-
Object
- Object
- Dionysus::Producer::ModelSerializer
- Defined in:
- lib/dionysus/producer/model_serializer.rb
Direct Known Subclasses
Class Method Summary collapse
- .attribute(name, options = {}) ⇒ Object
- .attributes(*names) ⇒ Object
- .declared_attributes ⇒ Object
- .declared_to_many_relationships ⇒ Object
- .declared_to_one_relationships ⇒ Object
- .has_many(name, options = {}) ⇒ Object
- .has_one(name, options = {}) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(record, include:, context_serializer:) ⇒ ModelSerializer
constructor
A new instance of ModelSerializer.
Constructor Details
#initialize(record, include:, context_serializer:) ⇒ ModelSerializer
Returns a new instance of ModelSerializer.
7 8 9 10 11 |
# File 'lib/dionysus/producer/model_serializer.rb', line 7 def initialize(record, include:, context_serializer:) @record = record @include = include @context_serializer = context_serializer end |
Class Method Details
.attribute(name, options = {}) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/dionysus/producer/model_serializer.rb', line 19 def self.attribute(name, = {}) declared_attributes << [name, ] define_method(name) do record.public_send(name) end end |
.attributes(*names) ⇒ Object
13 14 15 16 17 |
# File 'lib/dionysus/producer/model_serializer.rb', line 13 def self.attributes(*names) Array(names).each do |name| attribute(name, {}) end end |
.declared_attributes ⇒ Object
51 52 53 |
# File 'lib/dionysus/producer/model_serializer.rb', line 51 def self.declared_attributes @declared_attributes ||= [] end |
.declared_to_many_relationships ⇒ Object
59 60 61 |
# File 'lib/dionysus/producer/model_serializer.rb', line 59 def self.declared_to_many_relationships @declared_to_many_relationships ||= [] end |
.declared_to_one_relationships ⇒ Object
55 56 57 |
# File 'lib/dionysus/producer/model_serializer.rb', line 55 def self.declared_to_one_relationships @declared_to_one_relationships ||= [] end |
.has_many(name, options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dionysus/producer/model_serializer.rb', line 39 def self.has_many(name, = {}) declared_to_many_relationships << [name, ] define_method(name) do record.public_send(name) end define_method("#{name.to_s.singularize}_ids") do record.public_send("#{name.to_s.singularize}_ids") end end |
.has_one(name, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dionysus/producer/model_serializer.rb', line 27 def self.has_one(name, = {}) declared_to_one_relationships << [name, ] define_method(name) do record.public_send(name) end define_method("#{name}_id") do record.public_send("#{name}_id") end end |
Instance Method Details
#as_json ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/dionysus/producer/model_serializer.rb', line 63 def as_json {}.tap do |payload| declared_attributes.each do |declared_attribute, | payload[declared_attribute] = send(declared_attribute) end payload["links"] = {} declared_to_one_relationships.each do |declared_relationship, | payload["links"][declared_relationship] = send("#{declared_relationship}_id") end declared_to_many_relationships.each do |declared_relationship, | payload["links"][declared_relationship] = send("#{declared_relationship.to_s.singularize}_ids") end include.each do |relationship_to_include| relationship_to_include = relationship_to_include.to_sym if declared_to_one_relationships.to_h.key?(relationship_to_include) payload[relationship_to_include] = context_serializer.serialize(send(relationship_to_include), dependencies: []).first end if declared_to_many_relationships.to_h.key?(relationship_to_include) payload[relationship_to_include] = context_serializer.serialize(send(relationship_to_include), dependencies: []) end end end.deep_stringify_keys end |