neo4j 1.0.0.beta.17 → 1.0.0.beta.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -107,14 +107,14 @@ module Neo4j::Mapping
107
107
  end
108
108
 
109
109
  def create_relationship_to(node, other)
110
- dsl = incoming? ? incoming_dsl : self
111
-
112
- # If the are creating an incoming relationship we need to swap incoming and outgoing nodes
113
- if @direction == :outgoing
114
- from, to = node, other
115
- else
116
- from, to = other, node
117
- end
110
+ if incoming?
111
+ # If we are creating an incoming relationship, we need to swap incoming and outgoing nodes
112
+ dsl = incoming_dsl
113
+ from, to = other, node
114
+ else
115
+ dsl = self
116
+ from, to = node, other
117
+ end
118
118
 
119
119
  java_type = type_to_java(dsl.namespace_type)
120
120
 
@@ -2,7 +2,13 @@ module Neo4j
2
2
  module Mapping
3
3
 
4
4
  # Enables creating and traversal of nodes.
5
+ #
5
6
  # Includes the Enumerable Mixin.
7
+ # The Neo4j::Mapping::ClassMethods::Relationship#has_n and Neo4j::Mapping::ClassMethods::Relationship#one
8
+ # methods returns an object of this type.
9
+ #
10
+ # ==== See Also
11
+ # Neo4j::Mapping::ClassMethods::Relationship
6
12
  #
7
13
  class HasN
8
14
  include Enumerable
@@ -68,7 +74,11 @@ module Neo4j
68
74
  # self
69
75
  #
70
76
  def <<(other)
71
- @dsl.create_relationship_to(@node, other)
77
+ if @direction == :incoming && @dsl.direction == :outgoing
78
+ @dsl.create_relationship_to(other, @node)
79
+ else
80
+ @dsl.create_relationship_to(@node, other)
81
+ end
72
82
  self
73
83
  end
74
84
  end
@@ -1,5 +1,24 @@
1
1
  module Neo4j::Mapping
2
2
 
3
+ # This Mixin is used to wrap Neo4j Java Nodes in Ruby objects.
4
+ # It includes the Neo4j::Index module and forwards a number of method to the raw Java node which
5
+ # intern includes a number of mixins, see below.
6
+ #
7
+ # === Instance Mixins
8
+ # * Neo4j::Index
9
+ # * Neo4j::Property
10
+ # * Neo4j::NodeRelationship
11
+ # * Neo4j::Equal
12
+ # * Neo4j::Index
13
+ #
14
+ # === Class Mixins
15
+ # * Neo4j::Mapping::ClassMethods::Root
16
+ # * Neo4j::Mapping::ClassMethods::Property
17
+ # * Neo4j::Mapping::ClassMethods::InitNode
18
+ # * Neo4j::Mapping::ClassMethods::Relationship
19
+ # * Neo4j::Mapping::ClassMethods::Rule
20
+ # * Neo4j::Index::ClassMethods
21
+ #
3
22
  module NodeMixin
4
23
  extend Forwardable
5
24
  include Neo4j::Index
@@ -22,6 +41,7 @@ module Neo4j::Mapping
22
41
 
23
42
 
24
43
  # Creates a new node and initialize with given properties.
44
+ # You can override this to provide your own initialization.
25
45
  #
26
46
  def init_on_create(*args) # :nodoc:
27
47
  self[:_classname] = self.class.to_s
data/lib/neo4j/neo4j.rb CHANGED
@@ -1,8 +1,16 @@
1
+ # = Neo4j
2
+ #
3
+ # The Neo4j modules is used to interact with an Neo4j Database instance.
4
+ # You can for example start and stop an instance and list all the nodes that exist in the database.
5
+ #
6
+ # === Starting and Stopping Neo4j
7
+ # You don't normally need to start the Neo4j database since it will be automatically started when needed.
8
+ # Before the database is started you should configure where the database is stored, see Neo4j::Config.
9
+ #
1
10
  module Neo4j
2
11
 
3
12
  class << self
4
-
5
-
13
+
6
14
  # Start Neo4j using the default database.
7
15
  # This is usally not required since the database will be started automatically when it is used.
8
16
  #
@@ -31,9 +39,10 @@ module Neo4j
31
39
 
32
40
 
33
41
  # Returns an unstarted db instance
34
- # this is typically used for configuring the database, which must sometimes
42
+ #
43
+ # This is typically used for configuring the database, which must sometimes
35
44
  # be done before the database is started
