neo4j 2.0.0.alpha.5-java → 2.0.0.alpha.6-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. data/CHANGELOG +12 -0
  2. data/Gemfile +0 -4
  3. data/README.rdoc +106 -62
  4. data/lib/neo4j.rb +7 -33
  5. data/lib/neo4j/performance.rb +43 -0
  6. data/lib/neo4j/rails/accept_id.rb +19 -18
  7. data/lib/neo4j/rails/attributes.rb +366 -120
  8. data/lib/neo4j/rails/finders.rb +41 -15
  9. data/lib/neo4j/rails/has_n.rb +203 -0
  10. data/lib/neo4j/rails/identity.rb +25 -0
  11. data/lib/neo4j/rails/model.rb +65 -242
  12. data/lib/neo4j/rails/nested_attributes.rb +108 -0
  13. data/lib/neo4j/rails/node_persistance.rb +56 -0
  14. data/lib/neo4j/rails/observer.rb +0 -2
  15. data/lib/neo4j/rails/persistence.rb +32 -154
  16. data/lib/neo4j/rails/rack_middleware.rb +26 -2
  17. data/lib/neo4j/rails/rails.rb +9 -6
  18. data/lib/neo4j/rails/railtie.rb +1 -2
  19. data/lib/neo4j/rails/relationship.rb +18 -125
  20. data/lib/neo4j/rails/relationship_persistence.rb +107 -0
  21. data/lib/neo4j/rails/relationships/node_dsl.rb +72 -44
  22. data/lib/neo4j/rails/relationships/relationships.rb +187 -59
  23. data/lib/neo4j/rails/relationships/rels_dsl.rb +18 -17
  24. data/lib/neo4j/rails/relationships/storage.rb +19 -17
  25. data/lib/neo4j/rails/timestamps.rb +53 -51
  26. data/lib/neo4j/rails/transaction.rb +9 -1
  27. data/lib/neo4j/rails/validations/uniqueness.rb +1 -1
  28. data/lib/neo4j/rails/versioning/versioning.rb +2 -2
  29. data/lib/neo4j/version.rb +1 -1
  30. data/lib/orm_adapter/adapters/neo4j.rb +47 -46
  31. data/neo4j.gemspec +1 -1
  32. metadata +10 -69
  33. data/lib/neo4j/algo/algo.rb +0 -294
  34. data/lib/neo4j/batch/batch.rb +0 -4
  35. data/lib/neo4j/batch/indexer.rb +0 -109
  36. data/lib/neo4j/batch/inserter.rb +0 -179
  37. data/lib/neo4j/batch/rule_inserter.rb +0 -24
  38. data/lib/neo4j/batch/rule_node.rb +0 -72
  39. data/lib/neo4j/config.rb +0 -177
  40. data/lib/neo4j/core_ext/class/inheritable_attributes.rb +0 -12
  41. data/lib/neo4j/core_ext/class/rewrite_inheritable_attributes.rb +0 -170
  42. data/lib/neo4j/database.rb +0 -158
  43. data/lib/neo4j/equal.rb +0 -21
  44. data/lib/neo4j/event_handler.rb +0 -263
  45. data/lib/neo4j/has_list/class_methods.rb +0 -11
  46. data/lib/neo4j/has_list/has_list.rb +0 -3
  47. data/lib/neo4j/has_list/mapping.rb +0 -133
  48. data/lib/neo4j/has_n/class_methods.rb +0 -119
  49. data/lib/neo4j/has_n/decl_relationship_dsl.rb +0 -246
  50. data/lib/neo4j/has_n/has_n.rb +0 -3
  51. data/lib/neo4j/has_n/mapping.rb +0 -98
  52. data/lib/neo4j/identity_map.rb +0 -140
  53. data/lib/neo4j/index/class_methods.rb +0 -108
  54. data/lib/neo4j/index/index.rb +0 -39
  55. data/lib/neo4j/index/indexer.rb +0 -341
  56. data/lib/neo4j/index/indexer_registry.rb +0 -68
  57. data/lib/neo4j/index/lucene_query.rb +0 -256
  58. data/lib/neo4j/load.rb +0 -25
  59. data/lib/neo4j/migrations/class_methods.rb +0 -110
  60. data/lib/neo4j/migrations/extensions.rb +0 -58
  61. data/lib/neo4j/migrations/lazy_node_mixin.rb +0 -41
  62. data/lib/neo4j/migrations/migration.rb +0 -112
  63. data/lib/neo4j/migrations/migrations.rb +0 -6
  64. data/lib/neo4j/migrations/node_mixin.rb +0 -80
  65. data/lib/neo4j/migrations/ref_node_wrapper.rb +0 -32
  66. data/lib/neo4j/model.rb +0 -4
  67. data/lib/neo4j/neo4j.rb +0 -216
  68. data/lib/neo4j/node.rb +0 -270
  69. data/lib/neo4j/node_mixin/class_methods.rb +0 -51
  70. data/lib/neo4j/node_mixin/node_mixin.rb +0 -141
  71. data/lib/neo4j/paginated.rb +0 -23
  72. data/lib/neo4j/property/class_methods.rb +0 -79
  73. data/lib/neo4j/property/property.rb +0 -111
  74. data/lib/neo4j/rails/mapping/property.rb +0 -183
  75. data/lib/neo4j/rails/rel_persistence.rb +0 -237
  76. data/lib/neo4j/relationship.rb +0 -239
  77. data/lib/neo4j/relationship_mixin/class_methods.rb +0 -36
  78. data/lib/neo4j/relationship_mixin/relationship_mixin.rb +0 -142
  79. data/lib/neo4j/relationship_set.rb +0 -58
  80. data/lib/neo4j/rels/rels.rb +0 -110
  81. data/lib/neo4j/rels/traverser.rb +0 -102
  82. data/lib/neo4j/rule/class_methods.rb +0 -201
  83. data/lib/neo4j/rule/event_listener.rb +0 -66
  84. data/lib/neo4j/rule/functions/count.rb +0 -43
  85. data/lib/neo4j/rule/functions/function.rb +0 -74
  86. data/lib/neo4j/rule/functions/functions.rb +0 -3
  87. data/lib/neo4j/rule/functions/sum.rb +0 -29
  88. data/lib/neo4j/rule/rule.rb +0 -150
  89. data/lib/neo4j/rule/rule_node.rb +0 -217
  90. data/lib/neo4j/to_java.rb +0 -31
  91. data/lib/neo4j/transaction.rb +0 -73
  92. data/lib/neo4j/traversal/filter_predicate.rb +0 -25
  93. data/lib/neo4j/traversal/prune_evaluator.rb +0 -14
  94. data/lib/neo4j/traversal/rel_expander.rb +0 -31
  95. data/lib/neo4j/traversal/traversal.rb +0 -141
  96. data/lib/neo4j/traversal/traverser.rb +0 -284
  97. data/lib/neo4j/type_converters/type_converters.rb +0 -288
