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::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 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'].is_a?(Integer) ? @response_hash['start'] : @response_hash['start'].match(/\d+$/)[0].to_i
  @end_node_neo_id = @response_hash['end'].is_a?(Integer) ? @response_hash['end'] : @response_hash['end'].match(/\d+$/)[0].to_i
  @id = @response_hash['id']
end

Instance Attribute Details

#end_node_neo_idObject (readonly)

Returns the value of attribute end_node_neo_id.



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

def end_node_neo_id
  @end_node_neo_id
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#start_node_neo_idObject (readonly)

Returns the value of attribute start_node_neo_id.



39
40
41
# File 'lib/neo4j-server/cypher_relationship.rb', line 39

def start_node_neo_id
  @start_node_neo_id
end

Instance Method Details

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



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

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

#_end_nodeObject



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

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

#_end_node_idObject



47
48
49
# File 'lib/neo4j-server/cypher_relationship.rb', line 47

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

#_start_nodeObject



51
52
53
# File 'lib/neo4j-server/cypher_relationship.rb', line 51

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

#_start_node_idObject



43
44
45
# File 'lib/neo4j-server/cypher_relationship.rb', line 43

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

#delObject Also known as: delete, destroy



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

def del
  @session._query("#{match_start} DELETE n", neo_id: neo_id)
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  response = @session._query("#{match_start} RETURN n", neo_id: neo_id)
  # binding.pry
  (response.data.nil? || response.data.empty?) ? false : true
end

#get_node_id(direction) ⇒ Object



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

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

#get_property(key) ⇒ Object



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

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

#inspectObject



29
30
31
# File 'lib/neo4j-server/cypher_relationship.rb', line 29

def inspect
  "CypherRelationship #{neo_id}"
end

#load_resourceObject



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

def load_resource
  return if resource_data_present?

  @resource_data = @session._query_or_fail("#{match_start} RETURN n", true, neo_id: neo_id) # r.first_data
end

#neo_idObject



25
26
27
# File 'lib/neo4j-server/cypher_relationship.rb', line 25

def neo_id
  id
end

#propsHash<Symbol,Object>

Returns all properties of the relationship.

Returns:

  • (Hash<Symbol,Object>)

    all properties of the relationship



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

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

#props=(properties) ⇒ Object

replace all properties with new properties

Parameters:

  • properties (Hash)

    a hash of properties the relationship should have



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

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

#rel_typeObject



103
104
105
# File 'lib/neo4j-server/cypher_relationship.rb', line 103

def rel_type
  @rel_type.to_sym
end

#remove_property(key) ⇒ Object



73
74
75
# File 'lib/neo4j-server/cypher_relationship.rb', line 73

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

#set_property(key, value) ⇒ Object



69
70
71
# File 'lib/neo4j-server/cypher_relationship.rb', line 69

def set_property(key, value)
  @session._query_or_fail("#{match_start} SET n.`#{key}` = {value}", false,  value: value, neo_id: 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



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

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