36
- # if the database was already started an excetion will be raised
45
+ # if the database was already started an exception will be raised
37
46
  def unstarted_db
38
47
  @db ||= Database.new
39
48
  raise "database was already started" if @db.running?
@@ -46,10 +55,18 @@ module Neo4j
46
55
  end
47
56
 
48
57
 
58
+ # Stops this database
59
+ # There are Ruby hooks that will do this automatically for you.
60
+ #
49
61
  def shutdown(this_db = @db)
50
62
  this_db.shutdown if this_db
51
63
  end
52
64
 
65
+ # Returns the reference node, which is a "starting point" in the node space.
66
+ #
67
+ # Usually, a client attaches relationships to this node that leads into various parts of the node space.
68
+ # For more information about common node space organizational patterns, see the design guide at http://wiki.neo4j.org/content/Design_Guide
69
+ #
53
70
  def ref_node(this_db = self.started_db)
54
71
  this_db.graph.reference_node
55
72
  end
@@ -59,11 +76,13 @@ module Neo4j
59
76
  Enumerator.new(this_db, :each_node)
60
77
  end
61
78
 
62
- # Same as #all_nodes but does not return wrapped nodes.
79
+ # Same as #all_nodes but does not return wrapped nodes but instead raw java node objects.
63
80
  def _all_nodes(this_db = self.started_db)
64
81
  Enumerator.new(this_db, :_each_node)
65
82
  end
66
83
 
84
+ # Returns the Neo4j::EventHandler
85
+ #
67
86
  def event_handler(this_db = default_db)
68
87
  this_db.event_handler
69
88
  end
data/lib/neo4j/node.rb CHANGED
@@ -58,7 +58,7 @@ module Neo4j
58
58
  # * Neo4j::Index::ClassMethods lucene index class methods, like find
59
59
  # * Neo4j::Load - methods for loading a node
60
60
  #
61
- # See also the Neo4j::NodeMixin if you want to wrap a node with your own Ruby class.
61
+ # See also the Neo4j::NodeMixin (Neo4j::Mapping::NodeMixin) if you want to wrap a node with your own Ruby class.
62
62
  #
63
63
  class Node
64
64
  extend Neo4j::Index::ClassMethods
@@ -73,7 +73,7 @@ module Neo4j
73
73
  end
74
74
 
75
75
  # Sets the property of this node.
76
- # Property keys are always strings. Valid property value types are the primitives(<tt>String</tt>, <tt>Fixnum</tt>, <tt>Float</tt>, <tt>Boolean</tt>), and arrays of those primitives.
76
+ # Property keys are always strings. Valid property value types are the primitives(<tt>String</tt>, <tt>Fixnum</tt>, <tt>Float</tt>, <tt>Boolean</tt>).
77
77
  #
78
78
  def []=(key, value)
79
79
  k = key.to_s
@@ -57,7 +57,6 @@ module Neo4j
57
57
  persisted? ? [:id] : nil
58
58
  end
59
59
 
60
-
61
60
  # enables ActiveModel::Dirty and Validation
62
61
  def method_missing(method_id, *args, &block)
63
62
  if !self.class.attribute_methods_generated?
@@ -101,7 +100,6 @@ module Neo4j
101
100
  end
102
101
  end
103
102
 
104
-
105
103
  # Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved.
106
104
  # If saving fails because the resource is invalid then false will be returned.
107
105
  def update_attributes(attributes)
@@ -161,12 +159,6 @@ module Neo4j
161
159
  end
162
160
  end
163
161
 
164
- def delete
165
- super
166
- @_deleted = true
167
- @_persisted = false
168
- end
169
-
170
162
  def save
171
163
  _run_save_callbacks do
172
164
  if create_or_update_node
@@ -208,13 +200,12 @@ module Neo4j
208
200
  @previously_changed = changes
209
201
  @changed_attributes.clear
210
202
  end
211
-
203
+
212
204
  def reload(options = nil)
213
- clear_changes
214
- attributes = self.class.load(self.id.to_s).attributes
215
- self
205
+ clear_changes
206
+ reload_from_database or set_deleted_properties and return self
216
207
  end
217
-
208
+
218
209
  def save!
219
210
  raise RecordInvalidError.new(self) unless save
220
211
  end
@@ -234,14 +225,19 @@ module Neo4j
234
225
 
235
226
  alias :new_record? :new?
236
227
 
237
- def del
238
- @_deleted = true
239
- super
228
+ def ==(other)
229
+ new? ? self.__id__ == other.__id__ : @_java_node == (other)
240
230
  end