@@ -1,103 +1,206 @@
1
1
  module Neo4j
2
2
  module Rails
3
- module Relationships
4
-
5
- def write_changed_relationships #:nodoc:
6
- @_relationships.each_value do |storage|
7
- storage.persist
8
- end
9
- end
10
3
 
11
- def relationships_changed?
12
- @_relationships.each_value do |storage|
13
- return true if !storage.persisted?
14
- end
15
- false
16
- end
4
+ # This module overrides the Neo4j::Core::Rels module so that it can handle unpersisted relationships.
5
+ #
6
+ module Relationships
7
+ extend ActiveSupport::Concern
17
8
 
18
- def clear_relationships #:nodoc:
19
- @_relationships && @_relationships.each_value{|storage| storage.remove_from_identity_map}
20
- @_relationships = {}
9
+ included do
10
+ # TODO THIS DOES NOT WORK
11
+ alias_method :_core_rels, :rels
12
+ alias_method :_core_rel, :rel
13
+ alias_method :_core_rel?, :rel?
14
+ alias_method :_core_outgoing, :outgoing
15
+ alias_method :_core_incoming, :incoming
21
16
  end
22
17
 
23
-
24
- def _create_or_get_storage(rel_type) #:nodoc:
25
- dsl = _decl_rels_for(rel_type.to_sym)
26
- @_relationships[rel_type.to_sym] ||= Storage.new(self, rel_type, dsl)
18
+ # Same as {http://rdoc.info/github/andreasronge/neo4j-core/master/Neo4j/Core/Rels#outgoing-instance_method Neo4j::Core::Node#outgoing}
19
+ # Since the incoming method is redefined we might need the neo4j-core version of this method.
20
+ # @see #incoming
21
+ # @api public
22
+ def _outgoing(rel_type)
23
+ _core_outgoing(rel_type)
27
24
  end
