uorm 0.0.7 → 0.0.8
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.
- data/lib/uorm.rb +1 -1
- data/lib/uorm/callbacks.rb +4 -22
- data/lib/uorm/dsl.rb +20 -15
- data/lib/uorm/model.rb +10 -0
- data/lib/uorm/mongo/persistance.rb +4 -7
- data/lib/uorm/version.rb +1 -1
- data/spec/em-mongo_spec.rb +19 -2
- data/spec/spec_helper.rb +0 -2
- metadata +2 -1
data/lib/uorm.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'uorm/version'
|
2
2
|
|
3
|
-
require 'active_support/callbacks'
|
4
3
|
require 'active_support/core_ext/hash'
|
5
4
|
|
6
5
|
require 'em-synchrony'
|
@@ -11,6 +10,7 @@ module Uorm
|
|
11
10
|
autoload :FieldCollection, 'uorm/field_collection'
|
12
11
|
autoload :Attributes, 'uorm/attributes'
|
13
12
|
autoload :DSL, 'uorm/dsl'
|
13
|
+
autoload :Model, 'uorm/model'
|
14
14
|
autoload :Callbacks, 'uorm/callbacks'
|
15
15
|
|
16
16
|
autoload :Redis, 'uorm/redis'
|
data/lib/uorm/callbacks.rb
CHANGED
@@ -1,29 +1,11 @@
|
|
1
1
|
module Uorm
|
2
2
|
module Callbacks
|
3
|
-
|
4
|
-
|
5
|
-
base.instance_eval do
|
6
|
-
include ::ActiveSupport::Callbacks
|
7
|
-
define_callbacks :save, :create, :update, :delete, scope: [:kind, :name]
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def perform_create
|
14
|
-
run_callbacks(:save) do
|
15
|
-
run_callbacks(:create) { super }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def perform_update
|
20
|
-
run_callbacks(:save) do
|
21
|
-
run_callbacks(:update) { super }
|
22
|
-
end
|
3
|
+
class Create
|
4
|
+
include ::EM::Deferrable
|
23
5
|
end
|
24
6
|
|
25
|
-
|
26
|
-
|
7
|
+
class Update
|
8
|
+
include ::EM::Deferrable
|
27
9
|
end
|
28
10
|
end
|
29
11
|
end
|
data/lib/uorm/dsl.rb
CHANGED
@@ -6,7 +6,7 @@ module Uorm
|
|
6
6
|
if block_given?
|
7
7
|
persistance.all(args) { |attrs| yield new(attrs) if attrs }
|
8
8
|
else
|
9
|
-
persistance.all(args).map { |attrs| new
|
9
|
+
persistance.all(args).map { |attrs| new(attrs) }
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -14,19 +14,15 @@ module Uorm
|
|
14
14
|
if block_given?
|
15
15
|
persistance.find(id) { |attrs| yield new(attrs) if attrs }
|
16
16
|
else
|
17
|
-
new persistance.find
|
17
|
+
new persistance.find(id)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def create attrs
|
22
|
-
new(attrs).save
|
23
|
-
end
|
24
|
-
|
25
|
-
def exists? id, &block
|
21
|
+
def create attrs, &block
|
26
22
|
if block_given?
|
27
|
-
|
23
|
+
new(attrs).save &block
|
28
24
|
else
|
29
|
-
|
25
|
+
new(attrs).save
|
30
26
|
end
|
31
27
|
end
|
32
28
|
end
|
@@ -36,8 +32,8 @@ module Uorm
|
|
36
32
|
persistance.new? self
|
37
33
|
end
|
38
34
|
|
39
|
-
def save
|
40
|
-
new? ? perform_create : perform_update
|
35
|
+
def save &block
|
36
|
+
new? ? perform_create(&block) : perform_update(&block)
|
41
37
|
self
|
42
38
|
end
|
43
39
|
|
@@ -54,12 +50,21 @@ module Uorm
|
|
54
50
|
|
55
51
|
private
|
56
52
|
|
57
|
-
def perform_create
|
58
|
-
|
53
|
+
def perform_create &block
|
54
|
+
fiber = Fiber.current
|
55
|
+
create_callback = Callbacks::Create.new
|
56
|
+
create_callback.callback &block
|
57
|
+
persistance.create(self) { |object_id| fiber.resume object_id }
|
58
|
+
self.id = Fiber.yield
|
59
|
+
create_callback.succeed self
|
59
60
|
end
|
60
61
|
|
61
|
-
def perform_update
|
62
|
-
|
62
|
+
def perform_update &block
|
63
|
+
update_callback = Callbacks::Update.new
|
64
|
+
update_callback.callback &block
|
65
|
+
persistance.update self do
|
66
|
+
update_callback.succeed self
|
67
|
+
end
|
63
68
|
end
|
64
69
|
|
65
70
|
def perform_delete
|
data/lib/uorm/model.rb
ADDED
@@ -24,7 +24,6 @@ module Uorm
|
|
24
24
|
|
25
25
|
def find id, &block
|
26
26
|
object_id = BSON::ObjectId(id.to_s)
|
27
|
-
|
28
27
|
if block_given?
|
29
28
|
collection.afirst(_id: object_id).callback &block
|
30
29
|
else
|
@@ -32,14 +31,12 @@ module Uorm
|
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
35
|
-
def create object
|
36
|
-
|
37
|
-
rescue
|
38
|
-
false
|
34
|
+
def create object, &block
|
35
|
+
collection.safe_insert(object.attributes.except(:id)).callback &block
|
39
36
|
end
|
40
37
|
|
41
|
-
def update object
|
42
|
-
|
38
|
+
def update object, &block
|
39
|
+
collection.safe_update({_id: object.id}, object.attributes.except(:id)).callback &block
|
43
40
|
end
|
44
41
|
|
45
42
|
def delete object
|
data/lib/uorm/version.rb
CHANGED
data/spec/em-mongo_spec.rb
CHANGED
@@ -6,8 +6,7 @@ require 'em-synchrony/em-mongo'
|
|
6
6
|
Uorm::Mongo::DB.name = 'uorm_test'
|
7
7
|
|
8
8
|
class AMongoModel
|
9
|
-
include Uorm::
|
10
|
-
include Uorm::Attributes
|
9
|
+
include Uorm::Model
|
11
10
|
include Uorm::Mongo
|
12
11
|
|
13
12
|
field :reference, type: Uorm::Type::ObjectId
|
@@ -123,6 +122,24 @@ describe AMongoModel do
|
|
123
122
|
it 'returns an object with the newly created attributes' do
|
124
123
|
AMongoModel.create(attributes).should be_instance_of AMongoModel
|
125
124
|
end
|
125
|
+
|
126
|
+
it 'runs given block passing the created object if its creation was flawless' do
|
127
|
+
called = false
|
128
|
+
AMongoModel.create(attributes) do |model|
|
129
|
+
model.should be_instance_of AMongoModel
|
130
|
+
called = true
|
131
|
+
end
|
132
|
+
called.should be_true
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'does not run given block if the creation failed' do
|
136
|
+
called = false
|
137
|
+
first = AMongoModel.create(attributes)
|
138
|
+
AMongoModel.create(first.attributes) do |model|
|
139
|
+
called = true
|
140
|
+
end
|
141
|
+
called.should be_false
|
142
|
+
end
|
126
143
|
end
|
127
144
|
|
128
145
|
describe '#save' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uorm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -174,6 +174,7 @@ files:
|
|
174
174
|
- lib/uorm/dsl.rb
|
175
175
|
- lib/uorm/field.rb
|
176
176
|
- lib/uorm/field_collection.rb
|
177
|
+
- lib/uorm/model.rb
|
177
178
|
- lib/uorm/mongo.rb
|
178
179
|
- lib/uorm/mongo/db.rb
|
179
180
|
- lib/uorm/mongo/object_id.rb
|