Module: Jei::Document::Builder

Defined in:
lib/jei/document/builder.rb

Constant Summary collapse

EMPTY_COLLECTION =
[].freeze
EMPTY_FIELDSETS =
{}.freeze

Class Method Summary collapse

Class Method Details

.build(resource, options = {}) ⇒ Document

Parameters:

  • resource (Object)
  • options (Hash<Symbol, Object>) (defaults to: {})

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jei/document/builder.rb', line 12

def build(resource, options = {})
  document = Document.new
  root = document.root

  build_json_api(root) if options[:jsonapi]
  build_meta(root, options[:meta]) if options[:meta]
  build_links(root, options[:links]) if options[:links]

  if options[:errors]
    build_errors(root, options[:errors])
  elsif resource.nil?
    root[:data] = nil
  elsif resource.is_a?(Enumerable)
    build_collection(root, resource, options)
  else
    build_single(root, resource, options)
  end

  document
end

.build_attributes(context, attributes, serializer) ⇒ Object

Parameters:

See Also:



173
174
175
176
177
178
179
180
181
# File 'lib/jei/document/builder.rb', line 173

def build_attributes(context, attributes, serializer)
  root = {}

  attributes.each do |attribute|
    root[attribute.name] = attribute.evaluate(serializer)
  end

  context[:attributes] = root
end

.build_belongs_to_relationship(context, relationship, serializer) ⇒ Object

Parameters:

See Also:



233
234
235
236
237
238
239
# File 'lib/jei/document/builder.rb', line 233

def build_belongs_to_relationship(context, relationship, serializer)
  root = {}
  resource = relationship.evaluate(serializer)
  serializer = Serializer.factory(resource, relationship.options[:serializer])
  build_resource_identifier(root, serializer)
  context[:data] = root
end

.build_collection(context, resources, options) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)
  • resources (Enumerable)
  • options (Hash<Symbol, Object>)

See Also:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jei/document/builder.rb', line 96

def build_collection(context, resources, options)
  if resources.empty?
    context[:data] = EMPTY_COLLECTION
    return
  end

  fieldsets = options[:fields] ? Fieldset.parse(options[:fields]) : EMPTY_FIELDSETS

  if options[:include]
    paths = Path.parse(options[:include])
    serializers = Set.new

    context[:data] = resources.map do |resource|
      serializer = Serializer.factory(resource, options[:serializer])

      Path.find(paths, serializer, serializers)

      root = {}
      fieldset = fieldsets[serializer.type]

      build_resource(root, serializer, fieldset)

      root
    end

    build_included(context, serializers, fieldsets)
  else
    context[:data] = resources.map do |resource|
      root = {}
      serializer = Serializer.factory(resource, options[:serializer])
      fieldset = fieldsets[serializer.type]

      build_resource(root, serializer, fieldset)

      root
    end
  end
end

.build_errors(context, errors) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)
  • errors (Array<Hash<Symbol, Object>>)

See Also:



277
278
279
# File 'lib/jei/document/builder.rb', line 277

def build_errors(context, errors)
  context[:errors] = errors
end

.build_has_many_relationship(context, relationship, serializer) ⇒ Object

Parameters:

See Also:



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/jei/document/builder.rb', line 245

def build_has_many_relationship(context, relationship, serializer)
  resources = relationship.evaluate(serializer)

  context[:data] =
    if resources.empty?
      EMPTY_COLLECTION
    else
      resources.map do |resource|
        root = {}
        serializer = Serializer.factory(resource, relationship.options[:serializer])
        build_resource_identifier(root, serializer)
        root
      end
  end
end

.build_included(context, serializers, fieldsets) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)
  • serializers (Set<Serializer>)
  • fieldsets (Hash<String, Array<Symbol>>)

See Also:



265
266
267
268
269
270
271
272
# File 'lib/jei/document/builder.rb', line 265

def build_included(context, serializers, fieldsets)
  context[:included] = serializers.map do |serializer|
    root = {}
    fieldset = fieldsets[serializer.type]
    build_resource(root, serializer, fieldset)
    root
  end
