Class: Neo4j::Server::CypherNode
- Includes:
- Core::ActiveEntity, Core::CypherTranslator, Resource
- Defined in:
- lib/neo4j-server/cypher_node.rb
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
Attributes included from Resource
Instance Method Summary collapse
- #_cypher_label_list(labels) ⇒ Object
-
#_java_node ⇒ Object
TODO, needed by neo4j-cypher.
- #_map_result(r) ⇒ Object
- #add_label(*labels) ⇒ Object
-
#create_rel(type, other_node, props = nil) ⇒ Object
Creates a relationship of given type to other_node with optionally properties.
-
#del ⇒ Object
(also: #delete, #destroy)
Deletes this node from the database.
-
#exist? ⇒ Boolean
True if the node exists.
-
#get_property(key) ⇒ Object
Directly get the property on the node (low level method, may need transaction).
-
#initialize(session, value) ⇒ CypherNode
constructor
A new instance of CypherNode.
- #inspect ⇒ Object
-
#labels ⇒ Object
All the Neo4j labels for this node.
- #match(clazz, returns, match = {}) ⇒ Object
- #neo_id ⇒ Object
-
#node(match = {}) ⇒ Object
Returns the only node of a given type and direction that is attached to this node, or nil.
-
#nodes(match = {}) ⇒ Enumerable<Neo4j::Node>
abstract
Works like #rels method but instead returns the nodes.
-
#props ⇒ Hash<Symbol, Object>
All properties of the node.
-
#props=(properties) ⇒ Object
replace all properties with new properties.
- #refresh ⇒ Object
-
#rel(match = {}) ⇒ Object
Same as #node but returns the relationship.
-
#rel?(match = {}) ⇒ Boolean
Returns true or false if there is one or more relationships.
- #rel_string(type, other_node, props) ⇒ Object
-
#rels(match = {dir: :both}) ⇒ Enumerable<Neo4j::Relationship>
Returns an enumeration of relationships.
- #remove_label(*labels) ⇒ Object
- #remove_properties(properties) ⇒ Object
-
#remove_property(key) ⇒ Object
Directly remove the property on the node (low level method, may need transaction).
- #set_label(*label_names) ⇒ Object
-
#set_property(key, value) ⇒ Object
Directly set the property on the node (low level method, may need transaction).
-
#update_props(properties) ⇒ Object
Updates the properties, keeps old properties.
Methods included from Core::ActiveEntity
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
Methods included from EntityEquality
Constructor Details
#initialize(session, value) ⇒ CypherNode
Returns a new instance of CypherNode.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/neo4j-server/cypher_node.rb', line 7 def initialize(session, value) @session = session @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 Method Details
#_cypher_label_list(labels) ⇒ Object
117 118 119 |
# File 'lib/neo4j-server/cypher_node.rb', line 117 def _cypher_label_list(labels) ':' + labels.map{|label| "`#{label}`"}.join(':') end |
#_java_node ⇒ Object
TODO, needed by neo4j-cypher
29 30 31 |
# File 'lib/neo4j-server/cypher_node.rb', line 29 def _java_node self end |
#_map_result(r) ⇒ Object
189 190 191 |
# File 'lib/neo4j-server/cypher_node.rb', line 189 def _map_result(r) r.to_node_enumeration.map { |rel| rel.result } end |
#add_label(*labels) ⇒ Object
121 122 123 |
# File 'lib/neo4j-server/cypher_node.rb', line 121 def add_label(*labels) @session._query_or_fail("#{match_start} SET n #{_cypher_label_list(labels)}") end |
#create_rel(type, other_node, props = nil) ⇒ Object
Creates a relationship of given type to other_node with optionally properties
34 35 36 37 38 |
# File 'lib/neo4j-server/cypher_node.rb', line 34 def create_rel(type, other_node, props = nil) id = @session._query_or_fail(rel_string(type, other_node, props), true, cypher_prop_list(props)) data_hash = { 'type' => type, 'data' => props, 'start' => self.neo_id.to_s, 'end' => other_node.neo_id.to_s, 'id' => id } CypherRelationship.new(@session, data_hash) end |
#del ⇒ Object Also known as: delete, destroy
Deletes this node from the database
135 136 137 138 |
# File 'lib/neo4j-server/cypher_node.rb', line 135 def del @session._query_or_fail("#{match_start} MATCH n-[r]-() DELETE r") @session._query_or_fail("#{match_start} DELETE n") end |
#exist? ⇒ Boolean
Returns true if the node exists.
144 145 146 |
# File 'lib/neo4j-server/cypher_node.rb', line 144 def exist? @session._query("#{match_start} RETURN ID(n)").data.empty? ? false : true end |
#get_property(key) ⇒ Object
Directly get the property on the node (low level method, may need transaction)
103 104 105 106 107 108 109 |
# File 'lib/neo4j-server/cypher_node.rb', line 103 def get_property(key) if @props @props[key.to_sym] else @session._query_or_fail("#{match_start} RETURN n.`#{key}`", true) end end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/neo4j-server/cypher_node.rb', line 24 def inspect "CypherNode #{neo_id} (#{object_id})" end |
#labels ⇒ Object
Returns all the Neo4j labels for this node.
112 113 114 115 |
# File 'lib/neo4j-server/cypher_node.rb', line 112 def labels @labels ||= @session._query_or_fail("#{match_start} RETURN labels(n) as labels", true) @labels.map(&:to_sym) end |
#match(clazz, returns, match = {}) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/neo4j-server/cypher_node.rb', line 175 def match(clazz, returns, match={}) to_dir = {outgoing: ->(rel) {"-#{rel}->"}, incoming: ->(rel) {"<-#{rel}-"}, both: ->(rel) {"-#{rel}-"} } cypher_rel = match[:type] ? "[r:`#{match[:type]}`]" : '[r]' between_id = match[:between] ? "MATCH (p) WHERE ID(p) = #{match[:between].neo_id}" : "" dir_func = to_dir[match[:dir] || :both] cypher = "#{match_start} #{between_id} MATCH (n)#{dir_func.call(cypher_rel)}(p) RETURN #{returns}" r = @session._query(cypher) r.raise_error if r.error? _map_result(r) end |
#neo_id ⇒ Object
20 21 22 |
# File 'lib/neo4j-server/cypher_node.rb', line 20 def neo_id @id end |
#node(match = {}) ⇒ Object
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.”
149 150 151 |
# File 'lib/neo4j-server/cypher_node.rb', line 149 def node(match={}) ensure_single_relationship { match(CypherNode, "p as result LIMIT 2", match) } end |
#nodes(match = {}) ⇒ Enumerable<Neo4j::Node>
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
165 166 167 |
# File 'lib/neo4j-server/cypher_node.rb', line 165 def nodes(match={}) match(CypherNode, "p as result", match) end |
#props ⇒ Hash<Symbol, Object>
Returns all properties of the node.
45 46 47 48 49 50 51 52 |
# File 'lib/neo4j-server/cypher_node.rb', line 45 def props if @props @props else hash = @session._query_entity_data("#{match_start} RETURN n") @props = Hash[hash['data'].map{ |k, v| [k.to_sym, v] }] end end |
#props=(properties) ⇒ Object
replace all properties with new properties
72 73 74 75 76 |
# File 'lib/neo4j-server/cypher_node.rb', line 72 def props=(properties) refresh @session._query_or_fail("#{match_start} SET n = { props }", false, {props: properties}) properties end |
#refresh ⇒ Object
54 55 56 |
# File 'lib/neo4j-server/cypher_node.rb', line 54 def refresh @props = nil end |
#rel(match = {}) ⇒ Object
Same as #node but returns the relationship. Notice it may raise an exception if there are more then one relationship matching.
154 155 156 |
# File 'lib/neo4j-server/cypher_node.rb', line 154 def rel(match={}) ensure_single_relationship { match(CypherRelationship, "r as result LIMIT 2", match) } end |
#rel?(match = {}) ⇒ Boolean
Returns true or false if there is one or more relationships
159 160 161 162 |
# File 'lib/neo4j-server/cypher_node.rb', line 159 def rel?(match={}) result = match(CypherRelationship, "r as result", match) !!result.first end |
#rel_string(type, other_node, props) ⇒ Object
40 41 42 |
# File 'lib/neo4j-server/cypher_node.rb', line 40 def rel_string(type, other_node, props) "MATCH (a), (b) WHERE ID(a) = #{neo_id} AND ID(b) = #{other_node.neo_id} CREATE (a)-[r:`#{type}` #{prop_identifier(props)}]->(b) RETURN ID(r)" end |
#rels(match = {dir: :both}) ⇒ Enumerable<Neo4j::Relationship>
Returns an enumeration of relationships. It always returns relationships of depth one.
170 171 172 |
# File 'lib/neo4j-server/cypher_node.rb', line 170 def rels(match = {dir: :both}) match(CypherRelationship, "r as result", match) end |
#remove_label(*labels) ⇒ Object
125 126 127 |
# File 'lib/neo4j-server/cypher_node.rb', line 125 def remove_label(*labels) @session._query_or_fail("#{match_start} REMOVE n #{_cypher_label_list(labels)}") end |
#remove_properties(properties) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/neo4j-server/cypher_node.rb', line 78 def remove_properties(properties) refresh q = "#{match_start} REMOVE " + properties.map do |k| "n.`#{k}`" end.join(', ') @session._query_or_fail(q) end |
#remove_property(key) ⇒ Object
Directly remove the property on the node (low level method, may need transaction)
59 60 61 62 |
# File 'lib/neo4j-server/cypher_node.rb', line 59 def remove_property(key) refresh @session._query_or_fail("#{match_start} REMOVE n.`#{key}`") end |
#set_label(*label_names) ⇒ Object
129 130 131 132 |
# File 'lib/neo4j-server/cypher_node.rb', line 129 def set_label(*label_names) q = "#{match_start} #{remove_labels_if_needed} #{set_labels_if_needed(label_names)}" @session._query_or_fail(q) end |
#set_property(key, value) ⇒ Object
Directly set the property on the node (low level method, may need transaction)
65 66 67 68 69 |
# File 'lib/neo4j-server/cypher_node.rb', line 65 def set_property(key,value) refresh @session._query_or_fail("#{match_start} SET n.`#{key}` = { value }", false, value: value) value end |
#update_props(properties) ⇒ Object
Updates the properties, keeps old properties
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/neo4j-server/cypher_node.rb', line 87 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? q = "#{match_start} SET " + properties_to_set.map do |k| "n.`#{k}`= #{escape_value(properties[k])}" end.join(',') @session._query_or_fail(q) properties end |