Class: Neo4j::Server::CypherNode

Inherits:
Node
  • Object
show all
Includes:
Core::ActiveEntity, Core::CypherTranslator, Resource
Defined in:
lib/neo4j-server/cypher_node.rb

Constant Summary

Constant Summary

Constants included from Core::CypherTranslator

Core::CypherTranslator::EMPTY_PROPS, Core::CypherTranslator::SANITIZE_ESCAPED_REGEXP

Constants included from PropertyValidator

PropertyValidator::VALID_PROPERTY_VALUE_CLASSES

Instance Attribute Summary (collapse)

Attributes included from Resource

#resource_data, #resource_url

Instance Method Summary (collapse)

Methods included from Core::ActiveEntity

#persisted?

Methods included from Core::CypherTranslator

#create_escape_value, #cypher_prop_list, #cypher_string, #escape_quotes, #escape_value, #label_string, #prop_identifier, #sanitize_escape_sequences, sanitized_column_names, translate_response

Methods included from Resource

#convert_from_json_value, #expect_response_code, #handle_response_error, #init_resource_data, #resource_headers, #resource_url_id, #response_exception, #wrap_resource

Methods inherited from Node

_load, #_rel, create, find_nodes, load

Methods included from PropertyContainer

#[], #[]=

Methods included from PropertyValidator

#valid_property?, #validate_property

Methods included from Node::Wrapper

#neo4j_obj, #wrapper

Methods included from EntityEquality

#==

Constructor Details

- (CypherNode) initialize(session, value)

Returns a new instance of CypherNode



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/neo4j-server/cypher_node.rb', line 8

def initialize(session, value)
  @session = session

  @neo_id = if value.is_a?(Hash)
              hash = value['data']
              @props = Hash[hash.map { |k, v| [k.to_sym, v] }]
              @labels = value['metadata']['labels'].map!(&:to_sym) if value['metadata']
              value['id'] # value['self'].match(/\d+$/)[0].to_i
            else
              value
            end
end

Instance Attribute Details

- (Object) neo_id (readonly)

Returns the value of attribute neo_id



21
22
23
# File 'lib/neo4j-server/cypher_node.rb', line 21

def neo_id
  @neo_id
end

Instance Method Details

- (Object) _cypher_label_list(labels_list)



111
112
113
# File 'lib/neo4j-server/cypher_node.rb', line 111

def _cypher_label_list(labels_list)
  ':' + labels_list.map { |label| "`#{label}`" }.join(':')
end

- (Object) _java_node

TODO, needed by neo4j-cypher



28
29
30
# File 'lib/neo4j-server/cypher_node.rb', line 28

def _java_node
  self
end

- (Object) _map_result(r)



184
185
186
# File 'lib/neo4j-server/cypher_node.rb', line 184

def _map_result(r)
  r.to_node_enumeration.map(&:result)
end

- (Object) add_label(*new_labels)



115
116
117
118
# File 'lib/neo4j-server/cypher_node.rb', line 115

def add_label(*new_labels)
  @session._query_or_fail("#{match_start} SET n #{_cypher_label_list(new_labels)}", false, neo_id: neo_id)
  new_labels.each { |label| labels << label }
end

- (Object) create_rel(type, other_node, props = nil)

Creates a relationship of given type to other_node with optionally properties

Parameters:

  • type (Symbol)

    the type of the relation between the two nodes

  • other_node (Neo4j::Node)

    the other node

  • props (Hash<Symbol, Object>) (defaults to: nil)

    optionally properties for the created relationship



33
34
35
36
37
38
39
# File 'lib/neo4j-server/cypher_node.rb', line 33

def create_rel(type, other_node, props = nil)
  ids_hash = {start_neo_id: neo_id, end_neo_id: other_node.neo_id}
  props_with_ids = props.nil? ? ids_hash : cypher_prop_list(props).merge(ids_hash)
  id = @session._query_or_fail(rel_string(type, other_node, props), true, props_with_ids)
  data_hash = {'type' => type, 'data' => props, 'start' => neo_id, 'end' => other_node.neo_id, 'id' => id}
  CypherRelationship.new(@session, data_hash)
end

- (Object) del Also known as: delete, destroy

Deletes this node from the database



131
132
133
# File 'lib/neo4j-server/cypher_node.rb', line 131

