Class: SimpleJsonapi::Node::Relationships
- Inherits:
-
Base
- Object
- Base
- SimpleJsonapi::Node::Relationships
show all
- Defined in:
- lib/simple_jsonapi/node/relationships.rb
Overview
Represents a resource’s relationships
object, which contains the individual relationship
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:, relationship_definitions:, **options) ⇒ Relationships
Returns a new instance of Relationships.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/simple_jsonapi/node/relationships.rb', line 18
def initialize(resource:, resource_type:, relationship_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@relationship_definitions = relationship_definitions
@relationship_nodes = relationship_definitions_to_render.map do |_name, defn|
build_child_node(
SimpleJsonapi::Node::Relationship,
resource: resource,
relationship_definition: defn,
)
end
end
|
Instance Attribute Details
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
51
52
53
54
55
56
57
58
59
|
# File 'lib/simple_jsonapi/node/relationships.rb', line 11
class Relationships < Base
attr_reader :resource, :resource_type, :relationship_definitions
def initialize(resource:, resource_type:, relationship_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@relationship_definitions = relationship_definitions
@relationship_nodes = relationship_definitions_to_render.map do |_name, defn|
build_child_node(
SimpleJsonapi::Node::Relationship,
resource: resource,
relationship_definition: defn,
)
end
end
def as_jsonapi
if @relationship_nodes.any?
json = {}
@relationship_nodes.each do |rel_node|
json[rel_node.relationship_name] = rel_node.as_jsonapi
end
{ relationships: json }
else
{}
end
end
private
def relationship_definitions_to_render
@relationship_definitions_to_render ||= begin
include_all_fields = fields_spec.all_fields?(resource_type)
explicit_fields = fields_spec[resource_type]
relationship_definitions
.select { |name, _| include_all_fields || explicit_fields.include?(name) }
.select { |_, defn| render?(defn, resource) }
end
end
end
|
#resource ⇒ Object
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
51
52
53
54
55
56
57
58
59
|
# File 'lib/simple_jsonapi/node/relationships.rb', line 11
class Relationships < Base
attr_reader :resource, :resource_type, :relationship_definitions
def initialize(resource:, resource_type:, relationship_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@relationship_definitions = relationship_definitions
@relationship_nodes = relationship_definitions_to_render.map do |_name, defn|
build_child_node(
SimpleJsonapi::Node::Relationship,
resource: resource,
relationship_definition: defn,
)
end
end
def as_jsonapi
if @relationship_nodes.any?
json = {}
@relationship_nodes.each do |rel_node|
json[rel_node.relationship_name] = rel_node.as_jsonapi
end
{ relationships: json }
else
{}
end
end
private
def relationship_definitions_to_render
@relationship_definitions_to_render ||= begin
include_all_fields = fields_spec.all_fields?(resource_type)
explicit_fields = fields_spec[resource_type]
relationship_definitions
.select { |name, _| include_all_fields || explicit_fields.include?(name) }
.select { |_, defn| render?(defn, resource) }
end
end
end
|
#resource_type ⇒ String
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
51
52
53
54
55
56
57
58
59
|
# File 'lib/simple_jsonapi/node/relationships.rb', line 11
class Relationships < Base
attr_reader :resource, :resource_type, :relationship_definitions
def initialize(resource:, resource_type:, relationship_definitions:, **options)
super(options)
@resource = resource
@resource_type = resource_type
@relationship_definitions = relationship_definitions
@relationship_nodes = relationship_definitions_to_render.map do |_name, defn|
build_child_node(
SimpleJsonapi::Node::Relationship,
resource: resource,
relationship_definition: defn,
)
end
end
def as_jsonapi
if @relationship_nodes.any?
json = {}
@relationship_nodes.each do |rel_node|
json[rel_node.relationship_name] = rel_node.as_jsonapi
end
{ relationships: json }
else
{}
end
end
private
def relationship_definitions_to_render
@relationship_definitions_to_render ||= begin
include_all_fields = fields_spec.all_fields?(resource_type)
explicit_fields = fields_spec[resource_type]
relationship_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}
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/simple_jsonapi/node/relationships.rb', line 35
def as_jsonapi
if @relationship_nodes.any?
json = {}
@relationship_nodes.each do |rel_node|
json[rel_node.relationship_name] = rel_node.as_jsonapi
end
{ relationships: json }
else
{}
end
end
|