Class: Neo4j::Server::CypherRelationship

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

Constant Summary collapse

MARSHAL_INSTANCE_VARIABLES =
[:@rel_type, :@props, :@start_node_neo_id, :@end_node_neo_id, :@id]

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 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 EntityMarshal

#marshal_dump, #marshal_load

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.



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

def initialize(session, value)
  @session = session
  @response_hash = value
  @rel_type = @response_hash[:type]
  @props = @response_hash[:data]
  @start_node_neo_id = neo_id_integer(@response_hash[:start])
  @end_node_neo_id = neo_id_integer(@response_hash[:end])
  @id = @response_hash[:id]
end

Instance Attribute Details

#end_node_neo_idObject (readonly)

Returns the value of attribute end_node_neo_id.



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

def end_node_neo_id
  @end_node_neo_id
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#start_node_neo_idObject (readonly)

Returns the value of attribute start_node_neo_id.



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

def start_node_neo_id
  @start_node_neo_id
end

Instance Method Details

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



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

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

#_end_nodeObject



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

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

#_end_node_idObject



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

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

#_start_nodeObject



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

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

#_start_node_idObject



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

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

#delObject Also known as: delete, destroy



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

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

#exist?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/neo4j-server/cypher_relationship.rb', line 121

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



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

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

#get_property(key) ⇒ Object



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

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

#inspectObject



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

def inspect
  "CypherRelationship #{neo_id}"
end

#load_resourceObject



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

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



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

def neo_id
  id
end

#propsHash<Symbol,Object>

Returns all properties of the relationship.

Returns:

  • (Hash<Symbol,Object>)

    all properties of the relationship



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

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, v] }]
  end
end

#props=(properties) ⇒ Object

replace all properties with new properties

Parameters:

  • properties (Hash)

    a hash of properties the relationship should have



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

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

#rel_typeObject



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

def rel_type
  @rel_type.to_sym
end

#remove_property(key) ⇒ Object



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

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

#set_property(key, value) ⇒ Object



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

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/neo4j-server/cypher_relationship.rb', line 95

def update_props(properties)
  return if properties.empty?

  params = {}
  q = "#{match_start} SET " + properties.keys.each_with_index.map do |k, _i|
    param = k.to_s.tr_s('^a-zA-Z0-9', '_').gsub(/^_+|_+$/, '')
    params[param] = properties[k]

    "n.`#{k}`= {#{param}}"
  end.join(',')

  @session._query_or_fail(q, false, params.merge(neo_id: neo_id))

  properties
end