Class: Dionysus::Producer::ModelSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/dionysus/producer/model_serializer.rb

Direct Known Subclasses

DeletedRecordSerializer

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  declared_attributes << [name, options]

  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_attributesObject



51
52
53
# File 'lib/dionysus/producer/model_serializer.rb', line 51

def self.declared_attributes
  @declared_attributes ||= []
end

.declared_to_many_relationshipsObject



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_relationshipsObject



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, options = {})
  declared_to_many_relationships << [name, options]

  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, options = {})
  declared_to_one_relationships << [name, options]

  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_jsonObject



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, _options|
      payload[declared_attribute] = send(declared_attribute)
    end
    payload["links"] = {}
    declared_to_one_relationships.each do |declared_relationship, _options|
      payload["links"][declared_relationship] = send("#{declared_relationship}_id")
    end
    declared_to_many_relationships.each do |declared_relationship, _options|
      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