241
231
 
242
- def destroy
243
- _run_update_callbacks { del }
232
+ def del_with_wrapper
233
+ _run_destroy_callbacks do
234
+ del_without_wrapper
235
+ set_deleted_properties
236
+ end
244
237
  end
238
+
239
+ alias_method_chain :del, :wrapper
240
+ alias :destroy :del_with_wrapper
245
241
 
246
242
  def destroyed?()
247
243
  @_deleted
@@ -265,24 +261,24 @@ module Neo4j
265
261
  wrapped
266
262
  end
267
263
 
268
- # Behave like ActiveModel
264
+ # Behave like ActiveModel
269
265
  def all_with_args(*args)
270
- if args.empty?
271
- all_without_args
272
- else
273
- hits = find_without_checking_for_id(*args)
274
- # We need to save this so that the Rack Neo4j::Rails:LuceneConnection::Closer can close it
275
- Thread.current[:neo4j_lucene_connection] ||= []
276
- Thread.current[:neo4j_lucene_connection] << hits
277
- hits
278
- end
266
+ if args.empty?
267
+ all_without_args
268
+ else
269
+ hits = find_without_checking_for_id(*args)
270
+ # We need to save this so that the Rack Neo4j::Rails:LuceneConnection::Closer can close it
271
+ Thread.current[:neo4j_lucene_connection] ||= []
272
+ Thread.current[:neo4j_lucene_connection] << hits
273
+ hits
274
+ end
279
275
  end
280
-
276
+
281
277
  alias_method_chain :all, :args
282
278
 
283
279
  # Handle Model.find(params[:id])
284
280
  def find_with_checking_for_id(*args)
285
- if args.length == 1 && String === args[0] && args[0].to_i != 0
281
+ if args.length == 1 && String === args[0] && args[0].to_i != 0
286
282
  load(*args)
287
283
  else
288
284
  all_with_args(*args).first
@@ -300,7 +296,6 @@ module Neo4j
300
296
  end
301
297
  end
302
298
 
303
-
304
299
  alias_method :_orig_create, :create
305
300
 
306
301
  def create(*args)
@@ -308,12 +303,11 @@ module Neo4j
308
303
  end
309
304
 
310
305
  def create!(*args)
311
- new(*args).tap { |o| o.save! }
306
+ new(*args).tap { |o| o.save! }
312
307
  end
313
308
 
314
309
  tx_methods :create, :create!
315
310
 
316
-
317
311
  def transaction(&block)
318
312
  Neo4j::Rails::Transaction.run &block
319
313
  end
@@ -347,10 +341,20 @@ module Neo4j
347
341
  tx_methods("#{association_name}_attributes=")
348
342
  end
349
343
  end
350
-
351
344
  end
352
-
345
+
346
+ private
347
+ def reload_from_database
348
+ if reloaded = self.class.load(self.id.to_s)
349
+ attributes = reloaded.attributes
350
+ end
351
+ end
352
+
353
+ def set_deleted_properties
354
+ @_deleted = true
355
+ @_persisted = false
356
+ @_java_node = Neo4j::Rails::Value.new(self)
357
+ end
353
358
  end
354
-
355
359
  end
356
360
  end
@@ -0,0 +1,29 @@
1
+ module Neo4j
2
+ module Rails
3
+ # provide some properties before we have a real node or relationship
4
+ module Properties # :nodoc:
5
+ include Neo4j::Property
6
+
7
+ # override Neo4j::Property#props
8
+ def props
9
+ @props ||= {}
10
+ end
11
+
12
+ def has_property?(key)
13
+ !props[key].nil?
14
+ end
15
+
16
+ def set_property(key,value)
17
+ props[key] = value
18
+ end
19
+
20
+ def get_property(key)
21
+ props[key]
22
+ end
23
+
24
+ def remove_property(key)
25
+ props.delete(key)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,25 +1,18 @@
1
1
  module Neo4j::Rails
2
2
 
3
3
  class Value
4
- include Neo4j::Property
4
+ include Properties
5
5
  include org.neo4j.graphdb.Node
6
6
 
7
7
  def initialize(wrapper)
8
8
  @wrapper = wrapper
9
- @props = {}
10
9
  @outgoing_rels = {} # a hash of all relationship with key type
11
10
  end
12
11
 
13
- # override Neo4j::Property#props
14
- def props
15
- @props
16
- end
17
-
18
12
  def getId
19
13
  nil
20
14
  end
21
15
 
22
-
23
16
  def create_relationship_to(other_java_node, java_type)
