class GraphitiSpecHelpers::Node

Attributes

attributes[R]
relationships[R]

Public Class Methods

new(attributes, relationships, context) click to toggle source
# 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

Public Instance Methods

[](key) click to toggle source
# File lib/graphiti_spec_helpers/node.rb, line 27
def [](key)
  @attributes[key] || @attributes[key.to_s]
end
[]=(key, val) click to toggle source
# File lib/graphiti_spec_helpers/node.rb, line 31
def []=(key, val)
  @attributes[key] = val
end
has_key?(key) click to toggle source
# File lib/graphiti_spec_helpers/node.rb, line 23
def has_key?(key)
  @attributes.has_key?(key)
end
id() click to toggle source
# File lib/graphiti_spec_helpers/node.rb, line 11
def id
  rawid.to_i
end
jsonapi_type() click to toggle source
# File lib/graphiti_spec_helpers/node.rb, line 19
def jsonapi_type
  @attributes['jsonapi_type']
end
method_missing(id, *args, &blk) click to toggle source
# 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
rawid() click to toggle source
# File lib/graphiti_spec_helpers/node.rb, line 15
def rawid
  @attributes['id']
end
respond_to?(*args) click to toggle source
Calls superclass method
# File lib/graphiti_spec_helpers/node.rb, line 47
def respond_to?(*args)
  super
end
sideload(relationship_name) click to toggle source
# 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
Also aliased as: sideloads
sideloads(relationship_name)
Alias for: sideload

Private Instance Methods

include_for(type, id) click to toggle source
# File lib/graphiti_spec_helpers/node.rb, line 78
def include_for(type, id)
  data = @context.json['included'].find do |i|
    i['type'] == type && i['id'] == id
  end
  @context.node(from: data)
end