28
25
 
29
- def _create_or_get_storage_for_decl_rels(decl_rels) #:nodoc:
30
- @_relationships[decl_rels.rel_type.to_sym] ||= Storage.new(self, decl_rels.rel_type, decl_rels)
31
- end
32
-
33
- def _storage_for(rel_type) #:nodoc:
34
- @_relationships[rel_type.to_sym]
26
+ # Same as {http://rdoc.info/github/andreasronge/neo4j-core/master/Neo4j/Core/Rels#incoming-instance_method Neo4j::Core::Node#incoming}
27
+ # Since the incoming method is redefined we might need the neo4j-core version of this method.
28
+ # @see #incoming
29
+ # @api public
30
+ def _incoming(rel_type)
31
+ _core_incomig(rel_type)
35
32
  end
36
33
 
37
- # If the node is persisted and it does not have any unsaved relationship it returns a Neo4j::NodeTraverser.
38
- # Otherwise it will return a NodesDSL which behaves like the Neo4j::NodeTraverser except that it does not
39
- # allow to traverse both persisted and not persisted (not saved yet) relationship more then depth one.
34
+ # Similar to {http://rdoc.info/github/andreasronge/neo4j-core/master/Neo4j/Core/Rels#outgoing-instance_method Neo4j::Core::Node#outgoing}
35
+ # but allows traversing both persisted and unpersisted relationship of depth one.
36
+ # For deeper or more advanced traversals see #_outgoing
40
37
  #
41
- # See, Neo4j::NodeRelationship#outgoing (when node is persisted) which returns a Neo4j::NodeTraverser
38
+ # @example create a relationship between two nodes which is not persisted
39
+ # node.outgoing(:foo) << other_node
40
+ #
41
+ # @example create and persist a relationship between two nodes with given node
42
+ # node.outgoing(:foo) << other_node
43
+ # node.save
44
+ #
45
+ # @example create a new node with given attributes and persist the relationship
46
+ # node.outgoing(:foo).create(:name => 'bla')
42
47
  #
48
+ # @example create a new node with given attributes and but do not persist the relationship
49
+ # node.outgoing(:foo).new(:name => 'bla')
50
+ #
51
+ # @see #rels
52
+ # @see #_outgoing
53
+ # @api public
54
+ # @param [String, Symbol] rel_type the relationship type we want to create or traverse
55
+ # @return [NodesDSL]
43
56
  def outgoing(rel_type)
44
57
  storage = _create_or_get_storage(rel_type)
45
58
  NodesDSL.new(storage, :outgoing)
46
59
  end
47
60
 
48
61
 
49
- # Traverse or update an incoming relationship
62
+ # Similar to {http://rdoc.info/github/andreasronge/neo4j-core/master/Neo4j/Core/Rels#incoming-instance_method Neo4j::Core::Node#incoming}
63
+ # but allows traversing both persisted and unpersisted relationship of depth one.
50
64
  # See #outgoing
