ruby_mongo_utils 0.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/.gitignore +18 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +44 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/mongo_utils/mongo_dump.rb +14 -0
- data/lib/mongo_utils/mongo_restore.rb +12 -0
- data/lib/mongo_utils/stripable.rb +36 -0
- data/lib/mongo_utils/version.rb +3 -0
- data/lib/mongo_utils.rb +5 -0
- data/mongo_utils.gemspec +24 -0
- data/mongodumps/mongo_utils_tests/pages.bson +0 -0
- data/mongodumps/mongo_utils_tests/pages.metadata.json +1 -0
- data/spec/fixtures/page.rb +12 -0
- data/spec/fixtures/post.rb +9 -0
- data/spec/mongo_utils/mongo_restore_spec.rb +41 -0
- data/spec/mongo_utils/stripable_spec.rb +296 -0
- data/spec/spec_helper.rb +22 -0
- metadata +117 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongo_utils (0.0.1)
|
5
|
+
mongoid
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.13)
|
11
|
+
activesupport (= 3.2.13)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activesupport (3.2.13)
|
14
|
+
i18n (= 0.6.1)
|
15
|
+
multi_json (~> 1.0)
|
16
|
+
builder (3.0.4)
|
17
|
+
database_cleaner (0.9.1)
|
18
|
+
diff-lcs (1.2.4)
|
19
|
+
i18n (0.6.1)
|
20
|
+
mongoid (3.1.3)
|
21
|
+
activemodel (~> 3.2)
|
22
|
+
moped (~> 1.4.2)
|
23
|
+
origin (~> 1.0)
|
24
|
+
tzinfo (~> 0.3.22)
|
25
|
+
moped (1.4.5)
|
26
|
+
multi_json (1.7.2)
|
27
|
+
origin (1.1.0)
|
28
|
+
rspec (2.13.0)
|
29
|
+
rspec-core (~> 2.13.0)
|
30
|
+
rspec-expectations (~> 2.13.0)
|
31
|
+
rspec-mocks (~> 2.13.0)
|
32
|
+
rspec-core (2.13.1)
|
33
|
+
rspec-expectations (2.13.0)
|
34
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
35
|
+
rspec-mocks (2.13.1)
|
36
|
+
tzinfo (0.3.37)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
database_cleaner
|
43
|
+
mongo_utils!
|
44
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Nikita Cernovs
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# MongoUtils
|
2
|
+
|
3
|
+
Ruby Mongo Utilities
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mongo_utils'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mongo_utils
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module MongoUtils
|
2
|
+
module MongoDump
|
3
|
+
def self.call(options)
|
4
|
+
cmd = %{
|
5
|
+
mongodump --db #{Mongoid.respond_to?(:database) ? Mongoid.database.name : Mongoid.default_session['database'].database.name}
|
6
|
+
--host #{Mongoid.respond_to?(:database) ? Mongoid.database.connection.primary.join(':') : Mongoid.sessions['default']['hosts'][0]}
|
7
|
+
--collection #{options[:collection]}
|
8
|
+
--query "#{options[:query]}"
|
9
|
+
-o #{options[:path]}
|
10
|
+
}.squish
|
11
|
+
`#{cmd}`
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MongoUtils
|
2
|
+
module MongoRestore
|
3
|
+
def self.call(options)
|
4
|
+
cmd = %{
|
5
|
+
mongorestore --host #{Mongoid.respond_to?(:database) ? Mongoid.database.connection.primary.join(':') : Mongoid.sessions['default']['hosts'][0]}
|
6
|
+
--filter "#{options[:filter]}"
|
7
|
+
#{options[:path]}
|
8
|
+
}.squish
|
9
|
+
`#{cmd}`
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module MongoUtils
|
2
|
+
module Stripable
|
3
|
+
def blank_value?(value)
|
4
|
+
if value.is_a?(Hash)
|
5
|
+
value.reject! { |_, v| blank_value?(v) }
|
6
|
+
end
|
7
|
+
plain_value_blank?(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def plain_value_blank?(value)
|
11
|
+
value.blank? || value == 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def blank_and_was_blank_value?(attribute, value)
|
15
|
+
blank_value?(value) && respond_to?("#{attribute}_was") && blank_value?(send("#{attribute}_was"))
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.included(base)
|
19
|
+
base.send :include, Mongoid::Document
|
20
|
+
|
21
|
+
base.around_create do |&b|
|
22
|
+
attributes.reject! { |_, value| blank_value?(value) }
|
23
|
+
b.call
|
24
|
+
apply_defaults
|
25
|
+
end
|
26
|
+
|
27
|
+
base.around_update do |&b|
|
28
|
+
attributes.reject! { |a, v|
|
29
|
+
blank_and_was_blank_value?(a, v)
|
30
|
+
}
|
31
|
+
b.call
|
32
|
+
apply_defaults
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/mongo_utils.rb
ADDED
data/mongo_utils.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongo_utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'ruby_mongo_utils'
|
8
|
+
gem.version = MongoUtils::VERSION
|
9
|
+
gem.authors = ['Nikita Cernovs']
|
10
|
+
gem.email = ['n.cernovs@77agency.com']
|
11
|
+
gem.description = %q{Ruby Mongo Utilities}
|
12
|
+
gem.summary = %q{Ruby Mongo Utilities}
|
13
|
+
gem.homepage = ''
|
14
|
+
|
15
|
+
gem.add_dependency 'mongoid'
|
16
|
+
|
17
|
+
gem.add_development_dependency 'rspec'
|
18
|
+
gem.add_development_dependency 'database_cleaner'
|
19
|
+
|
20
|
+
gem.files = `git ls-files`.split($/)
|
21
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
22
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
23
|
+
gem.require_paths = ['lib']
|
24
|
+
end
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
{indexes:[{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "mongo_utils_tests.pages", "name" : "_id_" }]}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MongoUtils::MongoRestore do
|
4
|
+
it 'should create backup files and be able to restore from it' do
|
5
|
+
10.times { Page.create! page_type: 'Facebook' }
|
6
|
+
10.times { Page.create! page_type: 'Google+' }
|
7
|
+
10.times { Page.create! page_type: 'Wordpress' }
|
8
|
+
|
9
|
+
Page.count.should eq 30
|
10
|
+
|
11
|
+
path = "#{Dir.pwd}/mongodumps"
|
12
|
+
|
13
|
+
MongoUtils::MongoDump.call(
|
14
|
+
collection: Page.collection_name,
|
15
|
+
query: "{ 'page_type': { '\\$ne': 'Wordpress' } }",
|
16
|
+
path: path
|
17
|
+
)
|
18
|
+
|
19
|
+
Page.count.should eq 30
|
20
|
+
|
21
|
+
Page.destroy_all
|
22
|
+
|
23
|
+
Page.count.should eq 0
|
24
|
+
|
25
|
+
MongoUtils::MongoRestore.call(
|
26
|
+
collection: Page.collection_name,
|
27
|
+
filter: "{ 'page_type': 'Facebook' }",
|
28
|
+
path: path
|
29
|
+
)
|
30
|
+
|
31
|
+
Page.count.should eq 10
|
32
|
+
|
33
|
+
MongoUtils::MongoRestore.call(
|
34
|
+
collection: Page.collection_name,
|
35
|
+
filter: "{ 'page_type': 'Google+' }",
|
36
|
+
path: path
|
37
|
+
)
|
38
|
+
|
39
|
+
Page.count.should eq 20
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,296 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MongoUtils::Stripable do
|
4
|
+
context 'on create' do
|
5
|
+
it 'should not store blank default values' do
|
6
|
+
page = Page.create!
|
7
|
+
|
8
|
+
page.name.should eq nil
|
9
|
+
page.tags.should eq []
|
10
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
11
|
+
page.posts.should eq []
|
12
|
+
|
13
|
+
Page.collection.find.first.keys.should eq ['_id']
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should not store blank or zero values' do
|
17
|
+
page = Page.create! name: '', tags: [], likes: nil
|
18
|
+
|
19
|
+
page.name.should eq nil
|
20
|
+
page.tags.should eq []
|
21
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
22
|
+
page.posts.should eq []
|
23
|
+
|
24
|
+
Page.collection.find.first.keys.should eq ['_id']
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should store values with content' do
|
28
|
+
page = Page.create! name: 'Name', tags: ['a']
|
29
|
+
|
30
|
+
page.name.should eq 'Name'
|
31
|
+
page.tags.should eq ['a']
|
32
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
33
|
+
page.posts.should eq []
|
34
|
+
|
35
|
+
page = Page.collection.find.first
|
36
|
+
page.keys.sort.should eq ['_id', 'name', 'tags']
|
37
|
+
page['name'].should eq 'Name'
|
38
|
+
page['tags'].should eq ['a']
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should recursiverly detect if hash has no values' do
|
42
|
+
page = Page.create! likes: { lifetime: { user555: 0 } }
|
43
|
+
|
44
|
+
page.name.should eq nil
|
45
|
+
page.tags.should eq []
|
46
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
47
|
+
page.posts.should eq []
|
48
|
+
|
49
|
+
Page.collection.find.first.keys.sort.should eq ['_id']
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should recursiverly detect if hash has values' do
|
53
|
+
page = Page.create! likes: { lifetime: { user555: 42 } }
|
54
|
+
|
55
|
+
page.name.should eq nil
|
56
|
+
page.tags.should eq []
|
57
|
+
page.likes.should eq({ lifetime: { user555: 42 } })
|
58
|
+
page.posts.should eq []
|
59
|
+
|
60
|
+
page = Page.collection.find.first
|
61
|
+
page.keys.sort.should eq ['_id', 'likes']
|
62
|
+
page['likes'].should eq({ 'lifetime' => { 'user555' => 42 } })
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should recursiverly strtip blank values from hash and save the rest' do
|
66
|
+
page = Page.create! likes: { lifetime: { user555: 42, user777: 0 } }
|
67
|
+
|
68
|
+
page.likes.should eq({ lifetime: { user555: 42 } })
|
69
|
+
|
70
|
+
Page.collection.find.first['likes'].should eq({ 'lifetime' => { 'user555' => 42 } })
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should not strip empty values from embedded document when save is called on it' do
|
74
|
+
page = Page.new
|
75
|
+
post = page.posts.build
|
76
|
+
page.save!
|
77
|
+
|
78
|
+
post.message.should eq ''
|
79
|
+
post.likes.should eq 0
|
80
|
+
|
81
|
+
posts = Page.collection.find.first['posts']
|
82
|
+
posts.count.should eq 1
|
83
|
+
|
84
|
+
post = posts.first
|
85
|
+
post.keys.sort.should eq ['_id', 'likes', 'message']
|
86
|
+
post['message'].should eq ''
|
87
|
+
post['likes'].should eq 0
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should strip empty values from embedded document when save is called on it' do
|
91
|
+
page = Page.create!
|
92
|
+
post = page.posts.build
|
93
|
+
post.save!
|
94
|
+
|
95
|
+
post.message.should eq ''
|
96
|
+
post.likes.should eq 0
|
97
|
+
|
98
|
+
posts = Page.collection.find.first['posts']
|
99
|
+
posts.count.should eq 1
|
100
|
+
posts.first.keys.should eq ['_id']
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should save embedded document fields' do
|
104
|
+
page = Page.create!
|
105
|
+
post = page.posts.build message: 'Message of the post!', likes: 2
|
106
|
+
post.save!
|
107
|
+
|
108
|
+
post.message.should eq 'Message of the post!'
|
109
|
+
post.likes.should eq 2
|
110
|
+
|
111
|
+
posts = Page.collection.find.first['posts']
|
112
|
+
posts.count.should eq 1
|
113
|
+
|
114
|
+
post = posts.first
|
115
|
+
post.keys.sort.should eq ['_id', 'likes', 'message']
|
116
|
+
post['message'].should eq 'Message of the post!'
|
117
|
+
post['likes'].should eq 2
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe 'stored post' do
|
122
|
+
before do
|
123
|
+
@page = Page.new({
|
124
|
+
name: 'Page name',
|
125
|
+
tags: ['A', 'B', 'C']
|
126
|
+
})
|
127
|
+
@page.posts.build({ message: 'Post1'})
|
128
|
+
@page.save!
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should be fetched properly' do
|
132
|
+
page = Page.first
|
133
|
+
page.name.should eq 'Page name'
|
134
|
+
page.tags.should eq ['A', 'B', 'C']
|
135
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
136
|
+
page.posts.count.should eq 1
|
137
|
+
|
138
|
+
post = page.posts.first
|
139
|
+
post.message.should eq 'Post1'
|
140
|
+
post.likes.should eq 0
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should look in DB correctly' do
|
144
|
+
page = Page.collection.find.first
|
145
|
+
|
146
|
+
page.keys.sort.should eq ['_id', 'name', 'posts', 'tags']
|
147
|
+
page['name'].should eq 'Page name'
|
148
|
+
page['tags'].should eq ['A', 'B', 'C']
|
149
|
+
page['posts'].count.should eq 1
|
150
|
+
|
151
|
+
post = page['posts'].first
|
152
|
+
|
153
|
+
post.keys.sort.should eq ['_id', 'likes', 'message']
|
154
|
+
post['message'].should eq 'Post1'
|
155
|
+
post['likes'].should eq 0
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'on update' do
|
159
|
+
it 'should set present value to blank' do
|
160
|
+
page = Page.first
|
161
|
+
page.name = ''
|
162
|
+
page.save!
|
163
|
+
|
164
|
+
page.name.should eq ''
|
165
|
+
|
166
|
+
page = Page.collection.find.first
|
167
|
+
page.keys.should include 'name'
|
168
|
+
page['name'].should eq ''
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'should not set nil value to default empty value' do
|
172
|
+
page = Page.first
|
173
|
+
page.name = 'Page name 2'
|
174
|
+
page.save!
|
175
|
+
|
176
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
177
|
+
|
178
|
+
page = Page.collection.find.first
|
179
|
+
page.keys.should_not include 'likes'
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'should allow to set a value to a blank' do
|
183
|
+
page = Page.first
|
184
|
+
page.tags = []
|
185
|
+
page.save!
|
186
|
+
|
187
|
+
page.tags.should eq []
|
188
|
+
|
189
|
+
page = Page.collection.find.first
|
190
|
+
page['tags'].should eq([])
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'should not set empty value to another empty value' do
|
194
|
+
page = Page.first
|
195
|
+
page.likes = { three_months: { total: 0} }
|
196
|
+
page.save!
|
197
|
+
|
198
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
199
|
+
|
200
|
+
page = Page.collection.find.first
|
201
|
+
page.keys.should_not include 'likes'
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'should not set empty value to another empty value in the second level hash' do
|
205
|
+
page = Page.first
|
206
|
+
page.likes[:lifetime] = { by_user: 0 }
|
207
|
+
page.save!
|
208
|
+
|
209
|
+
page.likes.should eq({ lifetime: { total: 0 } })
|
210
|
+
|
211
|
+
page = Page.collection.find.first
|
212
|
+
page.keys.should_not include 'likes'
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'should update an empty value to another' do
|
216
|
+
page = Page.first
|
217
|
+
page.likes[:lifetime] = { user777: 77 }
|
218
|
+
page.save!
|
219
|
+
|
220
|
+
page.likes.should eq({ lifetime: { user777: 77 } })
|
221
|
+
|
222
|
+
page = Page.collection.find.first
|
223
|
+
page.keys.should include 'likes'
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'should update an empty value to another and stript empties in higher level hashes' do
|
227
|
+
page = Page.first
|
228
|
+
page.likes[:lifetime] = { user777: 77, user555: 0 }
|
229
|
+
page.save!
|
230
|
+
|
231
|
+
page.likes.should eq({ lifetime: { user777: 77 } })
|
232
|
+
|
233
|
+
page = Page.collection.find.first
|
234
|
+
page.keys.should include 'likes'
|
235
|
+
end
|
236
|
+
|
237
|
+
it 'should update an empty value to another and stript empties in higher level hashes' do
|
238
|
+
page = Page.first
|
239
|
+
page.likes[:lifetime] = { user777: 77, user555: 0, total: { by_user: [] } }
|
240
|
+
page.save!
|
241
|
+
|
242
|
+
page.likes.should eq({ lifetime: { user777: 77 } })
|
243
|
+
|
244
|
+
page = Page.collection.find.first
|
245
|
+
page.keys.should include 'likes'
|
246
|
+
end
|
247
|
+
|
248
|
+
context 'after update' do
|
249
|
+
before do
|
250
|
+
page = Page.first
|
251
|
+
page.likes[:lifetime] = { user777: 77, user555: 0, total: { by_user: [] } }
|
252
|
+
page.save!
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'should be able to add another key to the hash' do
|
256
|
+
page = Page.first
|
257
|
+
page.likes[:totals] = 55
|
258
|
+
page.save!
|
259
|
+
|
260
|
+
page = Page.first
|
261
|
+
page.likes['totals'].should eq 55
|
262
|
+
page.likes['lifetime'].should eq({ 'user777' => 77 })
|
263
|
+
end
|
264
|
+
|
265
|
+
it 'should be able to add another key to the second level hash' do
|
266
|
+
page = Page.first
|
267
|
+
page.likes['lifetime']['user555'] = 55
|
268
|
+
page.save!
|
269
|
+
|
270
|
+
page = Page.first
|
271
|
+
page.likes['lifetime']['user555'].should eq 55
|
272
|
+
page.likes['lifetime']['user777'].should eq 77
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'should be able to make existing key inside a hash to empty value' do
|
276
|
+
page = Page.first
|
277
|
+
page.likes['lifetime']['user777'] = 0
|
278
|
+
page.save!
|
279
|
+
|
280
|
+
page = Page.first
|
281
|
+
page.likes['lifetime'].should be_blank
|
282
|
+
end
|
283
|
+
|
284
|
+
it 'should be able to make existing key inside a hash to empty value and set another value' do
|
285
|
+
page = Page.first
|
286
|
+
page.likes['lifetime']['user555'] = 55
|
287
|
+
page.likes['lifetime']['user777'] = 0
|
288
|
+
page.save!
|
289
|
+
|
290
|
+
page = Page.first
|
291
|
+
page.likes['lifetime'].should eq({ 'user555' => 55 })
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'mongo_utils'
|
2
|
+
|
3
|
+
Mongoid::Config.send :load_configuration, { sessions: { default: { hosts: ['localhost:27017'] , database: 'mongo_utils_tests' } } }
|
4
|
+
|
5
|
+
require './spec/fixtures/page'
|
6
|
+
require './spec/fixtures/post'
|
7
|
+
|
8
|
+
require 'database_cleaner'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.mock_with :rspec
|
12
|
+
|
13
|
+
config.before(:suite) do
|
14
|
+
DatabaseCleaner.orm = :mongoid
|
15
|
+
DatabaseCleaner.strategy = :truncation
|
16
|
+
DatabaseCleaner.clean
|
17
|
+
end
|
18
|
+
|
19
|
+
config.after(:each) do
|
20
|
+
DatabaseCleaner.clean
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_mongo_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nikita Cernovs
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: database_cleaner
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Ruby Mongo Utilities
|
63
|
+
email:
|
64
|
+
- n.cernovs@77agency.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- Gemfile.lock
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/mongo_utils.rb
|
76
|
+
- lib/mongo_utils/mongo_dump.rb
|
77
|
+
- lib/mongo_utils/mongo_restore.rb
|
78
|
+
- lib/mongo_utils/stripable.rb
|
79
|
+
- lib/mongo_utils/version.rb
|
80
|
+
- mongo_utils.gemspec
|
81
|
+
- mongodumps/mongo_utils_tests/pages.bson
|
82
|
+
- mongodumps/mongo_utils_tests/pages.metadata.json
|
83
|
+
- spec/fixtures/page.rb
|
84
|
+
- spec/fixtures/post.rb
|
85
|
+
- spec/mongo_utils/mongo_restore_spec.rb
|
86
|
+
- spec/mongo_utils/stripable_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses: []
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.8.24
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: Ruby Mongo Utilities
|
112
|
+
test_files:
|
113
|
+
- spec/fixtures/page.rb
|
114
|
+
- spec/fixtures/post.rb
|
115
|
+
- spec/mongo_utils/mongo_restore_spec.rb
|
116
|
+
- spec/mongo_utils/stripable_spec.rb
|
117
|
+
- spec/spec_helper.rb
|