neo4j 1.0.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +141 -0
- data/CONTRIBUTORS +15 -0
- data/Gemfile +3 -0
- data/README.rdoc +2015 -0
- data/lib/neo4j.old/batch_inserter.rb +144 -0
- data/lib/neo4j.old/config.rb +138 -0
- data/lib/neo4j.old/event_handler.rb +73 -0
- data/lib/neo4j.old/extensions/activemodel.rb +158 -0
- data/lib/neo4j.old/extensions/aggregate.rb +12 -0
- data/lib/neo4j.old/extensions/aggregate/aggregate_enum.rb +40 -0
- data/lib/neo4j.old/extensions/aggregate/ext/node_mixin.rb +69 -0
- data/lib/neo4j.old/extensions/aggregate/node_aggregate.rb +8 -0
- data/lib/neo4j.old/extensions/aggregate/node_aggregate_mixin.rb +331 -0
- data/lib/neo4j.old/extensions/aggregate/node_aggregator.rb +216 -0
- data/lib/neo4j.old/extensions/aggregate/node_group.rb +43 -0
- data/lib/neo4j.old/extensions/aggregate/prop_group.rb +30 -0
- data/lib/neo4j.old/extensions/aggregate/property_enum.rb +24 -0
- data/lib/neo4j.old/extensions/aggregate/props_aggregate.rb +8 -0
- data/lib/neo4j.old/extensions/aggregate/props_aggregate_mixin.rb +31 -0
- data/lib/neo4j.old/extensions/aggregate/props_aggregator.rb +80 -0
- data/lib/neo4j.old/extensions/find_path.rb +117 -0
- data/lib/neo4j.old/extensions/graph_algo.rb +1 -0
- data/lib/neo4j.old/extensions/graph_algo/all_simple_paths.rb +133 -0
- data/lib/neo4j.old/extensions/graph_algo/neo4j-graph-algo-0.3.jar +0 -0
- data/lib/neo4j.old/extensions/reindexer.rb +104 -0
- data/lib/neo4j.old/extensions/rest.rb +21 -0
- data/lib/neo4j.old/extensions/rest/rest.rb +336 -0
- data/lib/neo4j.old/extensions/rest/rest_mixin.rb +193 -0
- data/lib/neo4j.old/extensions/rest/server.rb +50 -0
- data/lib/neo4j.old/extensions/rest/stubs.rb +141 -0
- data/lib/neo4j.old/extensions/rest_master.rb +34 -0
- data/lib/neo4j.old/extensions/rest_slave.rb +31 -0
- data/lib/neo4j.old/extensions/tx_tracker.rb +392 -0
- data/lib/neo4j.old/indexer.rb +187 -0
- data/lib/neo4j.old/jars.rb +6 -0
- data/lib/neo4j.old/jars/geronimo-jta_1.1_spec-1.1.1.jar +0 -0
- data/lib/neo4j.old/jars/neo4j-kernel-1.0.jar +0 -0
- data/lib/neo4j.old/mixins/java_list_mixin.rb +139 -0
- data/lib/neo4j.old/mixins/java_node_mixin.rb +205 -0
- data/lib/neo4j.old/mixins/java_property_mixin.rb +169 -0
- data/lib/neo4j.old/mixins/java_relationship_mixin.rb +60 -0
- data/lib/neo4j.old/mixins/migration_mixin.rb +157 -0
- data/lib/neo4j.old/mixins/node_mixin.rb +249 -0
- data/lib/neo4j.old/mixins/property_class_methods.rb +265 -0
- data/lib/neo4j.old/mixins/rel_class_methods.rb +167 -0
- data/lib/neo4j.old/mixins/relationship_mixin.rb +103 -0
- data/lib/neo4j.old/neo.rb +247 -0
- data/lib/neo4j.old/node.rb +49 -0
- data/lib/neo4j.old/reference_node.rb +15 -0
- data/lib/neo4j.old/relationship.rb +85 -0
- data/lib/neo4j.old/relationships/decl_relationship_dsl.rb +164 -0
- data/lib/neo4j.old/relationships/has_list.rb +101 -0
- data/lib/neo4j.old/relationships/has_n.rb +129 -0
- data/lib/neo4j.old/relationships/node_traverser.rb +138 -0
- data/lib/neo4j.old/relationships/relationship_dsl.rb +149 -0
- data/lib/neo4j.old/relationships/traversal_position.rb +50 -0
- data/lib/neo4j.old/relationships/wrappers.rb +51 -0
- data/lib/neo4j.old/search_result.rb +72 -0
- data/lib/neo4j.old/transaction.rb +254 -0
- data/lib/neo4j.old/version.rb +3 -0
- data/lib/neo4j.rb +50 -0
- data/lib/neo4j/config.rb +137 -0
- data/lib/neo4j/database.rb +43 -0
- data/lib/neo4j/equal.rb +22 -0
- data/lib/neo4j/event_handler.rb +91 -0
- data/lib/neo4j/index.rb +157 -0
- data/lib/neo4j/jars/geronimo-jta_1.1_spec-1.1.1.jar +0 -0
- data/lib/neo4j/jars/lucene-core-2.9.2.jar +0 -0
- data/lib/neo4j/jars/lucene-core-3.0.1.jar +0 -0
- data/lib/neo4j/jars/neo4j-index-1.1.jar +0 -0
- data/lib/neo4j/jars/neo4j-kernel-1.1.1.jar +0 -0
- data/lib/neo4j/jars/neo4j-kernel-1.1.jar +0 -0
- data/lib/neo4j/jars/neo4j-lucene-index-0.1-20100916.085626-67.jar +0 -0
- data/lib/neo4j/mapping/class_methods/index.rb +21 -0
- data/lib/neo4j/mapping/class_methods/property.rb +139 -0
- data/lib/neo4j/mapping/class_methods/relationship.rb +96 -0
- data/lib/neo4j/mapping/class_methods/rule.rb +135 -0
- data/lib/neo4j/mapping/decl_relationship_dsl.rb +151 -0
- data/lib/neo4j/mapping/has_n.rb +117 -0
- data/lib/neo4j/mapping/node_mixin.rb +70 -0
- data/lib/neo4j/neo4j.rb +65 -0
- data/lib/neo4j/node.rb +82 -0
- data/lib/neo4j/node_mixin.rb +4 -0
- data/lib/neo4j/node_relationship.rb +60 -0
- data/lib/neo4j/node_traverser.rb +141 -0
- data/lib/neo4j/property.rb +72 -0
- data/lib/neo4j/rails/lucene_connection_closer.rb +19 -0
- data/lib/neo4j/rails/model.rb +210 -0
- data/lib/neo4j/rails/railtie.rb +16 -0
- data/lib/neo4j/rails/transaction.rb +29 -0
- data/lib/neo4j/rails/value.rb +43 -0
- data/lib/neo4j/relationship.rb +88 -0
- data/lib/neo4j/relationship_traverser.rb +57 -0
- data/lib/neo4j/to_java.rb +17 -0
- data/lib/neo4j/transaction.rb +69 -0
- data/lib/neo4j/version.rb +3 -0
- data/neo4j.gemspec +30 -0
- metadata +243 -0
@@ -0,0 +1,265 @@
|
|
1
|
+
module Neo4j::PropertyClassMethods
|
2
|
+
|
3
|
+
#
|
4
|
+
# Access to class constants.
|
5
|
+
# These properties are shared by the class and its siblings.
|
6
|
+
# For example that means that we can specify properties for a parent
|
7
|
+
# class and the child classes will 'inherit' those properties.
|
8
|
+
#
|
9
|
+
|
10
|
+
def root_class # :nodoc:
|
11
|
+
self::ROOT_CLASS
|
12
|
+
end
|
13
|
+
|
14
|
+
def properties_info # :nodoc:
|
15
|
+
self::PROPERTIES_INFO
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
# ------------------------------------------------------------------------
|
20
|
+
|
21
|
+
|
22
|
+
# Generates accessor method and sets configuration for Neo4j node properties.
|
23
|
+
# The generated accessor is a simple wrapper around the #set_property and
|
24
|
+
# #get_property methods.
|
25
|
+
#
|
26
|
+
# If a property is set to nil the property will be removed.
|
27
|
+
#
|
28
|
+
# ==== Configuration
|
29
|
+
# By setting the :type configuration parameter to 'Object' makes
|
30
|
+
# it possible to marshal any ruby object.
|
31
|
+
#
|
32
|
+
# If no type is provided the only the native Neo4j property types are allowed:
|
33
|
+
# * TrueClass, FalseClass
|
34
|
+
# * String
|
35
|
+
# * Fixnum
|
36
|
+
# * Float
|
37
|
+
# * Boolean
|
38
|
+
#
|
39
|
+
# ==== Parameters
|
40
|
+
# props<Array,Hash>:: a variable length arguments or a hash, see example below
|
41
|
+
#
|
42
|
+
# ==== Example
|
43
|
+
# class Baaz; end
|
44
|
+
#
|
45
|
+
# class Foo
|
46
|
+
# include Neo4j::NodeMixin
|
47
|
+
# property :name, :city # can set several properties in one go
|
48
|
+
# property :bar, :type => Object # allow serialization of any ruby object
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# f = Foo.new
|
52
|
+
# f.bar = Baaz.new
|
53
|
+
#
|
54
|
+
def property(*props)
|
55
|
+
if props.size == 2 and props[1].kind_of?(Hash)
|
56
|
+
props[1].each_pair do |key, value|
|
57
|
+
pname = props[0].to_sym
|
58
|
+
properties_info[pname] ||= {}
|
59
|
+
properties_info[pname][key] = value
|
60
|
+
end
|
61
|
+
props = props[0..0]
|
62
|
+
end
|
63
|
+
|
64
|
+
props.each do |prop|
|
65
|
+
pname = prop.to_sym
|
66
|
+
properties_info[pname] ||= {}
|
67
|
+
properties_info[pname][:defined] = true
|
68
|
+
|
69
|
+
define_method(pname) do
|
70
|
+
self[pname]
|
71
|
+
end
|
72
|
+
|
73
|
+
name = (pname.to_s() +"=").to_sym
|
74
|
+
define_method(name) do |value|
|
75
|
+
self[pname] = value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns true if the given property name should be marshalled.
|
81
|
+
# All properties that has a type will be marshalled.
|
82
|
+
#
|
83
|
+
# ===== Example
|
84
|
+
# class Foo
|
85
|
+
# include Neo4j::NodeMixin
|
86
|
+
# property :name
|
87
|
+
# property :since, :type => Date
|
88
|
+
# end
|
89
|
+
# Foo.marshal?(:since) => true
|
90
|
+
# Foo.marshal?(:name) => false
|
91
|
+
#
|
92
|
+
# ==== Returns
|
93
|
+
# true if the property will be marshalled, false otherwise
|
94
|
+
#
|
95
|
+
def marshal?(prop_name)
|
96
|
+
return false if properties_info[prop_name.to_sym].nil?
|
97
|
+
return false if properties_info[prop_name.to_sym][:type].nil?
|
98
|
+
return true
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
# Returns true if the given property name has been defined with the class
|
103
|
+
# method property or properties.
|
104
|
+
#
|
105
|
+
# Notice that the node may have properties that has not been declared.
|
106
|
+
# It is always possible to set an undeclared property on a node.
|
107
|
+
#
|
108
|
+
# ==== Returns
|
109
|
+
# true or false
|
110
|
+
#
|
111
|
+
def property?(prop_name)
|
112
|
+
return false if properties_info[prop_name.to_sym].nil?
|
113
|
+
properties_info[prop_name.to_sym][:defined] == true
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
# Creates a struct class containig all properties of this class.
|
118
|
+
#
|
119
|
+
# ==== Example
|
120
|
+
#
|
121
|
+
# h = Person.value_object.new
|
122
|
+
# h.name # => nil
|
123
|
+
# h.name='kalle'
|
124
|
+
# h[:name] # => 'kalle'
|
125
|
+
#
|
126
|
+
# ==== Returns
|
127
|
+
# Struct
|
128
|
+
#
|
129
|
+
def value_object
|
130
|
+
@value_class ||= create_value_class
|
131
|
+
end
|
132
|
+
|
133
|
+
# Index a property or a relationship.
|
134
|
+
# If the rel_prop arg contains a '.' then it will index the relationship.
|
135
|
+
# For example "friends.name" will index each node with property name in the relationship friends.
|
136
|
+
# For example "name" will index the name property of this NodeMixin class.
|
137
|
+
#
|
138
|
+
# ==== Example
|
139
|
+
# class Person
|
140
|
+
# include Neo4j::NodeMixin
|
141
|
+
# property :name
|
142
|
+
# index :name
|
143
|
+
# end
|
144
|
+
#
|
145
|
+
def index(*rel_type_props)
|
146
|
+
if rel_type_props.size == 2 and rel_type_props[1].kind_of?(Hash)
|
147
|
+
rel_type_props[1].each_pair do |key, value|
|
148
|
+
idx = rel_type_props[0]
|
149
|
+
indexer.field_infos[idx.to_sym][key] = value
|
150
|
+
end
|
151
|
+
rel_type_props = rel_type_props[0..0]
|
152
|
+
end
|
153
|
+
rel_type_props.each do |rel_type_prop|
|
154
|
+
rel_name, prop = rel_type_prop.to_s.split('.')
|
155
|
+
index_property(rel_name) if prop.nil?
|
156
|
+
index_relationship(rel_name, prop) unless prop.nil?
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
# Remove one or more specified indexes.
|
162
|
+
# Those indexes will not be updated anymore, old indexes will still exist
|
163
|
+
# until the update_index method is called.
|
164
|
+
#
|
165
|
+
def remove_index(*keys)
|
166
|
+
keys.each do |key|
|
167
|
+
raise "Not implemented remove index on a relationship index" if key.to_s.include?('.')
|
168
|
+
indexer.remove_index_on_property(key)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
def index_property(prop) # :nodoc:
|
174
|
+
indexer.add_index_on_property(prop)
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
def index_relationship(rel_name, prop) # :nodoc:
|
179
|
+
# find the trigger and updater classes and the rel_type of the given rel_name
|
180
|
+
trigger_clazz = decl_relationships[rel_name.to_sym].to_class
|
181
|
+
trigger_clazz ||= self # if not defined in a has_n
|
182
|
+
|
183
|
+
updater_clazz = self
|
184
|
+
|
185
|
+
dsl = decl_relationships[rel_name.to_sym]
|
186
|
+
rel_type = dsl.to_type # this or the other node we index ?
|
187
|
+
rel_type ||= rel_name # if not defined (in a has_n) use the same name as the rel_name
|
188
|
+
|
189
|
+
if dsl.outgoing?
|
190
|
+
namespace_type = dsl.namespace_type
|
191
|
+
else
|
192
|
+
clazz = dsl.to_class || node.class
|
193
|
+
namespace_type = clazz.decl_relationships[dsl.to_type].namespace_type
|
194
|
+
end
|
195
|
+
|
196
|
+
# add index on the trigger class and connect it to the updater_clazz
|
197
|
+
# (a trigger may cause an update of the index using the Indexer specified on the updater class)
|
198
|
+
trigger_clazz.indexer.add_index_in_relationship_on_property(updater_clazz, rel_name, rel_type, prop, namespace_type.to_sym)
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
# Finds all nodes or relationship of this type (and ancestors of this type) having
|
203
|
+
# the specified property values.
|
204
|
+
# See the lucene module for more information how to do a query.
|
205
|
+
#
|
206
|
+
# ==== Example
|
207
|
+
# MyNode.find(:name => 'foo', :company => 'bar')
|
208
|
+
#
|
209
|
+
# Or using a DSL query (experimental)
|
210
|
+
# MyNode.find{(name == 'foo') & (company == 'bar')}
|
211
|
+
#
|
212
|
+
# ==== Returns
|
213
|
+
# Neo4j::SearchResult
|
214
|
+
#
|
215
|
+
def find(query=nil, &block)
|
216
|
+
self.indexer.find(query, block)
|
217
|
+
end
|
218
|
+
|
219
|
+
|
220
|
+
# Creates a new value object class (a Struct) representing this class.
|
221
|
+
#
|
222
|
+
# The struct will have the Ruby on Rails method: model_name and
|
223
|
+
# new_record? so that it can be used for restful routing.
|
224
|
+
#
|
225
|
+
def create_value_class # :nodoc:
|
226
|
+
# the name of the class we want to create
|
227
|
+
name = "#{self.to_s}ValueObject".gsub("::", '_')
|
228
|
+
|
229
|
+
# remove previous class if exists
|
230
|
+
Neo4j.instance_eval do
|
231
|
+
remove_const name
|
232
|
+
end if Neo4j.const_defined?(name)
|
233
|
+
|
234
|
+
# get the properties we want in the new class
|
235
|
+
props = self.properties_info.keys.map{|k| ":#{k}"}.join(',')
|
236
|
+
Neo4j.module_eval %Q[class #{name} < Struct.new(#{props}); end]
|
237
|
+
|
238
|
+
# get reference to the new class
|
239
|
+
clazz = Neo4j.const_get(name)
|
240
|
+
|
241
|
+
# make it more Ruby on Rails friendly - try adding model_name method
|
242
|
+
if self.respond_to?(:model_name)
|
243
|
+
model = self.model_name.clone
|
244
|
+
(
|
245
|
+
class << clazz;
|
246
|
+
self;
|
247
|
+
end).instance_eval do
|
248
|
+
define_method(:model_name) {model}
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
# by calling the _update method we change the state of the struct
|
253
|
+
# so that new_record returns false - Ruby on Rails
|
254
|
+
clazz.instance_eval do
|
255
|
+
define_method(:_update) do |hash|
|
256
|
+
@_updated = true
|
257
|
+
hash.each_pair {|key, value| self[key.to_sym] = value if members.include?(key.to_s) }
|
258
|
+
end
|
259
|
+
define_method(:new_record?) { ! defined?(@_updated) }
|
260
|
+
end
|
261
|
+
|
262
|
+
clazz
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
module Neo4j::RelClassMethods
|
2
|
+
|
3
|
+
|
4
|
+
# Contains information of all relationships, name, type, and multiplicity
|
5
|
+
#
|
6
|
+
# :api: private
|
7
|
+
def decl_relationships # :nodoc:
|
8
|
+
self::DECL_RELATIONSHIPS
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
# Specifies a relationship between two node classes.
|
13
|
+
# Generates assignment and accessor methods for the given relationship
|
14
|
+
#
|
15
|
+
# ==== Example
|
16
|
+
#
|
17
|
+
# class FileNode
|
18
|
+
# include Ne4j::NodeMixin
|
19
|
+
# has_one(:folder)
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# file = FileNode.new
|
23
|
+
# file.folder = Neo4j::Node.new
|
24
|
+
# file.folder # => the node above
|
25
|
+
#
|
26
|
+
# ==== Returns
|
27
|
+
#
|
28
|
+
# Neo4j::Relationships::DeclRelationshipDsl
|
29
|
+
#
|
30
|
+
def has_one(rel_type, params = {})
|
31
|
+
clazz = self
|
32
|
+
module_eval(%Q{def #{rel_type}=(value)
|
33
|
+
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
|
34
|
+
r = Neo4j::Relationships::HasN.new(self, dsl)
|
35
|
+
r.each {|n| n.del} # delete previous relationships, only one can exist
|
36
|
+
r << value
|
37
|
+
r
|
38
|
+
end}, __FILE__, __LINE__)
|
39
|
+
|
40
|
+
module_eval(%Q{def #{rel_type}
|
41
|
+
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
|
42
|
+
r = Neo4j::Relationships::HasN.new(self, dsl)
|
43
|
+
[*r][0]
|
44
|
+
end}, __FILE__, __LINE__)
|
45
|
+
|
46
|
+
module_eval(%Q{
|
47
|
+
def #{rel_type}_rel
|
48
|
+
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
|
49
|
+
r = Neo4j::Relationships::HasN.new(self, dsl).rels
|
50
|
+
[*r][0]
|
51
|
+
end}, __FILE__, __LINE__)
|
52
|
+
|
53
|
+
decl_relationships[rel_type.to_sym] = Neo4j::Relationships::DeclRelationshipDsl.new(rel_type, params)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
# Specifies a relationship between two node classes.
|
58
|
+
# Generates assignment and accessor methods for the given relationship.
|
59
|
+
#
|
60
|
+
# ==== Example
|
61
|
+
#
|
62
|
+
# class FolderNode
|
63
|
+
# include Ne4j::NodeMixin
|
64
|
+
# has_n(:files)
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# folder = FolderNode.new
|
68
|
+
# folder.files << Neo4j::Node.new << Neo4j::Node.new
|
69
|
+
# folder.files.inject {...}
|
70
|
+
#
|
71
|
+
# ==== Returns
|
72
|
+
#
|
73
|
+
# Neo4j::Relationships::DeclRelationshipDsl
|
74
|
+
#
|
75
|
+
def has_n(rel_type, params = {})
|
76
|
+
clazz = self
|
77
|
+
module_eval(%Q{
|
78
|
+
def #{rel_type}(&block)
|
79
|
+
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
|
80
|
+
Neo4j::Relationships::HasN.new(self, dsl, &block)
|
81
|
+
end}, __FILE__, __LINE__)
|
82
|
+
|
83
|
+
module_eval(%Q{
|
84
|
+
def #{rel_type}_rels
|
85
|
+
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
|
86
|
+
Neo4j::Relationships::HasN.new(self, dsl).rels
|
87
|
+
end}, __FILE__, __LINE__)
|
88
|
+
|
89
|
+
decl_relationships[rel_type.to_sym] = Neo4j::Relationships::DeclRelationshipDsl.new(rel_type, params)
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
# Specifies a relationship to a linked list of nodes.
|
94
|
+
# Each list item class may (but not necessarily use the belongs_to_list
|
95
|
+
# in order to specify which ruby class should be loaded when a list item is loaded.
|
96
|
+
#
|
97
|
+
# ==== Example
|
98
|
+
#
|
99
|
+
# class Company
|
100
|
+
# include Neo4j::NodeMixin
|
101
|
+
# has_list :employees
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# company = Company.new
|
105
|
+
# company.employees << employee1 << employee2
|
106
|
+
#
|
107
|
+
# # prints first employee2 and then employee1
|
108
|
+
# company.employees.each {|employee| puts employee.name}
|
109
|
+
#
|
110
|
+
# ===== Size Counter
|
111
|
+
# If the optional parameter :size is given then the list will contain a size counter.
|
112
|
+
#
|
113
|
+
# Example
|
114
|
+
#
|
115
|
+
# class Company
|
116
|
+
# include Neo4j::NodeMixin
|
117
|
+
# has_list :employees, :counter => true
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# company = Company.new
|
121
|
+
# company.employees << employee1 << employee2
|
122
|
+
# company.employees.size # => 2
|
123
|
+
#
|
124
|
+
# ==== Deleted List Items
|
125
|
+
#
|
126
|
+
# The list will be updated if an item is deleted in a list.
|
127
|
+
# Example:
|
128
|
+
#
|
129
|
+
# company = Company.new
|
130
|
+
# company.employees << employee1 << employee2 << employee3
|
131
|
+
# company.employees.size # => 3
|
132
|
+
#
|
133
|
+
# employee2.del
|
134
|
+
#
|
135
|
+
# [*company.employees] # => [employee1, employee3]
|
136
|
+
# company.employees.size # => 2
|
137
|
+
#
|
138
|
+
# ===== List Items Memberships
|
139
|
+
#
|
140
|
+
# For deciding which lists a node belongs to see the Neo4j::NodeMixin#list method
|
141
|
+
#
|
142
|
+
# :api: public
|
143
|
+
def has_list(rel_type, params = {})
|
144
|
+
clazz = self
|
145
|
+
#(self.kind_of?(Module))? self : self.class
|
146
|
+
module_eval(%Q{
|
147
|
+
def #{rel_type}(&block)
|
148
|
+
dsl = #{clazz}.decl_relationships[:'#{rel_type.to_s}']
|
149
|
+
Neo4j::Relationships::HasList.new(self, dsl, &block)
|
150
|
+
end}, __FILE__, __LINE__)
|
151
|
+
Neo4j.event_handler.add Neo4j::Relationships::HasList
|
152
|
+
decl_relationships[rel_type.to_sym] = Neo4j::Relationships::DeclRelationshipDsl.new(rel_type, params)
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
# Can be used together with the has_list to specify the ruby class of a list item.
|
157
|
+
#
|
158
|
+
# :api: public
|
159
|
+
def belongs_to_list(rel_type, params = {})
|
160
|
+
decl_relationships[rel_type] = Neo4j::Relationships::DeclRelationshipDsl.new(rel_type, params)
|
161
|
+
end
|
162
|
+
|
163
|
+
def indexer # :nodoc:
|
164
|
+
Neo4j::Indexer.instance(root_class) # create an indexer that search for nodes (and not relationships)
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Neo4j
|
2
|
+
|
3
|
+
|
4
|
+
# A module that can be mixed in like a Neo4j::NodeMixin
|
5
|
+
# It wraps the Neo4j Relationship class.
|
6
|
+
# It includes the Neo4j::PropertyClassMethods class methods.
|
7
|
+
#
|
8
|
+
module RelationshipMixin
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
attr_reader :_java_node
|
12
|
+
|
13
|
+
def_delegators :@_java_node, :[]=, :[], :property?, :props, :update, :neo_id, :del, :start_node, :end_node, :other_node, :relationship_type, :wrapper
|
14
|
+
|
15
|
+
# Initialize the Relationship object with specified java org.neo4j.graphdb.Relationship object
|
16
|
+
# Expects at least one parameter.
|
17
|
+
#
|
18
|
+
# This method is used both when neo4j.rb loads relationships from the database as well as when
|
19
|
+
# a relationship is first created. If you are only interested in when the relationship is created in the database
|
20
|
+
# see the #init_with_args (which you may want to override and call super on).
|
21
|
+
#
|
22
|
+
# ==== Parameters (when loading from DB)
|
23
|
+
# param1:: the internal java relationship object (org.neo4j.graphdb.Relationship)
|
24
|
+
#
|
25
|
+
# ==== Parameters (when creating a new relationship in db)
|
26
|
+
# type:: the key and value to be set
|
27
|
+
# from_node:: create relationship from this node
|
28
|
+
# to:: create relationship to this node
|
29
|
+
#
|
30
|
+
def initialize(*args)
|
31
|
+
if (args[0].kind_of?(Java::org.neo4j.graphdb.Relationship))
|
32
|
+
init_with_rel(args[0])
|
33
|
+
else
|
34
|
+
init_with_args(*args)
|
35
|
+
end
|
36
|
+
|
37
|
+
# must call super with no arguments so that chaining of initialize method will work
|
38
|
+
super()
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# Initialize this relationship with the given arguments.
|
43
|
+
# This method is only called when the relationship is created in the database
|
44
|
+
# (and not when it is loaded from the database).
|
45
|
+
#
|
46
|
+
# ==== Parameters
|
47
|
+
# type:: the key and value to be set
|
48
|
+
# from_node:: create relationship from this node
|
49
|
+
# to_node:: create relationship to this node
|
50
|
+
def init_with_args(type, from_node, to_node)
|
51
|
+
@_java_node = Neo4j.create_rel(type, from_node, to_node)
|
52
|
+
@_java_node._wrapper = self
|
53
|
+
@_java_node[:_classname] = self.class.to_s
|
54
|
+
Neo4j.event_handler.relationship_created(self)
|
55
|
+
self.class.indexer.on_relationship_created(@_wrapper, type)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Inits this node with the specified java neo relationship
|
59
|
+
#
|
60
|
+
# :api: private
|
61
|
+
def init_with_rel(rel)
|
62
|
+
@_java_node = rel # TODO hmm, should really name _java_node to something else
|
63
|
+
rel._wrapper=self
|
64
|
+
rel[:_classname] = self.class.to_s unless rel.property?(:_classname)
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def eql?(o)
|
69
|
+
o.kind_of?(RelationshipMixin) && o.internal_r == internal_r
|
70
|
+
end
|
71
|
+
|
72
|
+
def ==(o)
|
73
|
+
eql?(o)
|
74
|
+
end
|
75
|
+
|
76
|
+
def hash
|
77
|
+
_java_node.hashCode
|
78
|
+
end
|
79
|
+
|
80
|
+
# Adds class methods from
|
81
|
+
#
|
82
|
+
# * Neo4j::RelClassMethods
|
83
|
+
# * Neo4j::PropertyClassMethods
|
84
|
+
#
|
85
|
+
def self.included(c) # :nodoc:
|
86
|
+
c.instance_eval do
|
87
|
+
# these constants are used in the Neo4j::RelClassMethods and Neo4j::PropertyClassMethods
|
88
|
+
# they are defined here since they should only be defined once -
|
89
|
+
# all subclasses share the same index, declared properties and index_updaters
|
90
|
+
const_set(:ROOT_CLASS, self)
|
91
|
+
const_set(:PROPERTIES_INFO, {})
|
92
|
+
end unless c.const_defined?(:ROOT_CLASS)
|
93
|
+
c.extend Neo4j::PropertyClassMethods
|
94
|
+
c.extend ClassMethods
|
95
|
+
end
|
96
|
+
|
97
|
+
module ClassMethods
|
98
|
+
def indexer # :nodoc:
|
99
|
+
Neo4j::Indexer.instance(root_class, false) # create an indexer that search for relationships (and not nodes)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|