51
65
  # See, Neo4j::NodeRelationship#outgoing (when node is persisted) which returns a Neo4j::NodeTraverser
66
+ # @param (see #outgoing)
67
+ # @api public
68
+ # @return [NodesDSL]
52
69
  def incoming(rel_type)
53
70
  storage = _create_or_get_storage(rel_type)
54
71
  NodesDSL.new(storage, :incoming)
55
72
  end
56
73
 
57
- # See Neo4j::Rels#rels.
58
- #
59
- # Will also allow to access unsaved relationships - like the #outgoing and #incoming method.
60
- # It one argument - the relationship type.
61
- #
74
+ # Allow to access persisted and unpersisted relationships - like the #outgoing and #incoming method.
62
75
  # To only find all the persisted relationship, node._java_node.rels
63
76
  #
64
- # === Example
65
- #
77
+ # @example
66
78
  # node.outgoing(:foo) << node2
67
- # node.rels(:foo).outgoing #=> [node2]
79
+ # node.rels(:outgoing, :foo).outgoing #=> [node2]
68
80
  # node.rels(:foo).incoming #=> []
69
- # node.rels(:foo) #=> [node2] - incoming and outgoing
81
+ # node.rels(:both) #=> [node2] - incoming and outgoing of any type
82
+ #
83
+ # @example All the relationships between me and another node of given dir & type
84
+ # me.rels(:outgoing, :friends).to_other(node)
70
85
  #
71
- def rels(*rel_types)
72
- if rel_types.empty?
73
- AllRelsDsl.new(@_relationships, _java_node)
86
+ # @param [:both, :incoming, :outgoing] dir the direction of the relationship
87
+ # @param [String, Symbol] rel_type the requested relationship types we want look for, if none it gets relationships of any type
88
+ # @return [RelsDSL, AllRelsDsl] an object which included the Ruby Enumerable mixin
89
+ # @note it can return an unpersisted relationship
90
+ def rels(dir=:both, rel_type=nil)
91
+ raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
92
+ if rel_type.nil?
93
+ AllRelsDsl.new(@_relationships, _java_node, dir)
74
94
  else
75
- storage = _create_or_get_storage(rel_types.first)
76
- RelsDSL.new(storage)
95
+ storage = _create_or_get_storage(rel_type)
96
+ RelsDSL.new(storage, dir)
77
97
  end
78
98
  end
79
99
 
80
- def add_outgoing_rel(rel_type, rel) #:nodoc:
81
- _create_or_get_storage(rel_type).add_outgoing_rel(rel)
100
+ # Returns the only relationship of a given type and direction that is attached to this node, or nil.
101
+ # This is a convenience method that is used in the commonly occuring situation where a node has exactly zero or
102
+ # one relationships of a given type and direction to another node.
103
+ # Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships
104
+ # exist, it is a fatal error that should generate an unchecked exception. This method reflects that semantics and
105
+ # returns either:
106
+ #
107
+ # * nil if there are zero relationships of the given type and direction,
108
+ # * the relationship if there's exactly one, or
109
+ # * raise an exception in all other cases.
110
+ # @param [:both, :incoming, :outgoing] dir the direction of the relationship
111
+ # @param [Symbol, String] rel_type the type of relationship, see Neo4j::Core::Relationship#rel_type
112
+ # @return [Neo4j::Relationship, nil, Neo4j::Rails::Relationship] the Relationship or wrapper for the Relationship or nil
113
+ # @raise an exception if more then one relationship of given type and direction was found
114
+ # @note it can return an unpersisted relationship
115
+ def rel(dir, rel_type)
116
+ raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
117
+ storage = _create_or_get_storage(rel_type)
118
+ storage.single_relationship(dir)
82
119
  end
83
120
 
