neo4j 1.0.0.beta.8 → 1.0.0.beta.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,7 +37,7 @@ module Neo4j
37
37
  def each_node
38
38
  iter = @graph.all_nodes.iterator
39
39
  while (iter.hasNext)
40
- yield Node.load_wrapper(iter.next)
40
+ yield Node.wrapper(iter.next)
41
41
  end
42
42
  end
43
43
 
@@ -21,8 +21,9 @@ module Neo4j
21
21
  data.removed_node_properties.each { |tx_data| property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, nil) unless data.deleted_nodes.include?(tx_data.entity) }
22
22
  data.deleted_nodes.each { |node| node_deleted(node, deleted_properties_for(node,data))}
23
23
  data.created_relationships.each {|rel| relationship_created(rel)}
24
- data.deleted_relationships.each {|rel| relationship_deleted(rel)}
25
- # TODO Add relationship properties callbacks
24
+ data.deleted_relationships.each {|rel| relationship_deleted(rel, deleted_rel_properties_for(rel, data))}
25
+ data.assigned_relationship_properties.each { |tx_data| rel_property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, tx_data.value) }
26
+ data.removed_relationship_properties.each {|tx_data| rel_property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, nil) unless data.deleted_relationships.include?(tx_data.entity) }
26
27
  end
27
28
 
28
29
  def deleted_properties_for(node, data)
@@ -32,6 +33,13 @@ module Neo4j
32
33
  end
33
34
  end
34
35
 
36
+ def deleted_rel_properties_for(rel, data)
37
+ data.removed_relationship_properties.find_all{|tx_data| tx_data.entity == rel}.inject({}) do |memo, tx_data|
38
+ memo[tx_data.key] = tx_data.previously_commited_value
39
+ memo
40
+ end
41
+ end
42
+
35
43
  def add(listener)
36
44
  @listeners << listener unless @listeners.include?(listener)
37
45
  end
@@ -69,15 +77,19 @@ module Neo4j
69
77
  @listeners.each {|li| li.on_relationship_created(relationship) if li.respond_to?(:on_relationship_created)}
70
78
  end
71
79
 
72
- def relationship_deleted(relationship)
73
- @listeners.each {|li| li.on_relationship_deleted(relationship) if li.respond_to?(:on_relationship_deleted)}
80
+ def relationship_deleted(relationship, old_props)
81
+ @listeners.each {|li| li.on_relationship_deleted(relationship, old_props) if li.respond_to?(:on_relationship_deleted)}
74
82
  end
75
83
 
76
84
  def property_changed(node, key, old_value, new_value)
77
85
  @listeners.each {|li| li.on_property_changed(node, key, old_value, new_value) if li.respond_to?(:on_property_changed)}
78
86
  end
79
87
 
80
- # TODO
88
+ def rel_property_changed(rel, key, old_value, new_value)
89
+ @listeners.each {|li| li.on_rel_property_changed(rel, key, old_value, new_value) if li.respond_to?(:on_rel_property_changed)}
90
+ end
91
+
92
+ # TODO ?
81
93
  def tx_finished(tx)
82
94
  @listeners.each {|li| li.on_tx_finished(tx) if li.respond_to?(:on_tx_finished)}
83
95
  end
data/lib/neo4j/index.rb CHANGED
@@ -14,9 +14,20 @@ module Neo4j
14
14
 
15
15
  def_delegators :@indexer, :index, :find, :index?, :index_type?, :clear_index_type, :rm_index_type, :add_index, :rm_index, :index_type_for, :index_name
16
16
 
17
- # Sets which indexer should be used for the given class.
17
+
18
+ # Sets which indexer should be used for the given node class
19
+ # Returns the old one if there was an old indexer.
20
+ def node_indexer(clazz)
21
+ indexer(clazz, :node)
22
+ end
23
+
24
+ # Sets which indexer should be used for the given relationship class
18
25
  # Returns the old one if there was an old indexer.
19
- def indexer(clazz)
26
+ def rel_indexer(clazz)
27
+ indexer(clazz, :rel)
28
+ end
29
+
30
+ def indexer(clazz, type) #:nodoc:
20
31
  old = @indexer
21
32
  @@indexers ||= {}
22
33
  if @@indexers.include?(clazz)
@@ -24,7 +35,7 @@ module Neo4j
24
35
  @indexer = @@indexers[clazz]
