mongo_adaptor 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_adaptor.rb +31 -13
- data/lib/mongo_adaptor/version.rb +1 -1
- data/spec/mongo_adaptor_spec.rb +4 -1
- metadata +4 -4
data/lib/mongo_adaptor.rb
CHANGED
@@ -9,33 +9,37 @@ class MongoAdaptor
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
def initialize
|
12
|
+
def initialize name, klass
|
13
13
|
@collection = self.class.db.collection name.to_s.downcase
|
14
14
|
@klass = klass
|
15
15
|
end
|
16
16
|
|
17
|
-
def insert
|
18
|
-
@collection.insert
|
17
|
+
def insert model
|
18
|
+
@collection.insert process(model), safe_mode
|
19
19
|
end
|
20
|
-
|
21
|
-
|
20
|
+
|
21
|
+
def upsert model, query = { "_id" => model.id }
|
22
|
+
@collection.update query, set(process(model)), safe_mode.merge(upsert_mode true)
|
22
23
|
end
|
23
|
-
|
24
|
-
|
24
|
+
|
25
|
+
def update model, query = { "_id" => model.id }
|
26
|
+
@collection.update query, set(process(model)), safe_mode.merge(upsert_mode false)
|
25
27
|
end
|
26
28
|
|
27
|
-
def fetch
|
28
|
-
@collection.find_one
|
29
|
+
def fetch selector = {}, opts = {}
|
30
|
+
@collection.find_one selector, opts.merge( :transformer => builder )
|
29
31
|
end
|
30
|
-
|
31
|
-
|
32
|
+
|
33
|
+
def remove selector = {}, opts = {}
|
34
|
+
@collection.remove selector, opts
|
32
35
|
end
|
33
36
|
|
34
|
-
def find
|
35
|
-
@collection.find
|
37
|
+
def find selector = {}, opts = {}
|
38
|
+
@collection.find selector, opts.merge( :transformer => builder )
|
36
39
|
end
|
37
40
|
|
38
41
|
private
|
42
|
+
|
39
43
|
def builder
|
40
44
|
proc do |result|
|
41
45
|
@klass.new.tap do |model|
|
@@ -46,9 +50,23 @@ class MongoAdaptor
|
|
46
50
|
end
|
47
51
|
end
|
48
52
|
end
|
53
|
+
|
49
54
|
def process(model)
|
50
55
|
fields = {}
|
51
56
|
model.each_pair { |field,value| fields[field] = value unless field == :id }
|
52
57
|
fields
|
53
58
|
end
|
59
|
+
|
60
|
+
def safe_mode
|
61
|
+
{ :safe => true }
|
62
|
+
end
|
63
|
+
|
64
|
+
def upsert_mode level
|
65
|
+
{ :upsert => level }
|
66
|
+
end
|
67
|
+
|
68
|
+
def set query
|
69
|
+
{ "$set" => query }
|
70
|
+
end
|
71
|
+
|
54
72
|
end
|
data/spec/mongo_adaptor_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe 'adapting structs into mongo' do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
describe '
|
17
|
+
describe 'using the adaptor' do
|
18
18
|
let(:klass) { Struct.new :name, :other, :members, :id }
|
19
19
|
let(:adaptor) { MongoAdaptor.new 'test_collection', klass }
|
20
20
|
let(:collection) { Mongo::Configure.current.load.collection 'test_collection' }
|
@@ -122,6 +122,9 @@ describe 'adapting structs into mongo' do
|
|
122
122
|
it 'gets them all' do
|
123
123
|
subject.map(&:other).should == [0,1,2]
|
124
124
|
end
|
125
|
+
it 'will pass along options' do
|
126
|
+
expect { adaptor.find({ :name => 'My Model' },{ fields: { }}) }.to_not raise_error
|
127
|
+
end
|
125
128
|
end
|
126
129
|
end
|
127
130
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_adaptor
|
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:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
@@ -122,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
segments:
|
124
124
|
- 0
|
125
|
-
hash:
|
125
|
+
hash: 1562466360637587669
|
126
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
127
|
none: false
|
128
128
|
requirements:
|
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
131
|
version: '0'
|
132
132
|
segments:
|
133
133
|
- 0
|
134
|
-
hash:
|
134
|
+
hash: 1562466360637587669
|
135
135
|
requirements: []
|
136
136
|
rubyforge_project:
|
137
137
|
rubygems_version: 1.8.25
|