def del
  @session._query_or_fail("#{match_start} OPTIONAL MATCH n-[r]-() DELETE n, r", false, neo_id: neo_id)
end

- (Boolean) exist?

Returns true if the node exists

Returns:

  • (Boolean)

    true if the node exists



139
140
141
# File 'lib/neo4j-server/cypher_node.rb', line 139

def exist?
  @session._query("#{match_start} RETURN ID(n)", neo_id: neo_id).data.empty? ? false : true
end

- (Object) get_property(key)

Directly get the property on the node (low level method, may need transaction)

Parameters:

  • key (Symbol, String)

Returns:

  • the value of the key



102
103
104
# File 'lib/neo4j-server/cypher_node.rb', line 102

def get_property(key)
  @props ? @props[key.to_sym] : @session._query_or_fail("#{match_start} RETURN n.`#{key}`", true, neo_id: neo_id)
end

- (Object) inspect



23
24
25
# File 'lib/neo4j-server/cypher_node.rb', line 23

def inspect
  "CypherNode #{neo_id} (#{object_id})"
end

- (Object) labels

Returns all the Neo4j labels for this node

Returns:

  • all the Neo4j labels for this node



107
108
109
# File 'lib/neo4j-server/cypher_node.rb', line 107

def labels
  @labels ||= @session._query_or_fail("#{match_start} RETURN labels(n) as labels", true, neo_id: neo_id).map!(&:to_sym)
end

- (Object) node(match = {})

Returns the only node of a given type and direction that is attached to this node, or nil. This is a convenience method that is used in the commonly occuring situation where a node has exactly zero or one relationships of a given type and direction to another node. Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an exception.

This method reflects that semantics and returns either:

  • nil if there are zero relationships of the given type and direction,

  • the relationship if there's exactly one, or

  • throws an exception in all other cases.

