Class: GraphitiSpecHelpers::Node
- Inherits:
-
Object
- Object
- GraphitiSpecHelpers::Node
show all
- Defined in:
- lib/graphiti_spec_helpers/node.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes, relationships, context) ⇒ Node
Returns a new instance of Node.
5
6
7
8
9
|
# File 'lib/graphiti_spec_helpers/node.rb', line 5
def initialize(attributes, relationships, context)
@attributes = attributes.with_indifferent_access
@relationships = relationships.with_indifferent_access if relationships
@context = context
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args, &blk) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/graphiti_spec_helpers/node.rb', line 43
def method_missing(id, *args, &blk)
if @attributes.has_key?(id)
@attributes[id]
else
raise Errors::NoAttribute.new(id)
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/graphiti_spec_helpers/node.rb', line 3
def attributes
@attributes
end
|
#relationships ⇒ Object
Returns the value of attribute relationships.
3
4
5
|
# File 'lib/graphiti_spec_helpers/node.rb', line 3
def relationships
@relationships
end
|
Instance Method Details
#[](key) ⇒ Object
31
32
33
|
# File 'lib/graphiti_spec_helpers/node.rb', line 31
def [](key)
@attributes[key] || @attributes[key.to_s]
end
|
#[]=(key, val) ⇒ Object
35
36
37
|
# File 'lib/graphiti_spec_helpers/node.rb', line 35
def []=(key, val)
@attributes[key] = val
end
|
#has_key?(key) ⇒ Boolean
27
28
29
|
# File 'lib/graphiti_spec_helpers/node.rb', line 27
def has_key?(key)
@attributes.has_key?(key)
end
|
#id ⇒ Object
11
12
13
14
15
16
17
|
# File 'lib/graphiti_spec_helpers/node.rb', line 11
def id
begin
Integer(rawid) rescue ArgumentError
rawid
end
end
|
#jsonapi_type ⇒ Object
23
24
25
|
# File 'lib/graphiti_spec_helpers/node.rb', line 23
def jsonapi_type
@attributes['jsonapi_type']
end
|
#link(relationship_name, name) ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'lib/graphiti_spec_helpers/node.rb', line 55
def link(relationship_name, name)
if @relationships.has_key?(relationship_name)
links = @relationships[relationship_name][:links]
raise Errors::LinksNotFound.new(relationship_name) unless links
links[name]
else
raise Errors::SideloadNotFound.new(relationship_name)
end
end
|
#rawid ⇒ Object
19
20
21
|
# File 'lib/graphiti_spec_helpers/node.rb', line 19
def rawid
@attributes['id']
end
|
#respond_to?(*args) ⇒ Boolean
51
52
53
|
# File 'lib/graphiti_spec_helpers/node.rb', line 51
def respond_to?(*args)
super
end
|
#sideload(relationship_name) ⇒ Object
Also known as:
sideloads
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/graphiti_spec_helpers/node.rb', line 65
def sideload(relationship_name)
unless @relationships.has_key?(relationship_name)
raise Errors::SideloadNotFound.new(relationship_name)
end
rel = @relationships[relationship_name]
rel = rel[:data]
return if rel.nil?
if rel.is_a?(Hash)
include_for(rel[:type], rel[:id])
else
rel.map { |r| include_for(r[:type], r[:id]) }
end
end
|