25
36
  @indexer.include_trigger(self)
26
37
  else
27
- @indexer = Indexer.new(clazz)
38
+ @indexer = Indexer.new(clazz, type)
28
39
  @@indexers[clazz] = @indexer
29
40
  end
30
41
  old
@@ -84,9 +95,12 @@ module Neo4j
84
95
  class Indexer
85
96
  attr_reader :index_name
86
97
 
87
- def initialize(clazz)
98
+ def initialize(clazz, type)
88
99
  @index_name = clazz.to_s
89
100
 
101
+ # do we want to index nodes or relationships ?
102
+ @type = type
103
+
90
104
  @indexes = {} # key = type, value = java neo4j index
91
105
  @field_types = {} # key = field, value = type (e.g. :exact or :fulltext)
92
106
  @triggered_by = clazz.to_s
@@ -174,7 +188,11 @@ module Neo4j
174
188
  def create_index_with(type)
175
189
  db=Neo4j.started_db
176
190
  index_config = lucene_config(type)
177
- db.lucene.node_index("#{@index_name}-#{type}", index_config)
191
+ if @type == :node
192
+ db.lucene.node_index("#{@index_name}-#{type}", index_config)
193
+ else
194
+ db.lucene.relationship_index("#{@index_name}-#{type}", index_config)
195
+ end
178
196
  end
179
197
 
180
198
 
@@ -186,7 +204,7 @@ module Neo4j
186
204
  end
187
205
 
188
206
  def trigger?(classname)
189
- @triggered_by.include?(classname || 'Neo4j::Node')
207
+ @triggered_by.include?(classname || (@type==:node ? 'Neo4j::Node' : 'Neo4j::Relationship'))
190
208
  end
191
209
 
192
210
  def on_node_created(node)
@@ -195,7 +213,7 @@ module Neo4j
195
213
  end
196
214
 
197
215
  def on_node_deleted(node, old_props)
198
- return unless @triggered_by.include?(old_props['_classname'] || 'Neo4j::Node')
216
+ return unless trigger?(old_props['_classname'])
199
217
  @field_types.keys.each {|field| rm_index(node, field, old_props[field]) if old_props[field]}
200
218
  end
201
219
 
@@ -207,6 +225,22 @@ module Neo4j
207
225
  # add index
208
226
  add_index(node, field, new_val) if new_val
209
227
  end
228
+
229
+ def on_rel_property_changed(rel, field, old_val, new_val)
230
+ # works exactly like for nodes
231
+ on_property_changed(rel, field, old_val, new_val)
232
+ end
233
+
234
+ def on_relationship_created(rel)
235
+ # works exactly like for nodes
236
+ on_node_created(rel)
237
+ end
238
+
239
+ def on_relationship_deleted(rel, old_props)
240
+ # works exactly like for nodes
241
+ on_node_deleted(rel, old_props)
242
+ end
243
+
210
244
  end
211
245
 
212
246
  end