84
- def add_incoming_rel(rel_type, rel) #:nodoc:
85
- _create_or_get_storage(rel_type).add_incoming_rel(rel)
121
+ # Check if the given relationship exists
122
+ # Returns true if there are one or more relationships from this node to other nodes
123
+ # with the given relationship.
124
+ #
125
+ # @param [:both, :incoming, :outgoing] dir optional default :both (either, :outgoing, :incoming, :both)
126
+ # @param [String,Symbol] rel_type the key and value to be set, default any type
127
+ # @return [Boolean] true if one or more relationships exists for the given type and dir otherwise false
128
+ def rel?(dir=:both, rel_type=nil)
129
+ raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
130
+ storage = _create_or_get_storage(rel_type)
131
+ !!storage.single_relationship(dir)
86
132
  end
87
133
 
88
- def rm_incoming_rel(rel_type, rel) #:nodoc:
89
- _create_or_get_storage(rel_type).rm_incoming_rel(rel)
134
+
135
+ # Works like #rels method but instead returns the nodes.
136
+ # It does try to load a Ruby wrapper around each node
137
+ # @param (see #rels)
138
+ # @return [Enumerable] an Enumeration of either Neo4j::Node objects or wrapped Neo4j::Node objects
139
+ # @notice it's possible that the same node is returned more then once because of several relationship reaching to the same node, see #outgoing for alternative
140
+ def nodes(dir, rel_type)
141
+ raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
142
+ storage = _create_or_get_storage(rel_type)
143
+ NodesDSL.new(storage, dir)
90
144
  end
91
145
 
92
- def rm_outgoing_rel(rel_type, rel) #:nodoc:
93
- _create_or_get_storage(rel_type).rm_outgoing_rel(rel)
146
+ # Returns the only node of a given type and direction that is attached to this node, or nil.
147
+ # This is a convenience method that is used in the commonly occuring situation where a node has exactly zero or one relationships of a given type and direction to another node.
148
+ # Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an exception.
149
+ # This method reflects that semantics and returns either:
150
+ # * nil if there are zero relationships of the given type and direction,
151
+ # * the relationship if there's exactly one, or
152
+ # * throws an unchecked exception in all other cases.
153
+ #
154
+ # This method should be used only in situations with an invariant as described above. In those situations, a "state-checking" method (e.g. #rel?) is not required,
155
+ # because this method behaves correctly "out of the box."
156
+ #
157
+ # @param (see #rel)
158
+ # @see Neo4j::Core::Node#wrapper #wrapper - The method used to wrap the node in a Ruby object if the node was found
159
+ def node(dir, rel_type)
160
+ raise "Illegal argument, first argument must be :both, :incoming or :outgoing, got #{dir.inspect}" unless [:incoming, :outgoing, :both].include?(dir)
161
+ storage = _create_or_get_storage(rel_type)
162
+ storage.single_node(dir)
94
163
  end
95
164
 
96
- def rm_outgoing_rel(rel_type, rel) #:nodoc:
97
- _create_or_get_storage(rel_type).rm_outgoing_rel(rel)
165
+
166
+ # @private
167
+ def initialize_relationships
168
+ @_relationships = {}
169
+ end
170
+
171
+ # @private
172
+ def write_changed_relationships #:nodoc:
173
+ @_relationships.each_value do |storage|
174
+ storage.persist
175
+ end
98
176
  end
99
177
 
100
- def add_unpersisted_incoming_rel(rel_type, rel) #:nodoc
178
+ # @private
179
+ def relationships_changed?
180
+ @_relationships.each_value do |storage|
181
+ return true if !storage.persisted?
182
+ end
183
+ false
184
+ end
185
+
186
+ # @private
187
+ def clear_relationships
188
+ @_relationships && @_relationships.each_value { |storage| storage.remove_from_identity_map }
189
+ initialize_relationships
190
+ end
191
+
192
+ # @private
193
+ def add_outgoing_rel(rel_type, rel)
194
+ _create_or_get_storage(rel_type).add_outgoing_rel(rel)
195
+ end
196
+
197
+ # @private
198
+ def add_incoming_rel(rel_type, rel)
199
+ _create_or_get_storage(rel_type).add_incoming_rel(rel)
200
+ end
201
+
202
+ # @private
203
+ def add_unpersisted_incoming_rel(rel_type, rel)
101
204
  if (storage = _create_or_get_storage(rel_type))
