Class: GraphitiGql::ActiveResource::Node

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/graphiti_gql/active_resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash, resource = nil) ⇒ Node

Returns a new instance of Node.



7
8
9
10
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
# File 'lib/graphiti_gql/active_resource.rb', line 7

def initialize(hash, resource = nil)
  @resource = resource
  @edges = {}
  @node_id = hash.delete(:node_id)
  hash.each_pair do |key, value|
    if value.is_a?(Hash)
      if (sideload = resource.sideload(key))
        if value.key?(:edges)
          @edges[key] = value[:edges].map do |edge|
            node_id = edge[:node][:id] if edge[:node]
            Node.new(edge.except(:node).merge(node_id: node_id))
          end
          if value[:edges].any? { |e| e[:node] }
            hash[key] = value[:edges].map { |v| Node.new(v[:node], sideload.resource.class) }
          else
            hash[key] = value[:edges]
          end
        elsif value.key?(:nodes)
          hash[key] = value[:nodes].map { |n| Node.new(n, sideload.resource.class) }
        else
          hash[key] = Node.new(value, sideload.resource.class)
        end
      else
        hash[key] = Node.new(value)
      end
    end
  end
  super(hash)
end

Instance Method Details

#_idObject



51
52
53
54
# File 'lib/graphiti_gql/active_resource.rb', line 51

def _id
  deserialized = @resource.deserialize_gid(id)
  deserialized.to_i.to_s == deserialized ? deserialized.to_i : deserialized
end

#edge(name, node_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphiti_gql/active_resource.rb', line 37

def edge(name, node_id)
  found = @edges[name].empty? ? nil : @edges[name]
  if found && node_id
    found.find do |f|
      gql_node_id = f.instance_variable_get(:@node_id)
      gql_node_id == node_id ||
        gql_node_id == node_id.to_s ||
        gql_node_id == @resource.gid(node_id)
    end
  else
    found
  end
end