Class: GraphitiSpecHelpers::Node

Inherits:
Object
  • Object
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



39
40
41
42
43
44
45
# File 'lib/graphiti_spec_helpers/node.rb', line 39

def method_missing(id, *args, &blk)
  if @attributes.has_key?(id)
    @attributes[id]
  else
    raise Errors::NoAttribute.new(id)
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/graphiti_spec_helpers/node.rb', line 3

def attributes
  @attributes
end

#relationshipsObject (readonly)

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



27
28
29
# File 'lib/graphiti_spec_helpers/node.rb', line 27

def [](key)
  @attributes[key] || @attributes[key.to_s]
end

#[]=(key, val) ⇒ Object



31
32
33
# File 'lib/graphiti_spec_helpers/node.rb', line 31

def []=(key, val)
  @attributes[key] = val
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/graphiti_spec_helpers/node.rb', line 23

def has_key?(key)
  @attributes.has_key?(key)
end

#idObject



11
12
13
# File 'lib/graphiti_spec_helpers/node.rb', line 11

def id
  rawid.to_i
end

#jsonapi_typeObject



19
20
21
# File 'lib/graphiti_spec_helpers/node.rb', line 19

def jsonapi_type
  @attributes['jsonapi_type']
end


51
52
53
54
55
56
57
58
59
# File 'lib/graphiti_spec_helpers/node.rb', line 51

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

#rawidObject



15
16
17
# File 'lib/graphiti_spec_helpers/node.rb', line 15

def rawid
  @attributes['id']
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/graphiti_spec_helpers/node.rb', line 47

def respond_to?(*args)
  super
end

#sideload(relationship_name) ⇒ Object Also known as: sideloads



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphiti_spec_helpers/node.rb', line 61

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