102
205
  # move the relationship since we are now about to store the relationship
103
206
  storage.add_unpersisted_incoming_rel(rel)
@@ -105,7 +208,8 @@ module Neo4j
105
208
  end
106
209
  end
107
210
 
108
- def add_unpersisted_outgoing_rel(rel_type, rel) #:nodoc
211
+ # @private
212
+ def add_unpersisted_outgoing_rel(rel_type, rel)
109
213
  if (storage = _create_or_get_storage(rel_type))
110
214
  # move the relationship since we are now about to store the relationship
111
215
  storage.add_unpersisted_outgoing_rel(rel)
@@ -113,18 +217,42 @@ module Neo4j
113
217
  end
114
218
  end
115
219
 
116
- def rm_unpersisted_outgoing_rel(rel_type, rel) #:nodoc
220
+ # @private
221
+ def rm_unpersisted_outgoing_rel(rel_type, rel)
117
222
  if (storage = _storage_for(rel_type))
118
223
  storage.rm_unpersisted_outgoing_rel(rel)
119
224
  end
120
225
  end
121
226
 
122
- def rm_unpersisted_incoming_rel(rel_type, rel) #:nodoc
227
+ # @private
228
+ def rm_unpersisted_incoming_rel(rel_type, rel)
123
229
  if (storage = _storage_for(rel_type))
124
230
  storage.rm_unpersisted_incoming_rel(rel)
125
231
  end
126
232
  end
127
233
 
234
+ protected
235
+
236
+ def rm_incoming_rel(rel_type, rel) #:nodoc:
237
+ _create_or_get_storage(rel_type).rm_incoming_rel(rel)
238
+ end
239
+
240
+ def rm_outgoing_rel(rel_type, rel) #:nodoc:
241
+ _create_or_get_storage(rel_type).rm_outgoing_rel(rel)
242
+ end
243
+
244
+ def _create_or_get_storage(rel_type) #:nodoc:
245
+ @_relationships[rel_type.to_sym] ||= Storage.new(self, rel_type)
246
+ end
247
+
248
+ def _create_or_get_storage_for_decl_rels(decl_rels) #:nodoc:
249
+ @_relationships[decl_rels.rel_type.to_sym] ||= Storage.new(self, decl_rels.rel_type, decl_rels)
250
+ end
251
+
252
+ def _storage_for(rel_type) #:nodoc:
253
+ @_relationships[rel_type.to_sym]
254
+ end
255
+
128
256
 
129
257
  end
130
258
  end
@@ -7,13 +7,14 @@ module Neo4j
7
7
  class AllRelsDsl
8
8
  include Enumerable
9
9
 
10
- def initialize(rels_stores, node)
10
+ def initialize(rels_stores, node, dir)
11
11
  @rels_stores = rels_stores
12
12
  @node = node
13
+ @dir = dir
13
14
  end
14
15
 
15
16
  def each
16
- @rels_stores.each_value {|storage| storage.relationships(:both).each{|r| yield r}}
17
+ @rels_stores.each_value {|storage| storage.relationships(@dir).each{|r| yield r}}
17
18
  @node && @node.rels.each {|r| yield r}
18
19
  end
19
20
 
@@ -29,7 +30,7 @@ module Neo4j
29
30
  # Notice, only persisted relationships will be returned.
30
31
  def to_other(other)
31
32
  raise('node.rels(...).to_other() not allowed on a node that is not persisted') unless @node