data/lib/neo4j/load.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Neo4j
2
+ module Load
3
+ def wrapper(node)
4
+ return node unless node.property?(:_classname)
5
+ to_class(node[:_classname]).load_wrapper(node)
6
+ end
7
+
8
+ def to_class(class_name)
9
+ class_name.split("::").inject(Kernel) {|container, name| container.const_get(name.to_s) }
10
+ end
11
+
12
+ def exist?(node_or_node_id, db = Neo4j.started_db)
13
+ id = node_or_node_id.kind_of?(Fixnum) ? node_or_node_id : node_or_node_id.id
14
+ load(id, db) != nil
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,50 @@
1
+ module Neo4j::Mapping
2
+ module ClassMethods
3
+ module InitNode
4
+
5
+ def load_wrapper(node)
6
+ wrapped_node = self.orig_new
7
+ wrapped_node.init_on_load(node)
8
+ wrapped_node
9
+ end
10
+
11
+
12
+ # Creates a new node or loads an already existing Neo4j node.
13
+ #
14
+ # You can use two callback method to initialize the node
15
+ # init_on_load:: this method is called when the node is loaded from the database
16
+ # init_on_create:: called when the node is created, will be provided with the same argument as the new method
17
+ #
18
+ #
19
+ # Does
20
+ # * sets the neo4j property '_classname' to self.class.to_s
21
+ # * creates a neo4j node java object (in @_java_node)
22
+ #
23
+ # If you want to provide your own initialize method you should instead implement the
24
+ # method init_on_create method.
25
+ #
26
+ # === Example
27
+ #
28
+ # class MyNode
29
+ # include Neo4j::NodeMixin
30
+ #
31
+ # def init_on_create(name, age)
32
+ # self[:name] = name
33
+ # self[:age] = age
34
+ # end
35
+ # end
36
+ #
37
+ # node = MyNode.new('jimmy', 23)
38
+ #
39
+ def new(*args)
40
+ node = Neo4j::Node.create
41
+ wrapped_node = super()
42
+ wrapped_node.init_on_load(node)
43
+ wrapped_node.init_on_create(*args)
44
+ wrapped_node
45
+ end
46
+
47
+ alias_method :create, :new
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ module Neo4j::Mapping
2
+ module ClassMethods
3
+ module InitRel
4
+ def load_wrapper(rel)
5
+ wrapped_rel = self.orig_new
6
+ wrapped_rel.init_on_load(rel)
7
+ wrapped_rel
8
+ end
9
+
10
+
11
+ # Creates a relationship between given nodes.
12
+ #
13
+ # You can use two callback method to initialize the relationship
14
+ # init_on_load:: this method is called when the relationship is loaded from the database
15
+ # init_on_create:: called when the relationship is created, will be provided with the same argument as the new method
16
+ #
17
+ # ==== Parameters (when creating a new relationship in db)
18
+ # type:: the key and value to be set
19
+ # from_node:: create relationship from this node
20
+ # to_node:: create relationship to this node
21
+ # props:: optional hash of properties to initialize the create relationship with
22
+ #
23
+ def new(*args)
24
+ type, from_node, to_node, props = args
25
+ rel = Neo4j::Relationship.create(type, from_node, to_node)
26
+ wrapped_rel = super()
27
+ wrapped_rel.init_on_load(rel)
28
+ wrapped_rel.init_on_create(*args)
29
+ wrapped_rel
30
+ end
31
+
32
+ alias_method :create, :new
33
+ end
34
+ end
35
+ end
@@ -61,61 +61,6 @@ module Neo4j::Mapping
61
61
  _decl_props[prop_name.to_sym][:defined] == true
62
62
  end
63
63
 
64
-
65
- def load
66
- # TODO
67
- node = db.graph.get_node_by_id(node_id.to_i)
68
- clazz = Neo4j::Node.to_class(node[:_classname])
69
- raise "Expected classname #{self} got #{clazz}" if clazz != self
70
- end
71
-
72
- def load_wrapper(node, db = Neo4j.started_db)
73
- wrapped_node = self.orig_new # avo
74
- #id creating a new node, only a Ruby Object
75
- wrapped_node.init_on_load(node)
76
- wrapped_node
77
- end
78
-
79
-
80
- # Creates a new node or loads an already existing Neo4j node.
81
- #
82
- # Does
83
- # * sets the neo4j property '_classname' to self.class.to_s
84
- # * creates a neo4j node java object (in @_java_node)
85
- # * calls init_node if that is defined in the current class.
86
- #
87
- # If you want to provide your own initialize method you should instead implement the
88
- # method init_node method.
89
- #
90
- # === Example
91
- #
92
- # class MyNode
93
- # include Neo4j::NodeMixin
94
- #
95
- # def init_node(name, age)
96
- # self[:name] = name
97
- # self[:age] = age
98
- # end
99
- # end
100
- #
101
- # node = MyNode.new('jimmy', 23)
102
- # # notice the following is still possible:
103
- # node = MyNode :name => 'jimmy', :age => 12
104
- #
105
- # The init_node is only called when the node is created in the database.
106
- # The initialize method is used both for to purposes:
107
- # loading an already existing node from the Neo4j database and creating a new node in the database.
108
- #
109
- def new(*args)
110
- node = Neo4j::Node.create
111
- wrapped_node = super()
112
- wrapped_node.init_on_load(node)
113
- wrapped_node.init_on_create(*args)
114
- wrapped_node
115
- end
116
-
117
- alias_method :create, :new
118
-
119
64
  end
120
65
  end
121
- end
66
+ end
@@ -100,7 +100,7 @@ module Neo4j::Mapping
100
100
 
101
101
  def run_rule(rule, node)
