neo4j 1.0.0.beta.1 → 1.0.0.beta.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,8 @@ module Neo4j
20
20
  data.assigned_node_properties.each { |tx_data| property_changed(tx_data.entity, tx_data.key, tx_data.previously_commited_value, tx_data.value) }
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
+ data.created_relationships.each {|rel| relationship_created(rel)}
24
+ data.deleted_relationships.each {|rel| relationship_deleted(rel)}
23
25
  # TODO Add relationship properties callbacks
24
26
  end
25
27
 
data/lib/neo4j/index.rb CHANGED
@@ -31,6 +31,36 @@ module Neo4j
31
31
  end
32
32
  end
33
33
 
34
+ class WrappedQuery
35
+ include Enumerable
36
+
37
+ def initialize(index, query)
38
+ @index = index
39
+ @query = query
40
+ end
41
+
42
+ def each
43
+ hits.each{|n| yield n.wrapper}
44
+ end
45
+
46
+ def close
47
+ @hits.close if @hits
48
+ end
49
+
50
+ def empty?
51
+ hits.size == 0
52
+ end
53
+
54
+ def size
55
+ hits.size
56
+ end
57
+
58
+ def hits
59
+ @hits ||= @index.query(@query)
60
+ end
61
+
62
+ end
63
+
34
64
  class Indexer
35
65
  attr_reader :index_name
36
66
 
@@ -65,18 +95,20 @@ module Neo4j
65
95
  index_for_field(field).remove(entity, field, value)
66
96
  end
67
97
 
68
- def find(query, type = :exact)
98
+ def find(query, params = {})
99
+ type = params[:type] || :exact
69
100
  index = index_for_type(type)
70
- hits = index.query(query)
101
+ query = (params[:wrapped].nil? || params[:wrapped]) ? WrappedQuery.new(index, query) : index.query(query)
102
+
71
103
  if block_given?
72
104
  begin
73
- ret = yield hits
105
+ ret = yield query
74
106
  ensure
75
- hits.close if hits
107
+ query.close
76
108
  end
77
109
  ret
78
110
  else
79
- hits
111
+ query
80
112
  end
81
113
  end
82
114
 
@@ -2,8 +2,7 @@ module Neo4j::Mapping
2
2
  module ClassMethods
3
3
 
4
4
  module Relationship
5
- def decl_relationships
6
- # :nodoc:
5
+ def decl_relationships # :nodoc:
7
6
  self::DECL_RELATIONSHIPS
8
7
  end
9
8
 
@@ -2,12 +2,24 @@ module Neo4j::Mapping
2
2
  module ClassMethods
3
3
  class Rules
4
4
  class << self
5
- def add(clazz, field, &block)
5
+ def add(clazz, field, props, &block)
6
6
  clazz = clazz.to_s
7
7
  @rules ||= {}
8
8
  @rules[clazz] ||= {}
9
- filter = block.nil? ? Proc.new{|*| true} : block
9
+ filter = block.nil? ? Proc.new { |*| true } : block
10
10
  @rules[clazz][field] = filter
11
+ @triggers ||= {}
12
+ @triggers[clazz] ||= {}
13
+ trigger = props[:trigger].nil? ? [] : props[:trigger]
14
+ @triggers[clazz][field] = trigger.respond_to?(:each) ? trigger : [trigger]
15
+ end
16
+
17
+ def trigger_other_rules(node, field)
18
+ clazz = node[:_classname]
19
+ rel_types = @triggers[clazz][field]
20
+ rel_types.each do |rel_type|
21
+ node.incoming(rel_type).each { |n| n.trigger_rules }
22
+ end
11
23
  end
12
24
 
13
25
  def fields_for(clazz)
@@ -31,7 +43,7 @@ module Neo4j::Mapping
31
43
 
32
44
  def create_rules
33
45
  @rules.each_key do |clazz|
34
- # check if rule nodes exist, if note create them
46
+ # check if rule nodes exist, if not create them
35
47
  if !Neo4j.ref_node.rel?(clazz)
36
48
  Neo4j::Transaction.run do
37
49
  node = Neo4j::Node.new
@@ -52,28 +64,52 @@ module Neo4j::Mapping
52
64
  end
53
65
 
54
66
 
55
- def on_property_changed(node, key, old_value, new_value)
56
- return unless trigger?(node)
67
+ def on_relationship_created(rel)
68
+ trigger_start_node = trigger?(rel.start_node)
69
+ trigger_end_node = trigger?(rel.end_node)
70
+ # end or start node must be triggered by this event
71
+ return unless trigger_start_node || trigger_end_node
72
+ on_property_changed(trigger_start_node ? rel.start_node : rel.end_node)
73
+ end
74
+
75
+
76
+ def on_property_changed(node, *)
77
+ trigger_rules(node) if trigger?(node)
78
+ end
79
+
80
+ def trigger_rules(node)
57
81
  clazz = node[:_classname]
58
82
  return if @rules[clazz].nil?
83
+
59
84
  agg_node = rule_for(node[:_classname])