end

.build_json_api(context) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)

See Also:



35
36
37
# File 'lib/jei/document/builder.rb', line 35

def build_json_api(context)
  context[:jsonapi] = { version: Document::VERSION }
end

Parameters:

  • context (Hash<Symbol, Object>)
  • link (Link)

See Also:



58
59
60
61
62
63
64
65
# File 'lib/jei/document/builder.rb', line 58

def build_link(context, link)
  context[link.name] =
    if link.meta.any?
      { href: link.href, meta: link.meta }
    else
      link.href
    end
end

Parameters:

  • context (Hash<Symbol, Object>)
  • links (Array<Link>)

See Also:



49
50
51
52
53
# File 'lib/jei/document/builder.rb', line 49

def build_links(context, links)
  root = {}
  links.each { |link| build_link(root, link) }
  context[:links] = root
end

.build_meta(context, meta) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)
  • meta (Hash<Symbol, Object>)

See Also:



42
43
44
# File 'lib/jei/document/builder.rb', line 42

def build_meta(context, meta)
  context[:meta] = meta
end

.build_relationship(context, relationship, serializer) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/jei/document/builder.rb', line 202

def build_relationship(context, relationship, serializer)
  root = {}

  if relationship.options[:data] ||
      override_data?(serializer.options[:linkages], relationship)
    case relationship
    when BelongsToRelationship
      build_belongs_to_relationship(root, relationship, serializer)
    when HasManyRelationship
      build_has_many_relationship(root, relationship, serializer)
    else
      raise ArgumentError, 'invalid relationship type'
    end
  end

  if relationship.options[:links]
    links = relationship.links(serializer)
    build_links(root, links)
  end

  context[relationship.name] = root
end

.build_relationships(context, relationships, serializer) ⇒ Object

Parameters:

See Also:



187
188
189
190
191
192
193
194
195
# File 'lib/jei/document/builder.rb', line 187

def build_relationships(context, relationships, serializer)
  root = {}

  relationships.each do |relationship|
    build_relationship(root, relationship, serializer)
  end

  context[:relationships] = root
end

.build_resource(context, serializer, fieldset) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)
  • serializer (Serializer)
  • fieldset (Array<Symbol>, nil)

See Also:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/jei/document/builder.rb', line 147

def build_resource(context, serializer, fieldset)
  build_resource_identifier(context, serializer)

  attributes = serializer.attributes(fieldset).values

  if attributes.any?
    build_attributes(context, attributes, serializer)
  end

  relationships = serializer.relationships(fieldset).values

  if relationships.any?
    build_relationships(context, relationships, serializer)
  end

  links = serializer.links

  if links
    build_links(context, links)
  end
end

.build_resource_identifier(context, serializer) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)
  • serializer (Serializer)

See Also:



138
139
140
141
# File 'lib/jei/document/builder.rb', line 138

def build_resource_identifier(context, serializer)
  context[:id] = serializer.id
  context[:type] = serializer.type
end

.build_single(context, resource, options) ⇒ Object

Parameters:

  • context (Hash<Symbol, Object>)
  • resource (Object)
  • options (Hash<Symbol, Object>)

See Also:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jei/document/builder.rb', line 71

def build_single(context, resource, options)
  fieldsets = options[:fields] ? Fieldset.parse(options[:fields]) : EMPTY_FIELDSETS

  serializer = Serializer.factory(resource, options[:serializer])
  fieldset = fieldsets[serializer.type]

  if options[:include]
    paths = Path.parse(options[:include])
    serializers = Set.new
    Path.find(paths, serializer, serializers)
  end

  root = {}
  build_resource(root, serializer, fieldset)
  context[:data] = root

  if options[:include]
    build_included(context, serializers, fieldsets)
  end
end

.override_data?(linkages, relationship) ⇒ Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/jei/document/builder.rb', line 225

def override_data?(linkages, relationship)
  linkages && linkages.include?(relationship.name)
end