32
- @node.rels.to_other(other)
33
+ @node._java_node.rels(@dir).to_other(other)
33
34
  end
34
35
 
35
36
  def delete_all
@@ -68,7 +69,7 @@ module Neo4j
68
69
  class RelsDSL
69
70
  include Enumerable
70
71
 
71
- def initialize(storage, dir=:both)
72
+ def initialize(storage, dir)
72
73
  @storage = storage
73
74
  @dir = dir
74
75
  end
@@ -76,16 +77,16 @@ module Neo4j
76
77
 
77
78
  # Same as Neo4j::Rails::Relationships::NodesDSL#build except that you specify the properties of the
78
79
  # relationships and it returns a relationship
79
- def build(attrs = {})
80
- node = @storage.build(attrs)
81
- @storage.create_relationship_to(node, @dir)
80
+ def build(rels_attrs = nil, node_attr = nil)
81
+ node = @storage.build(node_attr)
82
+ @storage.create_relationship_to(node, @dir, rels_attrs)
82
83
  end
83
84
 
84
85
  # Same as Neo4j::Rails::Relationships::NodesDSL#create except that you specify the properties of the
85
86
  # relationships and it returns a relationship
86
- def create(attrs = {})
87
- node = @storage.create(attrs)
88
- rel = @storage.create_relationship_to(node, @dir)
87
+ def create(rels_attrs = {}, node_attr = nil)
88
+ node = @storage.build(node_attr)
89
+ rel = @storage.create_relationship_to(node, @dir, rels_attrs)
89
90
  node.save
90
91
  rel
91
92
  end
@@ -94,7 +95,7 @@ module Neo4j
94
95
  # Returns an Enumerable. If the node is not persisted it will raise an error.
95
96
  # Notice, only persisted relationships will be returned.
96
97
  def to_other(other)
97
- @storage.to_other(other)
98
+ @storage.to_other(@dir, other)
98
99
  end
99
100
 
100
101
  # Connects this node with an already existing other node with a new relationship.
@@ -109,8 +110,8 @@ module Neo4j
109
110
 
110
111
  # Same as Neo4j::Rails::Relationships::NodesDSL#create! except that you specify the properties of the
111
112
  # relationships and it returns a relationship
112
- def create!(attrs)
113
- node = @storage.create(attrs)
113
+ def create!(rels_attrs)
114
+ node = @storage.create(rels_attrs)
114
115
  rel = @storage.create_relationship_to(node, @dir)
115
116
  node.save!
116
117
  rel
@@ -118,7 +119,7 @@ module Neo4j
118
119
 
119
120
  # Specifies that we want outgoing (undeclared) relationships.
120
121
  #
121
- # ==== Example
122
+ # @example
122
123
  # class Thing < Neo4j::Rails::Model
123
124
  # end
124
125
  #
@@ -141,13 +142,13 @@ module Neo4j
141
142
  end
142
143
 
143
144
  # Simply counts all relationships
144
- def size
145
- @storage.size(@dir)
145
+ def count
146
+ @storage.count(@dir)
146
147
  end
147
148
 
148
149
  # True if no relationship
149
150
  def empty?
150
- size == 0
151
+ !@storage.relationships?(@dir)
151
152
  end
152
153
 
153
154
  # Destroys all relationships object. Will not destroy the nodes.
@@ -4,10 +4,9 @@ module Neo4j
4
4
 
5
5
  # Holds the relationships in memory but also allows read access to persisted relationships
6
6
  class Storage #:nodoc:
7
- include Neo4j::ToJava
8
7
  attr_reader :dsl, :node, :rel_type
9
8
 
10
- def initialize(node, rel_type, dsl)
9
+ def initialize(node, rel_type, dsl=nil)
11
10
  @rel_type = rel_type.to_sym
12
11
  @node = node
13
12
  @rel_class = (dsl && dsl.relationship_class) || Neo4j::Rails::Relationship