102
102
  if rule.arity != 1
103
- wrapper = Neo4j::Node.load_wrapper(node)
103
+ wrapper = Neo4j::Node.wrapper(node)
104
104
  wrapper.instance_eval(&rule)
105
105
  else
106
106
  rule.call(node)
@@ -6,6 +6,7 @@ module Neo4j
6
6
  #
7
7
  class HasN
8
8
  include Enumerable
9
+ include ToJava
9
10
 
10
11
  def initialize(node, dsl) # :nodoc:
11
12
  @node = node
@@ -64,6 +65,7 @@ module Neo4j
64
65
 
65
66
 
66
67
  # Creates a relationship instance between this and the other node.
68
+ # Returns the relationship object
67
69
  def new(other)
68
70
  create_rel(@node, other)
69
71
  end
@@ -79,11 +81,6 @@ module Neo4j
79
81
  #
80
82
  # n1 << n2 << n3
81
83
  #
82
- # This is the same as:
83
- #
84
- # n1.add_rel(:friends, n2)
85
- # n1.add_rel(:friends, n3)
86
- #
87
84
  # ==== Returns
88
85
  # self
89
86
  #
@@ -101,14 +98,16 @@ module Neo4j
101
98
  from, to = other, node
102
99
  end
103
100
 
104
- rel = from.outgoing(@dsl.namespace_type) << to
105
- # rel[_classname] = @dsl.relationship_class # TODO
101
+ java_type = type_to_java(@dsl.namespace_type)
102
+ rel = from._java_node.create_relationship_to(to._java_node, java_type)
103
+ rel[:_classname] = @dsl.relationship_class.to_s if @dsl.relationship_class
106
104
 
105
+ # TODO - not implemented yet
107
106
  # the from.neo_id is only used for cascade_delete_incoming since that node will be deleted when all the list items has been deleted.
108
107
  # if cascade_delete_outgoing all nodes will be deleted when the root node is deleted
109
108
  # if cascade_delete_incoming then the root node will be deleted when all root nodes' outgoing nodes are deleted
110
- rel[@dsl.cascade_delete_prop_name] = node.neo_id if @dsl.cascade_delete?
111
- rel
109
+ #rel[@dsl.cascade_delete_prop_name] = node.neo_id if @dsl.cascade_delete?
110
+ rel.wrapper
112
111
  end
113
112
 
114
113
  end
@@ -26,7 +26,7 @@ module Neo4j::Mapping
26
26
  def init_on_create(*args) # :nodoc:
27
27
  self[:_classname] = self.class.to_s
28
28
  if args[0].respond_to?(:each_pair)
29
- args[0].each_pair { |k, v| @_java_node.set_property(k.to_s, v) }
29
+ args[0].each_pair { |k, v| @_java_node.set_property(k.to_s, v) }
30
30
  end
31
31
  end
32
32
 
@@ -46,17 +46,21 @@ module Neo4j::Mapping
46
46
  alias_method :orig_new, :new
47
47
  end
48
48
  end
49
+
49
50
  c.extend ClassMethods::Root
50
51
  c.extend ClassMethods::Property
52
+ c.extend ClassMethods::InitNode
51
53
  c.extend ClassMethods::Relationship
52
54
  c.extend ClassMethods::Rule
53
55
  c.extend Neo4j::Index::ClassMethods
56
+
54
57
  def c.inherited(subclass)
55
- subclass.indexer subclass
58
+ subclass.node_indexer subclass
56
59
  subclass.root_class subclass
57
60
  super
58
61
  end
59
- c.indexer c
62
+
63
+ c.node_indexer c
60
64
  c.root_class c
61
65
  end
62
66
  end
