mongoid_adjust 0.1.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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.markdown +50 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/mongoid_adjust.rb +90 -0
- data/spec/.rspec +1 -0
- data/spec/mongoid_adjust_spec.rb +132 -0
- data/spec/spec_helper.rb +10 -0
- metadata +123 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kristian Mandrup
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Mongoid adjust
|
2
|
+
|
3
|
+
Adds adjust method to Mongoid::Document so you can adjust multiple items in a collection with the same mutation.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
<code>gem install mongoid_adjust</code>
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
<pre>
|
12
|
+
require 'mongoid_adjust'
|
13
|
+
|
14
|
+
# adjust all items where pos > 3, adding 1 to their position
|
15
|
+
list.items.where(:pos.gt => 3).adjust!(:pos => 1)
|
16
|
+
|
17
|
+
# Make name of each item uppercase
|
18
|
+
list.items.adjust!(:name => :upcase)
|
19
|
+
|
20
|
+
# Multiply all item positions by 2
|
21
|
+
list.items.adjust!(:pos => lambda{|i| i*2})
|
22
|
+
</pre>
|
23
|
+
|
24
|
+
+Note:+
|
25
|
+
|
26
|
+
This functionality was taken from Mongo Mapper where it builds on the native multi-update functionality exposed by Mongo DB.
|
27
|
+
Currently this project doesn't use the native Mongo DB functionality but that is a future goal!
|
28
|
+
|
29
|
+
## TODO
|
30
|
+
|
31
|
+
The adjust! should allow for an operator symbol (:add, :subtract, :multiply, :divide), defaulting to :add
|
32
|
+
<pre>
|
33
|
+
list.items.where(:pos.gt => 3).adjust!(:subtract, :pos => 1)
|
34
|
+
</pre>
|
35
|
+
|
36
|
+
The above can currently be achieved using <code>adjust!(:pos => -1)</code>, adding -1 to each.
|
37
|
+
|
38
|
+
## Note on Patches/Pull Requests
|
39
|
+
|
40
|
+
* Fork the project.
|
41
|
+
* Make your feature addition or bug fix.
|
42
|
+
* Add tests for it. This is important so I don't break it in a
|
43
|
+
future version unintentionally.
|
44
|
+
* Commit, do not mess with rakefile, version, or history.
|
45
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
46
|
+
* Send me a pull request. Bonus points for topic branches.
|
47
|
+
|
48
|
+
## Copyright
|
49
|
+
|
50
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mongoid_adjust"
|
8
|
+
gem.summary = %Q{Add simple adjust! method to Mongoid}
|
9
|
+
gem.description = %Q{Add simple adjust! method to Mongoid.}
|
10
|
+
gem.email = "kmandrup@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/kristianmandrup/mongoid_adjust"
|
12
|
+
gem.authors = ["Kristian Mandrup"]
|
13
|
+
gem.add_dependency("mongoid", ">= 2.0.0.beta.14")
|
14
|
+
gem.add_dependency("bson", ">= 1.0.3")
|
15
|
+
|
16
|
+
gem.add_development_dependency "rspec", ">= 2.0.0.beta.15"
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
# require 'spec/rake/spectask'
|
25
|
+
# Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
# spec.libs << 'lib' << 'spec'
|
27
|
+
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# Spec::Rake::SpecTask.new(:rcov) do |spec|
|
31
|
+
# spec.libs << 'lib' << 'spec'
|
32
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
33
|
+
# spec.rcov = true
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# task :spec => :check_dependencies
|
37
|
+
#
|
38
|
+
# task :default => :spec
|
39
|
+
#
|
40
|
+
# require 'rake/rdoctask'
|
41
|
+
# Rake::RDocTask.new do |rdoc|
|
42
|
+
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
43
|
+
#
|
44
|
+
# rdoc.rdoc_dir = 'rdoc'
|
45
|
+
# rdoc.title = "mongoid_adjust #{version}"
|
46
|
+
# rdoc.rdoc_files.include('README*')
|
47
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
# end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
module Mongoid::Extensions::Array
|
4
|
+
module Mutators
|
5
|
+
def adjust!(attrs = {})
|
6
|
+
attrs.each_pair do |key, value|
|
7
|
+
self.each do |doc|
|
8
|
+
doc.adjust!(attrs)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
self
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Mongoid
|
17
|
+
class Criteria
|
18
|
+
def adjust!(attrs = {})
|
19
|
+
to_a.adjust!(attrs)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Mongoid::Document
|
25
|
+
|
26
|
+
def has_key? key
|
27
|
+
@attributes[key.to_s] || respond_to?("#{key}=")
|
28
|
+
end
|
29
|
+
|
30
|
+
def adjust!(attrs = {})
|
31
|
+
(attrs || {}).each_pair do |key, value|
|
32
|
+
next if !has_key? key # only add to properties already present!
|
33
|
+
adjust_by_proc!(key, value) if value.kind_of?(Proc)
|
34
|
+
adjust_by_symbol!(key, value) if value.kind_of?(Symbol) || value.kind_of?(String)
|
35
|
+
adjust_by_number!(key, value) if value.kind_of?(Numeric) # only add integer values
|
36
|
+
end
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def adjust_by_proc! key, proc
|
43
|
+
if set_allowed?(key)
|
44
|
+
current_val = @attributes[key.to_s]
|
45
|
+
@attributes[key.to_s] = proc.call(current_val)
|
46
|
+
else
|
47
|
+
current_val = send("#{key}")
|
48
|
+
send("#{key}=", proc.call(current_val))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def adjust_by_symbol! key, name
|
53
|
+
method = name.to_sym
|
54
|
+
if set_allowed?(key)
|
55
|
+
current_val = @attributes[key.to_s]
|
56
|
+
@attributes[key.to_s] = current_val.send(method)
|
57
|
+
else
|
58
|
+
current_val = send("#{key}")
|
59
|
+
send("#{key}=", current_val.send(method))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def adjust_by_number! key, value
|
65
|
+
if set_allowed?(key)
|
66
|
+
current_val = @attributes[key.to_s] || 0
|
67
|
+
|
68
|
+
if current_val.kind_of? Numeric
|
69
|
+
@attributes[key.to_s] = current_val + value
|
70
|
+
end
|
71
|
+
else
|
72
|
+
current_val = send("#{key}") || 0
|
73
|
+
|
74
|
+
if current_val.kind_of? Numeric
|
75
|
+
send("#{key}=", current_val + value)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
class Array
|
83
|
+
include Mongoid::Extensions::Array::Mutators
|
84
|
+
end
|
85
|
+
|
86
|
+
class NilClass
|
87
|
+
def integer?
|
88
|
+
false
|
89
|
+
end
|
90
|
+
end
|
data/spec/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format nested
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
Mongoid.configure.master = Mongo::Connection.new.db('acts_as_list-test')
|
4
|
+
|
5
|
+
class Person
|
6
|
+
include Mongoid::Document
|
7
|
+
field :name, :type => String
|
8
|
+
embeds_many :lists
|
9
|
+
|
10
|
+
# after_update :do_it
|
11
|
+
# def do_it
|
12
|
+
# puts "hello"
|
13
|
+
# end
|
14
|
+
end
|
15
|
+
|
16
|
+
class PosPerson
|
17
|
+
include Mongoid::Document
|
18
|
+
field :pos, :type => Integer
|
19
|
+
field :name, :type => String
|
20
|
+
embeds_many :lists
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
class List
|
25
|
+
include Mongoid::Document
|
26
|
+
field :pos, :type => Integer
|
27
|
+
field :name, :type => String
|
28
|
+
embedded_in :person, :inverse_of => :lists
|
29
|
+
|
30
|
+
embeds_many :items
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
class Item
|
35
|
+
include Mongoid::Document
|
36
|
+
field :pos, :type => Integer
|
37
|
+
field :number, :type => Integer
|
38
|
+
|
39
|
+
field :assoc, :type => Symbol
|
40
|
+
embedded_in :list, :inverse_of => :items
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "MongoidAdjust" do
|
44
|
+
|
45
|
+
before :each do
|
46
|
+
@person = Person.create! :name => 'Kristian'
|
47
|
+
@pos_person = PosPerson.create! :name => 'Kristian'
|
48
|
+
# person.lists = []
|
49
|
+
list = List.new :pos => 1, :name => 'My list'
|
50
|
+
(1..3).each do |counter|
|
51
|
+
item = Item.new :pos => counter, :number => counter
|
52
|
+
list.items << item
|
53
|
+
end
|
54
|
+
@person.lists << list
|
55
|
+
@person.save!
|
56
|
+
end
|
57
|
+
|
58
|
+
after :each do
|
59
|
+
Mongoid.database.collections.each do |coll|
|
60
|
+
coll.remove
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#adjust! numeric' do
|
65
|
+
context 'on an array' do
|
66
|
+
it "should add 1 to all positions greater than 1" do
|
67
|
+
result = @person.lists[0].items.where(:pos.gt => 1).to_a.adjust!(:pos => 1)
|
68
|
+
result.map(&:pos).should == [3, 4]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'on a criteria' do
|
73
|
+
it "should add 1 to all positions greater than 1" do
|
74
|
+
result = @person.lists[0].items.where(:pos.gt => 1).adjust!(:pos => 1)
|
75
|
+
result.map(&:pos).should == [3, 4]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'on a document with a pos' do
|
80
|
+
it "should add 1 to the position" do
|
81
|
+
result = @pos_person.adjust!(:pos => 1)
|
82
|
+
result.pos.should == 1
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'on a document without a pos field' do
|
87
|
+
it "should NOT add 1 to the position" do
|
88
|
+
result = @person.adjust!(:pos => 1)
|
89
|
+
lambda {result.pos}.should raise_error
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '#adjust! by proc' do
|
95
|
+
context 'on an array' do
|
96
|
+
it "should times all positions (greater than 1) by 2" do
|
97
|
+
result = @person.lists[0].items.where(:pos.gt => 1).to_a.adjust!(:pos => lambda {|e| e * 2})
|
98
|
+
result.map(&:pos).should == [4, 6]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'on a criteria' do
|
103
|
+
it "should times all positions (greater than 1) by 2" do
|
104
|
+
result = @person.lists[0].items.where(:pos.gt => 1).adjust!(:pos => lambda {|e| e * 2})
|
105
|
+
result.map(&:pos).should == [4, 6]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'on a document with a name field' do
|
110
|
+
it "should upcase the name" do
|
111
|
+
result = @person.adjust!(:name => lambda {|e| e.upcase })
|
112
|
+
result.name.should == 'Kristian'.upcase
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '#adjust! by symbol and string' do
|
118
|
+
context 'on a document with a name field' do
|
119
|
+
it "should upcase the name - using string arg" do
|
120
|
+
result = @person.adjust!(:name => 'upcase')
|
121
|
+
result.name.should == 'Kristian'.upcase
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'on a document with a name field' do
|
126
|
+
it "should upcase the name - using symbol arg" do
|
127
|
+
result = @person.adjust!(:name => :upcase)
|
128
|
+
result.name.should == 'Kristian'.upcase
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_adjust
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kristian Mandrup
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-08 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mongoid
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- beta
|
33
|
+
- 14
|
34
|
+
version: 2.0.0.beta.14
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bson
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 3
|
49
|
+
version: 1.0.3
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: rspec
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 2
|
62
|
+
- 0
|
63
|
+
- 0
|
64
|
+
- beta
|
65
|
+
- 15
|
66
|
+
version: 2.0.0.beta.15
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
description: Add simple adjust! method to Mongoid.
|
70
|
+
email: kmandrup@gmail.com
|
71
|
+
executables: []
|
72
|
+
|
73
|
+
extensions: []
|
74
|
+
|
75
|
+
extra_rdoc_files:
|
76
|
+
- LICENSE
|
77
|
+
- README.markdown
|
78
|
+
files:
|
79
|
+
- .document
|
80
|
+
- .gitignore
|
81
|
+
- LICENSE
|
82
|
+
- Rakefile
|
83
|
+
- VERSION
|
84
|
+
- lib/mongoid_adjust.rb
|
85
|
+
- spec/.rspec
|
86
|
+
- spec/mongoid_adjust_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- README.markdown
|
89
|
+
has_rdoc: true
|
90
|
+
homepage: http://github.com/kristianmandrup/mongoid_adjust
|
91
|
+
licenses: []
|
92
|
+
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options:
|
95
|
+
- --charset=UTF-8
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.3.7
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: Add simple adjust! method to Mongoid
|
121
|
+
test_files:
|
122
|
+
- spec/mongoid_adjust_spec.rb
|
123
|
+
- spec/spec_helper.rb
|