60
- @rules[clazz].each_pair do |field, filter|
61
- if filter.call(node)
85
+ @rules[clazz].each_pair do |field, rule|
86
+ if run_rule(rule, node)
62
87
  # is this node already included ?
63
88
  if !node.rel?(field)
64
89
  agg_node.outgoing(field) << node
90
+ trigger_other_rules(node, field)
65
91
  end
66
92
  else
67
93
  # remove old ?
68
94
  node.rels(field).incoming.each { |x| x.del }
95
+ trigger_other_rules(node, field)
69
96
  end
70
97
  end
71
98
  end
99
+
100
+ def run_rule(rule, node)
101
+ if rule.arity != 1
102
+ wrapper = Neo4j::Node.load_wrapper(node)
103
+ wrapper.instance_eval(&rule)
104
+ else
105
+ rule.call(node)
106
+ end
107
+ end
72
108
  end
73
109
  end
74
110
 
75
111
 
76
- module Aggregate
112
+ module Rule
77
113
 
78
114
  # Creates an rule node attached to the Neo4j.ref_node
79
115
  # Can be used to rule all instances of a specific Ruby class.
@@ -93,7 +129,7 @@ module Neo4j::Mapping
93
129
  # Person.young # => [p1,p2]
94
130
  # p1.young? # => true
95
131
  #
96
- def rule(name, &block)
132
+ def rule(name, props = {}, &block)
97
133
  singelton = class << self;
98
134
  self;
99
135
  end
@@ -116,18 +152,25 @@ module Neo4j::Mapping
116
152
  instance_eval &block
117
153
  end
118
154
 
119
- Rules.add(self, name, &block)
155
+ Rules.add(self, name, props, &block)
120
156
  end
121
157
 
122
158
  # This is typically used for RSpecs to clean up rule nodes created by the #rule method.
123
159
  # It also remove the given class method.
124
160
  def delete_rules
125
- singelton = class << self; self; end
161
+ singelton = class << self;
162
+ self;
163
+ end
126
164
  Rules.fields_for(self).each do |name|
127
165
  singelton.send(:remove_method, name)
128
166
  end
129
167
  Rules.delete(self)
130
168
  end
169
+
170
+ def trigger_rules(node)
171
+ Rules.trigger_rules(node)
172
+ end
173
+
131
174
  end
132
175
 
133
176
  Neo4j.unstarted_db.event_handler.add(Rules)
@@ -6,7 +6,7 @@ module Neo4j::Mapping
6
6
 
7
7
  def_delegators :@_java_node, :[]=, :[], :property?, :props, :attributes, :update, :neo_id, :id, :rels, :rel?, :to_param, :getId,
8
8
  :rel, :del, :list?, :print, :print_sub, :outgoing, :incoming, :both,
9
- :equal?, :eql?, :==, :exist?
9
+ :equal?, :eql?, :==, :exist?, :getRelationships
10
10
 
11
11
 
12
12
  # --------------------------------------------------------------------------
@@ -35,10 +35,11 @@ module Neo4j::Mapping
35
35
  @_java_node
36
36
  end
37
37
 
38
- def id
39
- @_java_node.id
38
+ def trigger_rules
39
+ self.class.trigger_rules(self)
40
40
  end
41
41
 
42
+
42
43
  def self.included(c) # :nodoc:
43
44
  c.instance_eval do
44
45
  # these constants are used in the Neo4j::RelClassMethods and Neo4j::PropertyClassMethods
@@ -49,13 +50,14 @@ module Neo4j::Mapping
49
50
  const_set(:DECL_RELATIONSHIPS, {})
50
51
  const_set(:PROPERTIES_INFO, {})
51
52
  end
53
+
52
54
  class << self
53
55
  alias_method :orig_new, :new
54
56
  end
55
57
  end
56
58
  c.extend ClassMethods::Property
57
59
  c.extend ClassMethods::Relationship
58
- c.extend ClassMethods::Aggregate
60
+ c.extend ClassMethods::Rule
59
61
  c.extend Neo4j::Index::ClassMethods
60
62
  def c.inherited(subclass)
61
63
  subclass.indexer subclass
data/lib/neo4j/node.rb CHANGED
@@ -23,6 +23,13 @@ module Neo4j
23
23
  def wrapped_entity
24
24
  self
25
25
  end
26
+
27
+ # Loads the Ruby wrapper for this node
28
+ # If there is no _classname property for this node then it will simply return itself.
29
+ # Same as Neo4j::Node.load_wrapper(node)
30
+ def wrapper
31
+ self.class.load_wrapper(self)
32
+ end
26
33
 
27
34
  def class
28
35
  Neo4j::Node
data/lib/neo4j/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Neo4j
2
- VERSION = "1.0.0.beta.1"
2
+ VERSION = "1.0.0.beta.2"
3
3
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - beta
10
- - 1
11
- version: 1.0.0.beta.1
10
+ - 2
11
+ version: 1.0.0.beta.2
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-09-21 00:00:00 +02:00
19
+ date: 2010-09-23 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency