/
relationship.rb
160 lines (132 loc) · 3.78 KB
/
relationship.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module Neo4j
# A module that can be mixed in like a Neo4j::NodeMixin
# It wraps the Neo4j Relationship class.
#
module RelationshipMixin
extend TransactionalMixin
# Initialize the Relationship object with specified java org.neo4j.api.core.Relationship object
# Expects at least one parameter.
#
# ==== Parameters
# param1<org.neo4j.api.core.Relationship>:: the internal java relationship object
#
# :api: public
def initialize(*args)
init_with_rel(args[0])
# must call super with no arguments so that chaining of initialize method will work
super()
end
# Inits this node with the specified java neo node
#
# :api: private
def init_with_rel(node)
@internal_r = node
self.classname = self.class.to_s unless @internal_r.hasProperty("classname")
$NEO_LOGGER.debug {"loading relationship '#{self.class.to_s}' id #{@internal_r.getId()}"}
end
# :api: public
def end_node
id = @internal_r.getEndNode.getId
Neo4j.instance.find_node id
end
# :api: public
def start_node
id = @internal_r.getStartNode.getId
Neo4j.instance.find_node id
end
# :api: public
def other_node(node)
neo_node = node
neo_node = node.internal_node if node.respond_to?(:internal_node)
id = @internal_r.getOtherNode(neo_node).getId
Neo4j.instance.find_node id
end
# Returns the neo relationship type that this relationship is used in.
# (see java API org.neo4j.api.core.Relationship#getType and org.neo4j.api.core.RelationshipType)
#
# ==== Returns
# Symbol
#
# :api: public
def relationship_type
@internal_r.get_type.name.to_sym
end
# Deletes the relationship between two nodes.
# Will fire a RelationshipDeletedEvent on the start_node class.
#
# :api: public
def delete
type = @internal_r.getType().name()
@internal_r.delete
# TODO not sure if we need to do it on both start and end node ...
# start_node.class.indexer.on_relationship_deleted(start_node, type) unless start_node.nil?
end_node.class.indexer.on_relationship_deleted(end_node, type) unless end_node.nil?
end
def set_property(key, value)
@internal_r.setProperty(key, value)
end
def property?(key)
@internal_r.hasProperty(key)
end
def get_property(key)
return nil unless self.property?(key)
@internal_r.getProperty(key)
end
# Returns a hash of all properties.
#
# ==== Returns
# Hash:: property key and property value
#
# :api: public
def props
ret = {"id" => neo_relationship_id}
iter = @internal_r.getPropertyKeys.iterator
while (iter.hasNext) do
key = iter.next
ret[key] = @internal_r.getProperty(key)
end
ret
end
# Returns the given property
#
# :api: public
def [](name)
get_property(name.to_s)
end
# Sets the given property to a given value
#
# :api: public
def []=(name, value)
set_property(name.to_s, value)
end
def classname
get_property('classname')
end
def classname=(value)
set_property('classname', value)
end
def neo_relationship_id
@internal_r.getId()
end
transactional :initialize, :property?, :set_property, :get_property, :delete
#
# Adds classmethods in the ClassMethods module
#
def self.included(c)
c.extend ClassMethods
end
module ClassMethods
def property(*props)
props.each do |prop|
define_method(prop) do
get_property(prop.to_s)
end
name = (prop.to_s() +"=")
define_method(name) do |value|
set_property(prop.to_s, value)
end
end
end
end
end
end