@@ -0,0 +1,117 @@
1
+ module Neo4j::Mapping
2
+
3
+ module RelationshipMixin
4
+ extend Forwardable
5
+ include Neo4j::Index
6
+
7
+ def_delegators :@_java_rel, :[]=, :[], :property?, :props, :attributes, :update, :neo_id, :id, :to_param, :getId,
8
+ :equal?, :eql?, :==, :delete, :getStartNode, :getEndNode, :getOtherNode
9
+
10
+
11
+
12
+ # --------------------------------------------------------------------------
13
+ # Initialization methods
14
+ #
15
+
16
+
17
+ # Init this node with the specified java neo4j relationship.
18
+ #
19
+ def init_on_load(java_rel) # :nodoc:
20
+ @_java_rel = java_rel
21
+ end
22
+
23
+
24
+ # Creates a new node and initialize with given properties.
25
+ #
26
+ def init_on_create(*args) # :nodoc:
27
+ type, from_node, to_node, props = args
28
+ self[:_classname] = self.class.to_s
29
+ if props.respond_to?(:each_pair)
30
+ props.each_pair { |k, v| @_java_rel.set_property(k.to_s, v) }
31
+ end
32
+ end
33
+
34
+
35
+ # --------------------------------------------------------------------------
36
+ # Instance Methods
37
+ #
38
+
39
+ # Returns the org.neo4j.graphdb.Relationship wrapped object
40
+ def _java_rel
41
+ @_java_rel
42
+ end
43
+
44
+ # Returns the end node of this relationship
45
+ def end_node
46
+ id = getEndNode.getId
47
+ Neo4j::Node.load(id)
48
+ end
49
+
50
+ # Returns the start node of this relationship
51
+ def start_node
52
+ id = getStartNode.getId
53
+ Neo4j::Node.load(id)
54
+ end
55
+
56
+ # Deletes this relationship
57
+ def del
58
+ delete
59
+ end
60
+
61
+ def exist?
62
+ Neo4j::Relationship.load(neo_id) != nil
63
+ end
64
+
65
+ # A convenience operation that, given a node that is attached to this relationship, returns the other node.
66
+ # For example if node is a start node, the end node will be returned, and vice versa.
67
+ # This is a very convenient operation when you're manually traversing the node space by invoking one of the #rels operations on node.
68
+ #
69
+ # This operation will throw a runtime exception if node is neither this relationship's start node nor its end node.
70
+ #
71
+ # ==== Example
72
+ # For example, to get the node "at the other end" of a relationship, use the following:
73
+ # Node endNode = node.rel(:some_rel_type).other_node(node)
74
+ #
75
+ def other_node(node)
76
+ neo_node = node.respond_to?(:_java_node)? node._java_node : node
77
+ id = getOtherNode(neo_node).getId
78
+ Neo4j::Node.load(id)
79
+ end
80
+
81
+
82
+ # Returns the neo relationship type that this relationship is used in.
83
+ # (see java API org.neo4j.graphdb.Relationship#getType and org.neo4j.graphdb.RelationshipType)
84
+ #
85
+ # ==== Returns
86
+ # the relationship type (of type Symbol)
87
+ #
88
+ def relationship_type
89
+ get_type.name.to_sym
90
+ end
91
+
92
+ # --------------------------------------------------------------------------
93
+ # Class Methods
94
+ #
95
+
96
+ class << self
97
+ def included(c) # :nodoc:
98
+ c.instance_eval do
99
+ class << self
100
+ alias_method :orig_new, :new
101
+ end
102
+ end
103
+ c.extend ClassMethods::Root
104
+ c.extend ClassMethods::Property
105
+ c.extend ClassMethods::InitRel
106
+ c.extend Neo4j::Index::ClassMethods
107
+
108
+ def c.inherited(subclass)
109
+ subclass.rel_indexer subclass
110
+ super
111
+ end
112
+
113
+ c.rel_indexer c
114
+ end
115
+ end
116
+ end
117
+ end
data/lib/neo4j/node.rb CHANGED
@@ -19,16 +19,21 @@ module Neo4j
19
19
  Neo4j::Node.exist?(self)
20
20
  end
21
21
 
22
- # provide a hook for ruby class mapping
22
+ # same as _java_node
23
+ # Used so that we have same method for both relationship and nodes
23
24
  def wrapped_entity
24
25
  self
25
26
  end
26
27
 
27
28
  # Loads the Ruby wrapper for this node
28
29
  # If there is no _classname property for this node then it will simply return itself.
29
- # Same as Neo4j::Node.load_wrapper(node)
30
+ # Same as Neo4j::Node.wrapper(node)
30
31
  def wrapper
31
- self.class.load_wrapper(self)
32
+ self.class.wrapper(self)
33
+ end
34
+
35
+ def _java_node
36
+ self
32
37
  end
33
38
 
34
39
  def class
@@ -40,10 +45,10 @@ module Neo4j
40
45
  class Node
