neo4j 1.0.0.beta.4 → 1.0.0.beta.5

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.
@@ -44,7 +44,7 @@ module Neo4j::Mapping
44
44
  c.instance_eval do
45
45
  # these constants are used in the Neo4j::RelClassMethods and Neo4j::PropertyClassMethods
46
46
  # they are defined here since they should only be defined once -
47
- # all subclasses share the same index, declared properties and index_updaters
47
+ # all subclasses share the same index and declared properties
48
48
  unless c.const_defined?(:DECL_RELATIONSHIPS)
49
49
  const_set(:ROOT_CLASS, self)
50
50
  const_set(:DECL_RELATIONSHIPS, {})
@@ -64,9 +64,6 @@ module Neo4j::Mapping
64
64
  super
65
65
  end
66
66
  c.indexer c
67
-
68
67
  end
69
-
70
-
71
68
  end
72
69
  end
@@ -2,6 +2,7 @@ class Neo4j::Model
2
2
  include Neo4j::NodeMixin
3
3
  include ActiveModel::Validations
4
4
  include ActiveModel::Dirty
5
+ include ActiveModel::MassAssignmentSecurity
5
6
  extend ActiveModel::Naming
6
7
  extend ActiveModel::Callbacks
7
8
  define_model_callbacks :create, :save, :update, :destroy
@@ -23,12 +24,21 @@ class Neo4j::Model
23
24
  end
24
25
 
25
26
  def init_on_create(*args) # :nodoc:
26
- _run_create_callbacks do
27
- @_new_record = true
28
- super
27
+ if Neo4j::Rails::Transaction.running?
28
+ super()
29
+ init_on_create_in_tx(*args)
30
+ else
31
+ Neo4j::Rails::Transaction.run { super(); init_on_create_in_tx(*args) }
29
32
  end
30
33
  end
31
34
 
35
+ def init_on_create_in_tx(*args)
36
+ _run_save_callbacks do
37
+ _run_create_callbacks do
38
+ self.attributes=args[0] if args[0].respond_to?(:each_pair)
39
+ end
40
+ end
41
+ end
32
42
  # --------------------------------------
33
43
  # Identity
34
44
  # --------------------------------------
@@ -48,10 +58,7 @@ class Neo4j::Model
48
58
  end
49
59
 
50
60
 
51
- # --------------------------------------
52
61
  # enables ActiveModel::Dirty and Validation
53
- # --------------------------------------
54
-
55
62
  def method_missing(method_id, *args, &block)
56
63
  if !self.class.attribute_methods_generated?
57
64
  self.class.define_attribute_methods(self.class.properties_info.keys)
@@ -85,28 +92,37 @@ class Neo4j::Model
85
92
  self[key]
86
93
  end
87
94
 
88
- def attributes=(attrs)
89
- attrs.each do |k, v|
95
+ def attributes=(values)
96
+ sanitize_for_mass_assignment(values).each do |k, v|
90
97
  if respond_to?("#{k}=")
91
98
  send("#{k}=", v)
92
99
  else
93
100
  self[k] = v
94
101
  end
95
102
  end
96
- end
103
+ end
97
104
 
98
105
 
99
106
  # Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved.
100
107
  # If the saving fails because of a connection or remote service error, an exception will be raised.
101
108
  # If saving fails because the resource is invalid then false will be returned.
102
109
  def update_attributes(attributes)
103
- update(attributes) # TODO !!!
110
+ Neo4j::Rails::Transaction.running? ? update_attributes_in_tx(attributes): Neo4j::Rails::Transaction.run { update_attributes_in_tx(attributes) }
111
+ end
112
+
113
+ def update_attributes_in_tx(attributes)
114
+ self.attributes=attributes
104
115
  save
105
116
  end
106
117
 
107
- # --------------------------------------
108
- # CRUD
109
- # --------------------------------------
118
+ def update_attributes!(attributes)
119
+ Neo4j::Rails::Transaction.running? ? update_attributes_in_tx!(attributes): Neo4j::Rails::Transaction.run { update_attributes_in_tx!(attributes) }
120
+ end
121
+
122
+ def update_attributes_in_tx!(attributes)
123
+ self.attributes=attributes
124
+ save!
125
+ end
110
126
 
111
127
  def delete
112
128
  super
@@ -116,24 +132,38 @@ class Neo4j::Model
116
132
  def save
117
133
  if valid?
118
134
  # if we are trying to save a value then we should create a real node
119
- if persisted?
120
- _run_update_callbacks do
121
- @previously_changed = changes
122
- @changed_attributes.clear
123
- end
124
- else
125
- node = Neo4j::Node.new(props)
126
- init_on_load(node)
127
- init_on_create
135
+ Neo4j::Rails::Transaction.running? ? save_in_tx : Neo4j::Rails::Transaction.run { save_in_tx }
136
+ true
137
+ else
138
+ # if not valid we should rollback the transaction if there is one
139
+ # so that the changes does not take place.
140
+ # no point failing the transaction if we have not already persisted it since it will then
141
+ # not be persisted
142
+ Neo4j::Rails::Transaction.fail if Neo4j::Rails::Transaction.running? && persisted?
143
+ false
144
+ end
145
+ end
146
+
147
+ def save_in_tx
148
+ if persisted?
149
+ # already existing node - so we are updating it
150
+ _run_update_callbacks do
128
151
  @previously_changed = changes
