Class: Neo4j::Server::CypherRelationship

Inherits:
Relationship show all
Includes:
Core::ActiveEntity, Core::CypherTranslator, Resource
Defined in:
lib/neo4j-server/cypher_relationship.rb

Constant Summary

Constants included from Core::CypherTranslator

Core::CypherTranslator::SANITIZE_ESCAPED_REGEXP

Constants included from PropertyValidator

PropertyValidator::VALID_PROPERTY_VALUE_CLASSES

Instance Attribute Summary

Attributes included from Resource

#resource_data, #resource_url

Instance Method Summary collapse

Methods included from Core::ActiveEntity

#persisted?

Methods included from Core::CypherTranslator

#cypher_prop_list, #escape_quotes, #escape_value, #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 Relationship

_load, #_other_node, create, #end_node, load, #other_node, #start_node

Methods included from Relationship::Wrapper

#neo4j_obj, #wrapper

Methods included from PropertyContainer

#[], #[]=

Methods included from PropertyValidator

#valid_property?, #validate_property

Constructor Details

#initialize(session, value) ⇒ CypherRelationship

Returns a new instance of CypherRelationship.



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

def initialize(session, value)
  @session = session
  @response_hash = value
  @rel_type = @response_hash['type']
  @props = @response_hash['data']
  @start_node_neo_id = @response_hash['start'].match(/\d+$/)[0].to_i
  @end_node_neo_id = @response_hash['end'].match(/\d+$/)[0].to_i
  @id = @response_hash['id']
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



18
19
20
# File 'lib/neo4j-server/cypher_relationship.rb', line 18

def ==(o)
  o.class == self.class && o.neo_id == neo_id
end

#_end_nodeObject



61
62
63
64
# File 'lib/neo4j-server/cypher_relationship.rb', line 61

def _end_node
  load_resource
  @_end_node ||= Neo4j::Node._load(end_node_neo_id)
end

#_end_node_idObject



53
54
55
# File 'lib/neo4j-server/cypher_relationship.rb', line 53

def _end_node_id
  @end_node_neo_id ||= get_node_id(:end)
end

#_start_nodeObject



57
58
59
# File 'lib/neo4j-server/cypher_relationship.rb', line 57

def _start_node
  @_start_node ||= Neo4j::Node._load(start_node_neo_id)
end

#_start_node_idObject



49
50
51
# File 'lib/neo4j-server/cypher_relationship.rb', line 49

def _start_node_id
  @start_node_neo_id ||= get_node_id(:start)
end

#delObject Also known as: delete, destroy



113
114
115
116
# File 'lib/neo4j-server/cypher_relationship.rb', line 113

def del
  id = neo_id
  @session._query("START n=relationship(#{id}) DELETE n").raise_unless_response_code(200)
end

#end_node_neo_idObject



45
46
47
# File 'lib/neo4j-server/cypher_relationship.rb', line 45

def end_node_neo_id
  @end_node_neo_id
end

#exist?Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/neo4j-server/cypher_relationship.rb', line 120

def exist?
  id = neo_id
  response = @session._query("START n=relationship(#{id}) RETURN n")

  if (!response.error?)
    return true
  elsif (response.error_status == 'BadInputException') # TODO see github issue neo4j/1061
    return false
  else
    response.raise_error
  end
end

#get_node_id(direction) ⇒ Object



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

def get_node_id(direction)
  load_resource
  resource_url_id(resource_url(direction))
end

#get_property(key) ⇒ Object



71
72
73
# File 'lib/neo4j-server/cypher_relationship.rb', line 71

def get_property(key)
  @session._query_or_fail("START n=relationship(#{id}) RETURN n.`#{key}`", true)
end

#idObject



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

def id
  @id
end

#inspectObject



31
32
33
# File 'lib/neo4j-server/cypher_relationship.rb', line 31

def inspect
  "CypherRelationship #{neo_id}"
end

#load_resourceObject



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

def load_resource
  if resource_data.nil? || resource_data.empty?
    @resource_data = @session._query_or_fail("START n=relationship(#{id}) RETURN n", true) # r.first_data
  end
end

#neo_idObject



27
28
29
# File 'lib/neo4j-server/cypher_relationship.rb', line 27

def neo_id
  id
end

#propsHash<Symbol,Object>

Returns all properties of the relationship.

Returns:

  • (Hash<Symbol,Object>)

    all properties of the relationship



84
85
86
87
88
89
90
91
# File 'lib/neo4j-server/cypher_relationship.rb', line 84

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

#props=(properties) ⇒ Object

replace all properties with new properties

Parameters:

  • properties (Hash)

    a hash of properties the relationship should have



94
95
96
97
# File 'lib/neo4j-server/cypher_relationship.rb', line 94

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

#rel_typeObject



109
110
111
# File 'lib/neo4j-server/cypher_relationship.rb', line 109

def rel_type
  @rel_type.to_sym
end

#remove_property(key) ⇒ Object



79
80
81
# File 'lib/neo4j-server/cypher_relationship.rb', line 79

def remove_property(key)
  @session._query_or_fail("START n=relationship(#{id}) REMOVE n.`#{key}`")
end

#set_property(key, value) ⇒ Object



75
76
77
# File 'lib/neo4j-server/cypher_relationship.rb', line 75

def set_property(key,value)
  @session._query_or_fail("START n=relationship(#{id}) SET n.`#{key}` = {value}", false, {value: value})
end

#start_node_neo_idObject



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

def start_node_neo_id
  @start_node_neo_id
end

#update_props(properties) ⇒ Object

Updates the properties, keeps old properties

Parameters:

  • properties (Hash<Symbol,Object>)

    hash of properties that should be updated on the relationship



100
101
102
103
104
105
106
107
# File 'lib/neo4j-server/cypher_relationship.rb', line 100

def update_props(properties)
  return if properties.empty?
  q = "START n=relationship(#{neo_id}) SET " + properties.keys.map do |k|
    "n.`#{k}`= #{escape_value(properties[k])}"
  end.join(',')
  @session._query_or_fail(q)
  properties
end