41
46
  extend Neo4j::Index::ClassMethods
42
47
 
43
- self.indexer self
48
+ self.node_indexer self
44
49
 
45
50
  class << self
46
-
51
+ include Neo4j::Load
47
52
 
48
53
  # Creates a new node using the default db instance when given no args
49
54
  # Same as Neo4j::Node#create
@@ -63,26 +68,13 @@ module Neo4j
63
68
  alias_method :create, :new
64
69
 
65
70
  def load(node_id, db = Neo4j.started_db)
66
- load_wrapper(db.graph.get_node_by_id(node_id.to_i))
71
+ wrapper(db.graph.get_node_by_id(node_id.to_i))
67
72
  rescue java.lang.IllegalStateException
68
73
  nil # the node has been deleted
69
74
  rescue org.neo4j.graphdb.NotFoundException
70
75
  nil
71
76
  end
72
77
 
73
- def load_wrapper(node)
74
- return node unless node.property?(:_classname)
75
- to_class(node[:_classname]).load_wrapper(node)
76
- end
77
-
78
- def to_class(class_name)
79
- class_name.split("::").inject(Kernel) {|container, name| container.const_get(name.to_s) }
80
- end
81
-
82
- def exist?(node_or_node_id, db = Neo4j.started_db)
83
- id = node_or_node_id.kind_of?(Fixnum) ? node_or_node_id : node_or_node_id.id
84
- load(id, db) != nil
85
- end
86
78
 
87
79
  end
88
80
  end
@@ -121,7 +121,7 @@ module Neo4j
121
121
  def each
122
122
  iter = iterator
123
123
  while (iter.hasNext) do
124
- yield Neo4j::Node.load_wrapper(iter.next)
124
+ yield Neo4j::Node.wrapper(iter.next)
125
125
  end
126
126
  end
127
127
 
@@ -9,18 +9,40 @@ module Neo4j
9
9
  end
10
10
 
11
11
  def end_node # :nodoc:
12
- Neo4j::Node.load_wrapper(getEndNode)
12
+ getEndNode.wrapper
13
13
  end
14
14
 
15
15
  def start_node # :nodoc:
16
- Neo4j::Node.load_wrapper(getStartNode)
16
+ getStartNode.wrapper
17
17
  end
18
18
 
19
19
  def other_node(node) # :nodoc:
20
- java_node = node.respond_to?(:_java_node)? node._java_node : node
21
- other_node = getOtherNode(java_node)
22
- Neo4j::Node.load_wrapper(other_node)
20
+ getOtherNode(node._java_node).wrapper
23
21
  end
22
+
23
+
24
+ # same as _java_rel
25
+ # Used so that we have same method for both relationship and nodes
26
+ def wrapped_entity
27
+ self
28
+ end
29
+
30
+ def _java_rel
31
+ self
32
+ end
33
+
34
+ # Loads the Ruby wrapper for this node
35
+ # If there is no _classname property for this node then it will simply return itself.
36
+ # Same as Neo4j::Node.load_wrapper(node)
37
+ def wrapper
38
+ self.class.wrapper(self)
39
+ end
40
+
41
+
42
+ def class
43
+ Neo4j::Relationship
44
+ end
45
+
24
46
  end
25
47
 
26
48
  #
@@ -57,7 +79,15 @@ module Neo4j
57
79
  # (Those mixin are actually not included in the Neo4j::Relationship but instead directly included in the java class org.neo4j.kernel.impl.core.RelationshipProxy)
58
80
  #
59
81
  class Relationship
82
+ extend Neo4j::Index::ClassMethods
83
+
84
+ self.rel_indexer self
85
+
60
86
  class << self
87
+ include Neo4j::Load
88
+ include Neo4j::ToJava
89
+
90
+
61
91
  # Returns a org.neo4j.graphdb.Relationship java object (!)
62
92
  # Will trigger a event that the relationship was created.
63
93
  #
@@ -74,11 +104,24 @@ module Neo4j
74
104
  #
75
105
  # Neo4j::Relationship.new :friend, node1, node2, :since => '2001-01-02', :status => 'okey'
76
106
  #
77
- def new(type, from_node, to_node, props={})
78
- rel = from_node.outgoing(type).new(to_node)
79
- props.each_pair {|k,v| rel[k] = v}
107
+ def new(type, from_node, to_node, props=nil)
108
+ java_type = type_to_java(type)
109
+ rel = from_node._java_node.create_relationship_to(to_node._java_node, java_type)
110
+ props.each_pair {|k,v| rel[k] = v} if props
80
111
  rel