129
152
  @changed_attributes.clear
130
153
  end
131
- true
154
+ else
155
+ # we are creating a new node
156
+ node = Neo4j::Node.new(props)
157
+ init_on_load(node)
158
+ init_on_create
159
+ @previously_changed = changes
160
+ @changed_attributes.clear
132
161
  end
162
+
133
163
  end
134
164
 
135
165
  def save!
136
- raise RecordInvalidError.new(self) unless save
166
+ raise RecordInvalidError.new(self) unless save
137
167
  end
138
168
 
139
169
  # In neo4j all object are automatically persisted in the database when created (but the Transaction might get rollback)
@@ -147,7 +177,7 @@ class Neo4j::Model
147
177
  end
148
178
 
149
179
  def new_record?()
150
- @_new_record
180
+ _java_node.kind_of?(Neo4j::Value)
151
181
  end
152
182
 
153
183
  def del
@@ -156,7 +186,7 @@ class Neo4j::Model
156
186
  end
157
187
 
158
188
  def destroy
159
- _run_update_callbacks { del }
189
+ Neo4j::Rails::Transaction.running? ? _run_update_callbacks { del } : Neo4j::Rails::Transaction.run { _run_update_callbacks { del } }
160
190
  end
161
191
 
162
192
  def destroyed?()
@@ -171,9 +201,10 @@ class Neo4j::Model
171
201
  class << self
172
202
  # returns a value object instead of creating a new node
173
203
  def new(*args)
174
- value = Neo4j::Value.new(*args)
204
+ value = Neo4j::Value.new
175
205
  wrapped = self.orig_new
176
206
  wrapped.init_on_load(value)
207
+ wrapped.attributes=args[0] if args[0].respond_to?(:each_pair)
177
208
  wrapped
178
209
  end
179
210
 
@@ -200,11 +231,19 @@ class Neo4j::Model
200
231
  end
201
232
  end
202
233
 
234
+ def create(*)
235
+ Neo4j::Rails::Transaction.running? ? super : Neo4j::Rails::Transaction.run { super }
236
+ end
237
+
203
238
  def create!(*args)
204
239
  model = create(*args)
205
240
  raise RecordInvalidError.new(model) unless model.valid?
206
241
  model
207
242
  end
243
+
244
+ def transaction(&block)
245
+ Neo4j::Rails::Transaction.run &block
246
+ end
208
247
  end
209
248
 
210
249
  end
@@ -11,19 +11,56 @@ module Neo4j
11
11
  # ...
12
12
  # end
13
13
  class Transaction
14
- def self.filter(*)
15
- begin
16
- tx = Neo4j::Transaction.new
17
- yield
18
- tx.success
19
- rescue Exception
20
- tx.failure unless tx.nil?
21
- raise
22
- ensure
23
- tx.finish unless tx.nil?
14
+ class << self
15
+ def new
16
+ finish if Thread.current[:neo4j_transaction]
17
+ Thread.current[:neo4j_transaction] = Neo4j::Transaction.new
18
+ end
19
+
20
+ def current
21
+ Thread.current[:neo4j_transaction]
22
+ end
23
+
24
+ def running?
25
+ Thread.current[:neo4j_transaction] != nil
26
+ end
27
+
28
+ def fail?
29
+ Thread.current[:neo4j_transaction_fail] != nil
30
+ end
31
+
32
+ def fail
33
+ Thread.current[:neo4j_transaction_fail] = true
34
+ end
35
+
36
+ def success
37
+ Thread.current[:neo4j_transaction_fail] = nil
38
+ end
39
+
40
+ def finish
41
+ tx = Thread.current[:neo4j_transaction]
42
+ tx.success unless Thread.current[:neo4j_transaction_fail]
43
+ tx.finish
44
+ Thread.current[:neo4j_transaction] = nil
45
+ Thread.current[:neo4j_transaction_fail] = nil
46
+ end
47
+
48
+ def filter(*, &block)
49
+ run &block
50
+ end
51
+
52
+ def run
53
+ begin
54
+ new
55
+ yield
56
+ rescue
57
+ fail
58
+ raise
59
+ ensure
60
+ finish
61
+ end
24
62
  end
25
63
  end
26
64
  end
27
65
  end
28
-
29
66
  end
@@ -1,3 +1,3 @@
1
1
  module Neo4j
2
- VERSION = "1.0.0.beta.4"
2
+ VERSION = "1.0.0.beta.5"
3
3
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - beta
10
- - 4
11
- version: 1.0.0.beta.4
10
+ - 5
11
+ version: 1.0.0.beta.5
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-26 00:00:00 +02:00
19
+ date: 2010-09-28 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency