Class: SimpleJsonapi::Node::Attributes
- Inherits:
-
Base
- Object
- Base
- SimpleJsonapi::Node::Attributes
show all
- Defined in:
- lib/simple_jsonapi/node/attributes.rb
Overview
Represents a resource’s attributes
object.
Instance Attribute Summary collapse
Attributes inherited from Base
#extras, #fields_spec, #include_spec, #root_node, #serializer, #serializer_inferrer, #sort_spec
Instance Method Summary
collapse
Constructor Details
#initialize(resource:, resource_type:, attribute_definitions:, **options) ⇒ Attributes
Returns a new instance of Attributes.
17
18
19
20
21
22
23
|
# File 'lib/simple_jsonapi/node/attributes.rb', line 17
def initialize(resource:, resource_type:, attribute_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@attribute_definitions = attribute_definitions
end
|
Instance Attribute Details
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/simple_jsonapi/node/attributes.rb', line 10
class Attributes < Base
attr_reader :resource, :resource_type, :attribute_definitions
def initialize(resource:, resource_type:, attribute_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@attribute_definitions = attribute_definitions
end
def as_jsonapi
if attribute_definitions_to_render.any?
json = {}
attribute_definitions_to_render.each do |name, defn|
json[name] = evaluate(defn.value_proc, resource)
end
{ attributes: json }
else
{}
end
end
private
def attribute_definitions_to_render
@attribute_definitions_to_render ||= begin
include_all_fields = fields_spec.all_fields?(resource_type)
explicit_fields = fields_spec[resource_type]
attribute_definitions
.select { |name, _| include_all_fields || explicit_fields.include?(name) }
.select { |_, defn| render?(defn, resource) }
end
end
end
|
#resource ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/simple_jsonapi/node/attributes.rb', line 10
class Attributes < Base
attr_reader :resource, :resource_type, :attribute_definitions
def initialize(resource:, resource_type:, attribute_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@attribute_definitions = attribute_definitions
end
def as_jsonapi
if attribute_definitions_to_render.any?
json = {}
attribute_definitions_to_render.each do |name, defn|
json[name] = evaluate(defn.value_proc, resource)
end
{ attributes: json }
else
{}
end
end
private
def attribute_definitions_to_render
@attribute_definitions_to_render ||= begin
include_all_fields = fields_spec.all_fields?(resource_type)
explicit_fields = fields_spec[resource_type]
attribute_definitions
.select { |name, _| include_all_fields || explicit_fields.include?(name) }
.select { |_, defn| render?(defn, resource) }
end
end
end
|
#resource_type ⇒ String
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/simple_jsonapi/node/attributes.rb', line 10
class Attributes < Base
attr_reader :resource, :resource_type, :attribute_definitions
def initialize(resource:, resource_type:, attribute_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@attribute_definitions = attribute_definitions
end
def as_jsonapi
if attribute_definitions_to_render.any?
json = {}
attribute_definitions_to_render.each do |name, defn|
json[name] = evaluate(defn.value_proc, resource)
end
{ attributes: json }
else
{}
end
end
private
def attribute_definitions_to_render
@attribute_definitions_to_render ||= begin
include_all_fields = fields_spec.all_fields?(resource_type)
explicit_fields = fields_spec[resource_type]
attribute_definitions
.select { |name, _| include_all_fields || explicit_fields.include?(name) }
.select { |_, defn| render?(defn, resource) }
end
end
end
|
Instance Method Details
#as_jsonapi ⇒ Hash{Symbol => Hash}
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/simple_jsonapi/node/attributes.rb', line 26
def as_jsonapi
if attribute_definitions_to_render.any?
json = {}
attribute_definitions_to_render.each do |name, defn|
json[name] = evaluate(defn.value_proc, resource)
end
{ attributes: json }
else
{}
end
end
|