This method should be used only in situations with an invariant as described above. In those situations, a “state-checking” method (e.g. #rel?) is not required, because this method behaves correctly “out of the box.”



144
145
146
# File 'lib/neo4j-server/cypher_node.rb', line 144

def node(match = {})
  ensure_single_relationship { match(CypherNode, 'p as result LIMIT 2', match) }
end

- (Enumerable<Neo4j::Node>) nodes(match = {})

This method is abstract.
Note:

it's possible that the same node is returned more than once because of several relationship reaching to the same node, see #outgoing for alternative

Works like #rels method but instead returns the nodes. It does try to load a Ruby wrapper around each node

Parameters:

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

    the options to create a message with.

Returns:

  • (Enumerable<Neo4j::Node>)

    an Enumeration of either Neo4j::Node objects or wrapped Neo4j::Node objects



160
161
162
# File 'lib/neo4j-server/cypher_node.rb', line 160

def nodes(match = {})
  match(CypherNode, 'p as result', match)
end

- (Hash<Symbol, Object>) props

Returns all properties of the node

Returns:

  • (Hash<Symbol, Object>)

    all properties of the node



46
47
48
49
50
51
52
53
# File 'lib/neo4j-server/cypher_node.rb', line 46

def props
  if @props
    @props
  else
    hash = @session._query_entity_data("#{match_start} RETURN n", nil, neo_id: neo_id)
    @props = Hash[hash['data'].map { |k, v| [k.to_sym, v] }]
  end
end

- (Object) props=(properties)

replace all properties with new properties

Parameters:

  • properties (Hash<Symbol, Object>)

    a hash of properties the node should have



73
74
75
76
77
# File 'lib/neo4j-server/cypher_node.rb', line 73

def props=(properties)
  refresh
  @session._query_or_fail("#{match_start} SET n = { props }", false,  props: properties, neo_id: neo_id)
  properties
end

- (Object) refresh



55
56
57
# File 'lib/neo4j-server/cypher_node.rb', line 55

def refresh
  @props = nil
end

- (Object) rel(match = {})

Same as #node but returns the relationship. Notice it may raise an exception if there are more then one relationship matching.



149
150
151
# File 'lib/neo4j-server/cypher_node.rb', line 149

def rel(match = {})
  ensure_single_relationship { match(CypherRelationship, 'r as result LIMIT 2', match) }
end

- (Boolean) rel?(match = {})

Returns true or false if there is one or more relationships

Returns:

  • (Boolean)


154
155
156
157
# File 'lib/neo4j-server/cypher_node.rb', line 154

def rel?(match = {})
  result = match(CypherRelationship, 'r as result', match)
  !!result.first
end

- (Object) rel_string(type, other_node, props)



41
42
43
# File 'lib/neo4j-server/cypher_node.rb', line 41

def rel_string(type, other_node, props)
  "MATCH (a), (b) WHERE ID(a) = {start_neo_id} AND ID(b) = {end_neo_id} CREATE (a)-[r:`#{type}` #{prop_identifier(props)}]->(b) RETURN ID(r)"
end

- (Enumerable<Neo4j::Relationship>) rels(match = {dir: :both})

Returns an enumeration of relationships. It always returns relationships of depth one.

Examples:

Return both incoming and outgoing relationships of any type

node_a.rels

All outgoing or incoming relationship of type friends

node_a.rels(type: :friends)

All outgoing relationships between me and another node of type friends

node_a.rels(type: :friends, dir: :outgoing, between: node_b)

Parameters:

  • match (Hash) (defaults to: {dir: :both})

    the options to create a message with.

Options Hash (match):

  • :dir (Symbol)

    dir the direction of the relationship, allowed values: :both, :incoming, :outgoing.

  • :type (Symbol)

    the type of relationship to navigate

  • :between (Symbol)

    return all the relationships between this and given node

Returns:



165
166
167
# File 'lib/neo4j-server/cypher_node.rb', line 165

def rels(match = {dir: :both})
  match(CypherRelationship, 'r as result', match)
end

- (Object) remove_label(*target_labels)



120
121
122
123
# File 'lib/neo4j-server/cypher_node.rb', line 120

def remove_label(*target_labels)
  @session._query_or_fail("#{match_start} REMOVE n #{_cypher_label_list(target_labels)}", false, neo_id: neo_id)
  target_labels.each { |label| labels.delete(label) } unless labels.nil?
end

- (Object) remove_properties(properties)



79
80
81
82
83
84
85
# File 'lib/neo4j-server/cypher_node.rb', line 79

def remove_properties(properties)
  refresh
  q = "#{match_start} REMOVE " + properties.map do |k|
    "n.`#{k}`"
  end.join(', ')
  @session._query_or_fail(q, false, neo_id: neo_id)
end

- (Object) remove_property(key)

Directly remove the property on the node (low level method, may need transaction)



60
61
62
63
# File 'lib/neo4j-server/cypher_node.rb', line 60

def remove_property(key)
  refresh
  @session._query_or_fail("#{match_start} REMOVE n.`#{key}`", false,  neo_id: neo_id)
end

- (Object) set_label(*label_names)



125
126
127
128
# File 'lib/neo4j-server/cypher_node.rb', line 125

def set_label(*label_names)
  q = "#{match_start} #{remove_labels_if_needed} #{set_labels_if_needed(label_names)}"
  @session._query_or_fail(q, false, neo_id: neo_id)
end

- (Object) set_property(key, value)

Directly set the property on the node (low level method, may need transaction)

Parameters:

  • key (Symbol, String)
  • value

    see Neo4j::PropertyValidator::VALID_PROPERTY_VALUE_CLASSES for valid values



66
67
68
69
70
# File 'lib/neo4j-server/cypher_node.rb', line 66

def set_property(key, value)
  refresh
  @session._query_or_fail("#{match_start} SET n.`#{key}` = { value }", false,  value: value, neo_id: neo_id)
  value
end

- (Object) update_props(properties)

Updates the properties, keeps old properties

Parameters:

  • properties (Hash<Symbol, Object>)

    hash of properties that should be updated on the node



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/neo4j-server/cypher_node.rb', line 88

def update_props(properties)
  refresh
  return if properties.empty?

  removed_keys = properties.keys.select { |k| properties[k].nil? }
  remove_properties(removed_keys) unless removed_keys.empty?
  properties_to_set = properties.keys - removed_keys
  return if properties_to_set.empty?
  props_list = cypher_prop_list(properties)[:props].merge(neo_id: neo_id)
  @session._query_or_fail("#{match_start} SET #{cypher_properties(properties_to_set)}", false, props_list)
  properties
end