Class: Neon::Node::Rest

Inherits:
Object
  • Object
show all
Includes:
PropertyContainer::Rest
Defined in:
lib/neon/node/rest.rb

Constant Summary collapse

QUERIES =

Move to a separate file

{
  :[] => lambda do |node, *keys|
    query = "START n = node({id})\nWITH "
    query << keys.map { |key| "n.#{key.to_s.strip} as #{key.to_s.strip}" }.join(", ")
    query << "\nRETURN "
    query << keys.map { |key| key.to_s.strip }.join(", ")
    [query, {id: node.id}]
  end
}
RESULT_PARSER =

Move to a seprate file

{
  :[] => lambda do |result, *keys|
    parsed_result = []
    result = result.first
    columns = result["columns"]
    data = result["data"].first["row"].dup
    raise "Corrupted result" if columns.length != keys.length
    for i in 0...keys.length
      parsed_result << if keys[i] == columns[i]
                        data.shift
                      else
                        nil
                      end
    end
    if keys.length == 1
      parsed_result.pop
    else
      parsed_result
    end
  end
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PropertyContainer::Rest

#==, #[], #[]=, #del, #destroy, #props, #props=

Methods included from TransactionHelpers::Rest

#run_rest_transaction

Constructor Details

#initialize(node, session) ⇒ Node::Rest

Initialize the node with a neography node and a REST session

Parameters:

  • node (Hash)

    a neogrpahy node hash.

  • session (Session::Rest)

    the session this node was initialized to.



17
18
19
20
21
# File 'lib/neon/node/rest.rb', line 17

def initialize(node, session)
  @session = session # Set the session
  @node = node # Set the node
  @id = node["self"].split('/').last.to_i # Set the id
end

Instance Attribute Details

#idObject (readonly)

The neo id of this node.



8
9
10
# File 'lib/neon/node/rest.rb', line 8

def id
  @id
end

#nodeObject (readonly)

The neography hash containing information about the node.



9
10
11
# File 'lib/neon/node/rest.rb', line 9

def node
  @node
end

#sessionObject (readonly)

The Rest session this node belongs to.



7
8
9
# File 'lib/neon/node/rest.rb', line 7

def session
  @session
end

Instance Method Details

#create_rel_to(end_node, type, attributes = {}) ⇒ Relationship::Rest

Create a unidirectional relationship starting from this node to another node.

Parameters:

  • end_node (Node::Rest)

    the end node for the unidirectional relationship.

  • type (String, Symbol)

    the type of this relationship.

  • attributes (Hash) (defaults to: {})

    a hash of the initial property-value pairs.

Returns:



34
35
36
37
38
39
40
41
42
# File 'lib/neon/node/rest.rb', line 34

def create_rel_to(end_node, type, attributes = {})
  return nil if @session.url != end_node.session.url
  attributes.delete_if { |key, value| value.nil? }
  neo_rel = @session.neo.create_relationship(type, @node, end_node.node, attributes)
  return nil if neo_rel.nil?
  rel = Relationship::Rest.new(neo_rel, @session)
rescue NoMethodError => e
  _raise_doesnt_exist_anymore_error(e)
end

#parse_result(result, method, *args) ⇒ Object



83
84
85
# File 'lib/neon/node/rest.rb', line 83

def parse_result(result, method, *args)
  RESULT_PARSER[method].call(result, *args)
end

#query_for(method, *args) ⇒ Object



55
56
57
58
# File 'lib/neon/node/rest.rb', line 55

def query_for(method, *args)
  # Fetch appropriate query and covert the args to a hash corresponding the query parameters
  QUERIES[method].call(self, *args)
end

#to_sObject



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

def to_s
  "REST Node[#{@id}]"
end