@@ -37,17 +36,20 @@ module Neo4j
37
36
  @unpersisted_incoming_rels = nil
38
37
  end
39
38
 
40
- def size(dir)
39
+ def count(dir)
41
40
  counter = 0
42
41
  # count persisted relationship
43
- @node._java_node && @node._java_node.getRelationships(java_rel_type, dir_to_java(dir)).each { |*| counter += 1 }
42
+ @node._rels(dir, @rel_type).each { |*| counter += 1 } if @node.persisted?
44
43
  # count relationship which has not yet been persisted
45
44
  counter += relationships(dir).size
46
45
  counter
47
46
  end
48
47
 
49
- def to_other(other)
50
- (@node._java_node) ? @node._java_node.rels(@rel_type).to_other(other) : raise('node.rels(...).to_other() not allowed on a node that is not persisted')
48
+ def to_other(dir, other)
49
+ raise('node.rels(...).to_other() not allowed on a node that is not persisted') if @node.new_record?
50
+ all_relationships(dir).find_all do |rel|
51
+ rel._other_node(@node) == other
52
+ end
51
53
  end
52
54
 
53
55
  def build(attrs)
@@ -69,10 +71,6 @@ module Neo4j
69
71
  end
70
72
  end
71
73
 
72
- def java_rel_type
73
- type_to_java(rel_type)
74
- end
75
-
76
74
  def each_rel(dir, &block) #:nodoc:
77
75
  relationships(dir).each { |rel| block.call rel }
78
76
  if @node.persisted?
@@ -83,7 +81,7 @@ module Neo4j
83
81
 
84
82
  def cache_relationships(dir)
85
83
  @persisted_relationships[dir] ||= []
86
- node._java_node.getRelationships(java_rel_type, dir_to_java(dir)).each do |rel|
84
+ node._rels(dir, @rel_type).each do |rel|
87
85
  @persisted_relationships[dir] << rel.wrapper
88
86
  end
89
87
  end
@@ -91,8 +89,8 @@ module Neo4j
91
89
  def cache_persisted_nodes_and_relationships(dir)
92
90
  @persisted_related_nodes[dir] ||= []
93
91
  @persisted_node_to_relationships[dir] ||= {}
94
- @node._java_node.getRelationships(java_rel_type, dir_to_java(dir)).each do |rel|
95
- end_node = rel.getOtherNode(@node._java_node).wrapper
92
+ @node._rels(dir, @rel_type).each do |rel|
93
+ end_node = rel._other_node(@node._java_node).wrapper
96
94
  @persisted_related_nodes[dir] << end_node
97
95
  @persisted_node_to_relationships[dir][end_node]=rel
98
96
  end
@@ -116,10 +114,14 @@ module Neo4j
116
114
  end
117
115
  end
118
116
 
117
+ def relationships?(dir)
118
+ !!relationships(dir).first || (@node.persisted? && @node._rels(dir, @rel_type).first)
119
+ end
120
+
119
121
  def single_relationship(dir, raw = false)
120
122
  rel = relationships(dir).first
121
123
  if rel.nil? && @node.persisted?
122
- java_rel = @node._java_node.getSingleRelationship(java_rel_type, dir_to_java(dir))
124
+ java_rel = @node._rel(dir, @rel_type)
123
125
  raw ? java_rel : java_rel && java_rel.wrapper
124
126
  else
125
127
  rel
@@ -148,11 +150,11 @@ module Neo4j
148
150
  end
149
151
  end
150
152
 
151
- def create_relationship_to(to, dir)
153
+ def create_relationship_to(to, dir, attributes=nil)
152
154
  if dir == :outgoing
153
- @rel_class.new(@rel_type, @node, to, self)
155
+ @rel_class.new(@rel_type, @node, to, attributes)
154
156
  else
155
- @rel_class.new(@rel_type, to, @node, self)
157
+ @rel_class.new(@rel_type, to, @node, attributes)
156
158
  end
157
159
  end
158
160