Class: Graphiti::SpecHelpers::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.



6
7
8
9
10
# File 'lib/graphiti/spec_helpers/node.rb', line 6

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



41
42
43
44
45
46
47
# File 'lib/graphiti/spec_helpers/node.rb', line 41

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) Also known as: to_hash

Returns the value of attribute attributes.



4
5
6
# File 'lib/graphiti/spec_helpers/node.rb', line 4

def attributes
  @attributes
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



4
5
6
# File 'lib/graphiti/spec_helpers/node.rb', line 4

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 Also known as: key?

Returns:

  • (Boolean)


26
27
28
# File 'lib/graphiti/spec_helpers/node.rb', line 26

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

#idObject



12
13
14
15
16
# File 'lib/graphiti/spec_helpers/node.rb', line 12

def id
  Integer(rawid) # Only convert if using integer ids
rescue ArgumentError
  rawid
end

#jsonapi_typeObject



22
23
24
# File 'lib/graphiti/spec_helpers/node.rb', line 22

def jsonapi_type
  @attributes["jsonapi_type"]
end


53
54
55
56
57
58
59
60
61
# File 'lib/graphiti/spec_helpers/node.rb', line 53

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



18
19
20
# File 'lib/graphiti/spec_helpers/node.rb', line 18

def rawid
  @attributes["id"]
end

#respond_to_missing?(id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/graphiti/spec_helpers/node.rb', line 49

def respond_to_missing?(id, include_private = false)
  @attributes.has_key?(id) || super
end

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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/graphiti/spec_helpers/node.rb', line 63

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