24
17
  outgoing(java_type.name).new(other_java_node)
25
18
  end
@@ -36,23 +29,6 @@ module Neo4j::Rails
36
29
  outgoing.rels
37
30
  end
38
31
 
39
- # Pretend this object is a Java Node
40
- def has_property?(key)
41
- !@props[key].nil?
42
- end
43
-
44
- def set_property(key,value)
45
- @props[key] = value
46
- end
47
-
48
- def get_property(key)
49
- @props[key]
50
- end
51
-
52
- def remove_property(key)
53
- @props.delete(key)
54
- end
55
-
56
32
  def wrapper
57
33
  @wrapper
58
34
  end
@@ -64,10 +40,11 @@ module Neo4j::Rails
64
40
  def save_nested(root_node)
65
41
  valid = true
66
42
  @outgoing_rels.each_pair do |type, rel|
67
- rel.each do |new_node|
68
- wrapper = new_node.respond_to?(:wrapper) ? new_node.wrapper : new_node
43
+ rel.each_with_index do |new_node, i|
44
+ wrapper = new_node.respond_to?(:wrapper) ? new_node.wrapper : new_node
69
45
  if wrapper.save
70
- root_node.outgoing(type) << wrapper
46
+ new_rel = Neo4j::Relationship.new(type.to_sym, root_node, wrapper)
47
+ rel.rels[i].props.each_pair { |property, value| new_rel[property] = value }
71
48
  else
72
49
  valid = false
73
50
  end
@@ -82,7 +59,9 @@ module Neo4j::Rails
82
59
  end
83
60
 
84
61
  class Relationship
85
- include org.neo4j.graphdb.Relationship
62
+ include org.neo4j.graphdb.Relationship
63
+ include Properties
64
+
86
65
  attr_reader :end_node, :start_node
87
66
 
88
67
  def initialize(from, to)
@@ -15,7 +15,6 @@ module Neo4j
15
15
  # # modify something
16
16
  # tx.success
17
17
  # tx.finish
18
-
19
18
  def self.new(instance = Neo4j.started_db)
20
19
  instance.begin_tx
21
20
  end
data/lib/neo4j/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Neo4j
2
- VERSION = "1.0.0.beta.17"
2
+ VERSION = "1.0.0.beta.18"
3
3
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - beta
10
- - 17
11
- version: 1.0.0.beta.17
10
+ - 18
11
+ version: 1.0.0.beta.18
12
12
  platform: ruby
13
13
  authors:
14
14
  - Andreas Ronge
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-11-02 00:00:00 +01:00
19
+ date: 2010-11-04 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,9 @@ extra_rdoc_files:
59
59
  - README.rdoc
60
60
  files:
61
61
  - lib/neo4j.rb
62
+ - lib/generators/neo4j.rb
63
+ - lib/generators/neo4j/model/model_generator.rb
64
+ - lib/generators/neo4j/model/templates/model.rb
62
65
  - lib/neo4j/event_handler.rb
63
66
  - lib/neo4j/model.rb
64
67
  - lib/neo4j/node_traverser.rb
@@ -82,6 +85,7 @@ files:
82
85
  - lib/neo4j/rails/lucene_connection_closer.rb
83
86
  - lib/neo4j/rails/transaction.rb
84
87
  - lib/neo4j/rails/tx_methods.rb
88
+ - lib/neo4j/rails/properties.rb
85
89
  - lib/neo4j/rails/value.rb
86
90
  - lib/neo4j/rails/validations/uniqueness.rb
87
91
  - lib/neo4j/jars/lucene-core-3.0.1.jar
@@ -96,13 +100,12 @@ files:
96
100
  - lib/neo4j/mapping/class_methods/rule.rb
97
101
  - lib/neo4j/mapping/class_methods/init_node.rb
98
102
  - lib/neo4j/mapping/class_methods/property.rb
99
- - lib/neo4j/mapping/class_methods/index.rb
100
103
  - lib/neo4j/mapping/class_methods/root.rb
101
104
  - lib/neo4j/mapping/class_methods/init_rel.rb
102
105
  - lib/neo4j/mapping/class_methods/relationship.rb
103
106
  - lib/neo4j/index/index_registry.rb
104
- - lib/neo4j/index/wrapped_query.rb
105
107
  - lib/neo4j/index/indexer.rb
108
+ - lib/neo4j/index/lucene_query.rb
106
109
  - lib/neo4j/index/class_methods.rb
107
110
  - lib/neo4j/index/index.rb
108
111
  - README.rdoc