81
112
  end
113
+
114
+ # create is the same as new
115
+ alias_method :create, :new
116
+
117
+ def load(rel_id, db = Neo4j.started_db)
118
+ wrapper(db.graph.get_relationship_by_id(rel_id.to_i))
119
+ rescue java.lang.IllegalStateException
120
+ nil # the node has been deleted
121
+ rescue org.neo4j.graphdb.NotFoundException
122
+ nil
123
+ end
124
+
82
125
  end
83
126
 
84
127
  end
@@ -0,0 +1,4 @@
1
+ module Neo4j
2
+ # make an alias so that we don't have to write the long name Neo4j::Mapping::Relationship
3
+ RelationshipMixin = Neo4j::Mapping::RelationshipMixin
4
+ end
@@ -18,7 +18,7 @@ module Neo4j
18
18
  def each
19
19
  iter = iterator
20
20
  while (iter.hasNext) do
21
- yield iter.next
21
+ yield iter.next.wrapper
22
22
  end
23
23
  end
24
24
 
data/lib/neo4j/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Neo4j
2
- VERSION = "1.0.0.beta.8"
2
+ VERSION = "1.0.0.beta.9"
3
3
  end
data/lib/neo4j.rb CHANGED
@@ -20,10 +20,13 @@ require 'neo4j/node_traverser'
20
20
  require 'neo4j/property'
21
21
  require 'neo4j/transaction'
22
22
  require 'neo4j/node_relationship'
23
+ require 'neo4j/load'
23
24
  require 'neo4j/relationship'
24
25
  require 'neo4j/node'
25
26
  require 'neo4j/config'
26
27
  require 'neo4j/neo4j'
28
+ require 'neo4j/mapping/class_methods/init_node'
29
+ require 'neo4j/mapping/class_methods/init_rel'
27
30
  require 'neo4j/mapping/class_methods/root'
28
31
  require 'neo4j/mapping/class_methods/property'
29
32
  require 'neo4j/mapping/class_methods/index'
@@ -31,7 +34,9 @@ require 'neo4j/mapping/class_methods/relationship'
31
34
  require 'neo4j/mapping/decl_relationship_dsl'
32
35
  require 'neo4j/mapping/has_n'
33
36
  require 'neo4j/mapping/node_mixin'
37
+ require 'neo4j/mapping/relationship_mixin'
34
38
  require 'neo4j/node_mixin'
39
+ require 'neo4j/relationship_mixin'
35
40
  require 'neo4j/mapping/class_methods/rule'
36
41
 
37
42
  # rails
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - beta
10
- - 8
11
- version: 1.0.0.beta.8
10
+ - 9
11
+ version: 1.0.0.beta.9
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-10-04 00:00:00 +02:00
19
+ date: 2010-10-05 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -166,6 +166,8 @@ files:
166
166
  - lib/neo4j/node.rb
167
167
  - lib/neo4j/node_mixin.rb
168
168
  - lib/neo4j/property.rb
169
+ - lib/neo4j/relationship_mixin.rb
170
+ - lib/neo4j/load.rb
169
171
  - lib/neo4j/database.rb
170
172
  - lib/neo4j/index.rb
171
173
  - lib/neo4j/version.rb
@@ -186,10 +188,13 @@ files:
186
188
  - lib/neo4j/mapping/decl_relationship_dsl.rb
187
189
  - lib/neo4j/mapping/node_mixin.rb
188
190
  - lib/neo4j/mapping/has_n.rb
191
+ - lib/neo4j/mapping/relationship_mixin.rb
189
192
  - lib/neo4j/mapping/class_methods/rule.rb
193
+ - lib/neo4j/mapping/class_methods/init_node.rb
190
194
  - lib/neo4j/mapping/class_methods/property.rb
191
195
  - lib/neo4j/mapping/class_methods/index.rb
192
196
  - lib/neo4j/mapping/class_methods/root.rb
197
+ - lib/neo4j/mapping/class_methods/init_rel.rb
193
198
  - lib/neo4j/mapping/class_methods/relationship.rb
194
199
  - README.rdoc
195
200
  - CHANGELOG