uorm 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/uorm/dsl.rb CHANGED
@@ -2,12 +2,20 @@ module Uorm
2
2
  module DSL
3
3
  def self.included base
4
4
  base.instance_eval do
5
- def all args = {}
6
- persistance.all(args).map { |attrs| new attrs }
5
+ def all args = {}, &block
6
+ if block_given?
7
+ persistance.all(args) { |attrs| yield new(attrs) if attrs }
8
+ else
9
+ persistance.all(args).map { |attrs| new attrs }
10
+ end
7
11
  end
8
12
 
9
- def find id
10
- new persistance.find id
13
+ def find id, &block
14
+ if block_given?
15
+ persistance.find(id) { |attrs| yield new(attrs) if attrs }
16
+ else
17
+ new persistance.find id
18
+ end
11
19
  end
12
20
 
13
21
  def create attrs
@@ -12,12 +12,24 @@ module Uorm
12
12
  object.id ? false : true
13
13
  end
14
14
 
15
- def all query = {}
16
- collection.find format_query(query)
15
+ def all query = {}, &block
16
+ formatted = format_query(query)
17
+
18
+ if block_given?
19
+ collection.afind(formatted).each &block
20
+ else
21
+ collection.find formatted
22
+ end
17
23
  end
18
24
 
19
- def find id
20
- collection.find_one _id: BSON::ObjectId(id.to_s)
25
+ def find id, &block
26
+ object_id = BSON::ObjectId(id.to_s)
27
+
28
+ if block_given?
29
+ collection.afirst(_id: object_id).callback &block
30
+ else
31
+ collection.first(_id: object_id)
32
+ end
21
33
  end
22
34
 
23
35
  def create object
data/lib/uorm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Uorm
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -91,6 +91,11 @@ describe AMongoModel do
91
91
  AMongoModel.all(reference: '50995b8c15529d4676000001', created: '2003-01-01').
92
92
  should be_collection_of AMongoModel
93
93
  end
94
+
95
+ it 'runs async given a block' do
96
+ AMongoModel.collection.insert reference: BSON::ObjectId('50995b8c15529d4676000001')
97
+ AMongoModel.all { |model| model.should be_instance_of AMongoModel }
98
+ end
94
99
  end
95
100
 
96
101
  describe '.find' do
@@ -103,6 +108,11 @@ describe AMongoModel do
103
108
  model = AMongoModel.create attributes
104
109
  AMongoModel.find(model.id.to_s).should be_instance_of AMongoModel
105
110
  end
111
+
112
+ it 'runs async given a block' do
113
+ model = AMongoModel.create attributes
114
+ AMongoModel.find(model.id) { |model| model.should be_instance_of AMongoModel }
115
+ end
106
116
  end
107
117
 
108
118
  describe '.create' do
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.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: