mince_mongo_db 1.0.0.pre.3 → 1.0.1
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/mince_mongo_db/config.rb +61 -11
- data/lib/mince_mongo_db/connection.rb +16 -1
- data/lib/mince_mongo_db/interface.rb +6 -6
- data/lib/mince_mongo_db/version.rb +1 -1
- data/spec/units/mince_mongo_db/config_spec.rb +83 -0
- data/spec/units/mince_mongo_db/connection_spec.rb +28 -3
- data/spec/units/mince_mongo_db/interface_spec.rb +13 -6
- metadata +12 -15
@@ -6,34 +6,84 @@ module MinceMongoDb # :nodoc:
|
|
6
6
|
# Config specifies the configuration settings
|
7
7
|
#
|
8
8
|
# @author Matt Simpson
|
9
|
+
#
|
10
|
+
# The following fields can be changed:
|
11
|
+
# * database_name - Defaults to 'mince'
|
12
|
+
# * database_host - Defaults to '127.0.0.1'
|
13
|
+
# * username - Defaults to nil
|
14
|
+
# * password - Defaults to nil
|
15
|
+
#
|
16
|
+
# The following fields cannot be changed:
|
17
|
+
# * primary_key
|
18
|
+
# * test_env_number
|
19
|
+
#
|
20
|
+
# You can change a field by passing a hash:
|
21
|
+
#
|
22
|
+
# MinceMongoDb::Config.options = {
|
23
|
+
# username: 'my_db_user',
|
24
|
+
# password: 'my_db_passw0rd'
|
25
|
+
# }
|
26
|
+
#
|
27
|
+
# Or you can change a field individually:
|
28
|
+
#
|
29
|
+
# MinceMongoDb::Config.username = 'my_db_user'
|
30
|
+
#
|
31
|
+
# If you are running tests in parallel, make sure that
|
32
|
+
# ENV['TEST_ENV_NUMBER'] is set for the instance number for each
|
33
|
+
# parallel test. This number is amended to the database name.
|
9
34
|
class Config
|
10
35
|
include Singleton
|
11
36
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
37
|
+
OPTIONS = %w(database_name database_host username password)
|
38
|
+
|
39
|
+
class << self
|
40
|
+
getters = %w(primary_key)
|
41
|
+
accessors = OPTIONS
|
42
|
+
|
43
|
+
(getters+accessors).each do |field|
|
44
|
+
define_method(field) do
|
45
|
+
instance.send(field)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
accessors.each do |field|
|
50
|
+
define_method("#{field}=") do |val|
|
51
|
+
instance.send("#{field}=", val)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.options
|
57
|
+
{
|
58
|
+
database_name: database_name,
|
59
|
+
database_host: database_host,
|
60
|
+
username: username,
|
61
|
+
password: password
|
62
|
+
}
|
18
63
|
end
|
19
64
|
|
20
|
-
def self.
|
21
|
-
|
65
|
+
def self.options=(new_options)
|
66
|
+
whitelist_keys(new_options).each do |key, value|
|
67
|
+
send("#{key}=", value)
|
68
|
+
end
|
22
69
|
end
|
23
70
|
|
24
|
-
def self.
|
25
|
-
|
71
|
+
def self.whitelist_keys(options)
|
72
|
+
options.select{|key, value| OPTIONS.include?(key.to_s) }
|
26
73
|
end
|
27
74
|
|
75
|
+
# Returns the test environment number, useful for when testing with multiple
|
76
|
+
# processes in parallel
|
28
77
|
def self.test_env_number
|
29
78
|
ENV['TEST_ENV_NUMBER']
|
30
79
|
end
|
31
80
|
|
32
|
-
attr_accessor :primary_key, :database_name
|
81
|
+
attr_accessor :primary_key, :database_name, :database_host, :username, :password
|
33
82
|
|
34
83
|
def initialize
|
35
84
|
self.primary_key = '_id'
|
36
85
|
self.database_name = 'mince'
|
86
|
+
self.database_host = '127.0.0.1'
|
37
87
|
end
|
38
88
|
|
39
89
|
def database_name=(name)
|
@@ -17,16 +17,31 @@ module MinceMongoDb
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def initialize
|
20
|
-
self.connection = Mongo::Connection.new
|
20
|
+
self.connection = Mongo::Connection.new(Config.database_host)
|
21
21
|
end
|
22
22
|
|
23
23
|
def connection=(con)
|
24
24
|
@connection = con
|
25
25
|
self.db = connection.db(database_name)
|
26
|
+
auth
|
27
|
+
end
|
28
|
+
|
29
|
+
def auth
|
30
|
+
db.authenticate(Config.username, Config.password) if auth_provided?
|
31
|
+
end
|
32
|
+
|
33
|
+
def auth_provided?
|
34
|
+
!!Config.username && !!Config.password
|
26
35
|
end
|
27
36
|
|
28
37
|
def database_name
|
29
38
|
Config.database_name
|
30
39
|
end
|
31
40
|
end
|
41
|
+
|
42
|
+
class Db
|
43
|
+
def initialize
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
32
47
|
end
|
@@ -51,16 +51,16 @@ module MinceMongoDb
|
|
51
51
|
collection(collection_name).find
|
52
52
|
end
|
53
53
|
|
54
|
-
def self.find(collection_name,
|
55
|
-
collection(collection_name).find_one({
|
54
|
+
def self.find(collection_name, value)
|
55
|
+
collection(collection_name).find_one({primary_key.to_s => value})
|
56
56
|
end
|
57
57
|
|
58
|
-
def self.push_to_array(collection_name,
|
59
|
-
collection(collection_name).update({
|
58
|
+
def self.push_to_array(collection_name, identifying_value, array_key, value_to_push)
|
59
|
+
collection(collection_name).update({primary_key.to_s => identifying_value}, {'$push' => {array_key.to_s => value_to_push}})
|
60
60
|
end
|
61
61
|
|
62
|
-
def self.remove_from_array(collection_name,
|
63
|
-
collection(collection_name).update({
|
62
|
+
def self.remove_from_array(collection_name, identifying_value, array_key, value_to_remove)
|
63
|
+
collection(collection_name).update({primary_key.to_s => identifying_value}, {'$pull' => {array_key.to_s => value_to_remove}})
|
64
64
|
end
|
65
65
|
|
66
66
|
def self.containing_any(collection_name, key, values)
|
@@ -5,6 +5,19 @@ describe MinceMongoDb::Config do
|
|
5
5
|
described_class.primary_key.should == '_id'
|
6
6
|
end
|
7
7
|
|
8
|
+
it 'contains a database host' do
|
9
|
+
described_class.database_host.should == '127.0.0.1'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'can change the database host' do
|
13
|
+
original_host = described_class.database_host
|
14
|
+
new_host = mock
|
15
|
+
described_class.database_host = new_host
|
16
|
+
described_class.database_host.should == new_host
|
17
|
+
|
18
|
+
described_class.database_host = original_host
|
19
|
+
end
|
20
|
+
|
8
21
|
describe 'the database name' do
|
9
22
|
subject { described_class.database_name }
|
10
23
|
|
@@ -37,4 +50,74 @@ describe MinceMongoDb::Config do
|
|
37
50
|
end
|
38
51
|
end
|
39
52
|
end
|
53
|
+
|
54
|
+
it 'does not contain a username' do
|
55
|
+
described_class.username.should be_nil
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'can change the database username' do
|
59
|
+
new_username = mock
|
60
|
+
described_class.username = new_username
|
61
|
+
described_class.username.should == new_username
|
62
|
+
|
63
|
+
described_class.username = nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'does not contain a password' do
|
67
|
+
described_class.password.should be_nil
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'can change the database password' do
|
71
|
+
new_password = mock
|
72
|
+
described_class.password = new_password
|
73
|
+
described_class.password.should == new_password
|
74
|
+
|
75
|
+
described_class.password = nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'can return all writable options' do
|
79
|
+
described_class.options.should == {
|
80
|
+
database_name: described_class.database_name,
|
81
|
+
database_host: described_class.database_host,
|
82
|
+
username: described_class.username,
|
83
|
+
password: described_class.password
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'can change the options by passing a hash' do
|
88
|
+
original_options = described_class.options
|
89
|
+
new_options = {
|
90
|
+
database_name: 'new_database_name',
|
91
|
+
database_host: mock,
|
92
|
+
username: mock,
|
93
|
+
password: mock
|
94
|
+
}
|
95
|
+
described_class.options = new_options
|
96
|
+
described_class.options.should == new_options
|
97
|
+
|
98
|
+
described_class.options = original_options
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'can change a select number of the options by passing a hash' do
|
102
|
+
original_options = described_class.options
|
103
|
+
new_options = {
|
104
|
+
database_name: 'new_database_name',
|
105
|
+
database_host: mock
|
106
|
+
}
|
107
|
+
described_class.options = new_options
|
108
|
+
described_class.options.should == original_options.merge(new_options)
|
109
|
+
|
110
|
+
described_class.options = original_options
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'does not allow assigning unacceptable options' do
|
114
|
+
original_options = described_class.options
|
115
|
+
new_options = {
|
116
|
+
foo: 'foo'
|
117
|
+
}
|
118
|
+
described_class.options = new_options
|
119
|
+
described_class.options.should == original_options
|
120
|
+
|
121
|
+
described_class.options = original_options
|
122
|
+
end
|
40
123
|
end
|
@@ -6,13 +6,38 @@ describe MinceMongoDb::Connection do
|
|
6
6
|
let(:mongo_connection) { mock 'a mongo connection object', :db => db }
|
7
7
|
let(:db) { mock 'db'}
|
8
8
|
let(:database_name) { mock }
|
9
|
+
let(:database_host) { mock }
|
9
10
|
|
10
11
|
before do
|
11
|
-
MinceMongoDb::Config.stub(database_name: database_name)
|
12
|
-
|
12
|
+
MinceMongoDb::Config.stub(database_name: database_name, database_host: database_host, username: nil, password: nil)
|
13
|
+
Mongo::Connection.stub(:new).with(database_host).and_return(mongo_connection)
|
13
14
|
end
|
14
15
|
|
15
|
-
it 'is a mongo connection' do
|
16
|
+
it 'is a mongo connection for the configured host' do
|
17
|
+
subject.connection = mongo_connection
|
18
|
+
|
16
19
|
subject.connection.should == mongo_connection
|
17
20
|
end
|
21
|
+
|
22
|
+
it 'uses the configured database name' do
|
23
|
+
mongo_connection.should_receive(:db).with(database_name).and_return(db)
|
24
|
+
subject.connection = mongo_connection
|
25
|
+
|
26
|
+
subject.db.should == db
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when authentication information is provided' do
|
30
|
+
let(:username) { mock }
|
31
|
+
let(:password) { mock }
|
32
|
+
|
33
|
+
before do
|
34
|
+
MinceMongoDb::Config.stub(username: username, password: password)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'auths the connection' do
|
38
|
+
db.should_receive(:authenticate).with(username, password)
|
39
|
+
|
40
|
+
subject.connection = mongo_connection
|
41
|
+
end
|
42
|
+
end
|
18
43
|
end
|
@@ -82,6 +82,12 @@ describe MinceMongoDb::Interface do
|
|
82
82
|
|
83
83
|
interface.find_all(collection_name).should == return_data
|
84
84
|
end
|
85
|
+
|
86
|
+
it 'can find a record by id' do
|
87
|
+
collection.should_receive(:find_one).with(primary_key.to_s => mock_id).and_return(return_data)
|
88
|
+
|
89
|
+
interface.find(collection_name, mock_id).should == return_data
|
90
|
+
end
|
85
91
|
|
86
92
|
it 'can replace a record' do
|
87
93
|
collection.should_receive(:update).with({primary_key => data[primary_key]}, data)
|
@@ -92,10 +98,11 @@ describe MinceMongoDb::Interface do
|
|
92
98
|
it 'can get one document' do
|
93
99
|
field = "stuff"
|
94
100
|
value = "more stuff"
|
101
|
+
mongo_scope = mock first: return_data
|
95
102
|
|
96
|
-
collection.should_receive(:
|
103
|
+
collection.should_receive(:find).with(field => value).and_return(mongo_scope)
|
97
104
|
|
98
|
-
interface.
|
105
|
+
interface.get_for_key_with_value(collection_name, field, value).should == return_data
|
99
106
|
end
|
100
107
|
|
101
108
|
it 'can clear the data store' do
|
@@ -133,14 +140,14 @@ describe MinceMongoDb::Interface do
|
|
133
140
|
end
|
134
141
|
|
135
142
|
it 'can push a value to an array for a specific record' do
|
136
|
-
collection.should_receive(:update).with({
|
143
|
+
collection.should_receive(:update).with({interface.primary_key.to_s => "value"}, { '$push' => { "array_key" => "value_to_push"}}).and_return(return_data)
|
137
144
|
|
138
|
-
interface.push_to_array(collection_name,
|
145
|
+
interface.push_to_array(collection_name, "value", :array_key, "value_to_push").should == return_data
|
139
146
|
end
|
140
147
|
|
141
148
|
it 'can remove a value from an array for a specific record' do
|
142
|
-
collection.should_receive(:update).with({
|
149
|
+
collection.should_receive(:update).with({interface.primary_key.to_s => "value"}, { '$pull' => { "array_key" => "value_to_remove"}}).and_return(return_data)
|
143
150
|
|
144
|
-
interface.remove_from_array(collection_name,
|
151
|
+
interface.remove_from_array(collection_name, "value", :array_key, "value_to_remove").should == return_data
|
145
152
|
end
|
146
153
|
end
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mince_mongo_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Matt Simpson
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2013-02-26 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: mongo
|
@@ -148,7 +148,7 @@ dependencies:
|
|
148
148
|
requirements:
|
149
149
|
- - ~>
|
150
150
|
- !ruby/object:Gem::Version
|
151
|
-
version: '1.
|
151
|
+
version: '1.3'
|
152
152
|
type: :development
|
153
153
|
prerelease: false
|
154
154
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -156,23 +156,23 @@ dependencies:
|
|
156
156
|
requirements:
|
157
157
|
- - ~>
|
158
158
|
- !ruby/object:Gem::Version
|
159
|
-
version: '1.
|
159
|
+
version: '1.3'
|
160
160
|
- !ruby/object:Gem::Dependency
|
161
161
|
name: mince
|
162
162
|
requirement: !ruby/object:Gem::Requirement
|
163
163
|
none: false
|
164
164
|
requirements:
|
165
|
-
- -
|
165
|
+
- - ~>
|
166
166
|
- !ruby/object:Gem::Version
|
167
|
-
version: 2.0
|
167
|
+
version: '2.0'
|
168
168
|
type: :development
|
169
169
|
prerelease: false
|
170
170
|
version_requirements: !ruby/object:Gem::Requirement
|
171
171
|
none: false
|
172
172
|
requirements:
|
173
|
-
- -
|
173
|
+
- - ~>
|
174
174
|
- !ruby/object:Gem::Version
|
175
|
-
version: 2.0
|
175
|
+
version: '2.0'
|
176
176
|
- !ruby/object:Gem::Dependency
|
177
177
|
name: rb-fsevent
|
178
178
|
requirement: !ruby/object:Gem::Requirement
|
@@ -220,18 +220,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
220
220
|
- - ! '>='
|
221
221
|
- !ruby/object:Gem::Version
|
222
222
|
version: '0'
|
223
|
-
segments:
|
224
|
-
- 0
|
225
|
-
hash: 1499999616384769131
|
226
223
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
227
224
|
none: false
|
228
225
|
requirements:
|
229
|
-
- - ! '
|
226
|
+
- - ! '>='
|
230
227
|
- !ruby/object:Gem::Version
|
231
|
-
version:
|
228
|
+
version: '0'
|
232
229
|
requirements: []
|
233
230
|
rubyforge_project: mince_mongo_db
|
234
|
-
rubygems_version: 1.8.
|
231
|
+
rubygems_version: 1.8.25
|
235
232
|
signing_key:
|
236
233
|
specification_version: 3
|
237
234
|
summary: Lightweight MongoDB ORM for Ruby.
|