mongomapper_plugins 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2010 by Andrew Timberlake
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ = MongoMapper Plugins
2
+
3
+ == Versioned Update
4
+
5
+ This plugin implements the "Update if Current" (see Mongo documentation[http://www.mongodb.org/display/DOCS/Atomic+Operations#AtomicOperations-%22UpdateifCurrent%22]) Atomic operation.
6
+
7
+ The plugin will add a <code>_version</code> key to your model and increment it on each save.
8
+ Each save will ensure that it only saves if the database contains the record with the same version at the time of save and raises an InvalidVersion error if the versions do not match.
9
+
10
+ ====Usage
11
+
12
+ class Model
13
+ include MongoMapper::Doument
14
+ versioned_update
15
+ end
16
+
17
+ == Updating Modifiers
18
+
19
+ This plugin will update the internal value of a model when using modifiers like <code>model.set</code>, <code>model.increment</code>, <code>model.decrement</code>, <code>model.push</code>, <code>model.pull</code> and <code>model.push_uniq</code>
20
+
21
+ ====Usage
22
+
23
+ #in initializer
24
+ MongoMapper::Document.append_inclusions MongoMapper::Plugins::UpdatingModifiers::Addition
25
+
26
+ #Use your model as usual
27
+ counter => Counter.create(:count => 1)
28
+ counter.increment(:count, 1)
29
+ counter.count #=> 2
@@ -0,0 +1,83 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module UpdatingModifiers
4
+ def self.configure(mod)
5
+ end
6
+
7
+ module InstanceMethods
8
+ def set(hash)
9
+ hash.each do |key, value|
10
+ obj, method = get_obj_and_method(key)
11
+ obj.send("#{method}=", value)
12
+ end
13
+ super
14
+ end
15
+
16
+ def increment(hash)
17
+ hash.each do |key, value|
18
+ obj, method = get_obj_and_method(key)
19
+ val = obj.send(method)
20
+ obj.send("#{method}=", val + value)
21
+ end
22
+ super
23
+ end
24
+
25
+ def decrement(hash)
26
+ hash.each do |key, value|
27
+ obj, method = get_obj_and_method(key)
28
+ val = obj.send(method)
29
+ obj.send("#{method}=", val - value)
30
+ end
31
+ super
32
+ end
33
+
34
+ def push(hash)
35
+ hash.each do |key, value|
36
+ obj, method = get_obj_and_method(key)
37
+ obj = obj.send(method)
38
+ obj.push value
39
+ end
40
+ super
41
+ end
42
+
43
+ def pull(hash)
44
+ hash.each do |key, value|
45
+ obj, method = get_obj_and_method(key)
46
+ obj = obj.send(method)
47
+ obj.delete_if { |e| e == value }
48
+ end
49
+ super
50
+ end
51
+
52
+ def add_to_set(hash)
53
+ hash.each do |key, value|
54
+ obj, method = get_obj_and_method(key)
55
+ obj = obj.send(method)
56
+ obj.push(value) unless obj.include?(value)
57
+ end
58
+ super
59
+ end
60
+ alias push_uniq add_to_set
61
+
62
+ private
63
+ def get_obj_and_method(key)
64
+ children = key.to_s.split(/\./)
65
+ method = children.pop
66
+
67
+ obj = self
68
+ children.each do |child|
69
+ obj = obj.send(child)
70
+ end
71
+
72
+ [obj, method]
73
+ end
74
+ end
75
+
76
+ module Addition
77
+ def self.included(model)
78
+ model.plugin UpdatingModifiers
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,43 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module VersionedUpdate
4
+ def self.configure(mod)
5
+ mod.class_eval {
6
+ key :_version, Integer, :default => 1
7
+ }
8
+ end
9
+
10
+ module ClassMethods
11
+ def versioned_update
12
+ plugin MongoMapper::Plugins::VersionedUpdate
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+ private
18
+ def update(options={})
19
+ version = self._version
20
+ self._version += 1
21
+ ret = collection.update({:_id => _id, :_version => version},
22
+ to_mongo,
23
+ :safe => true)
24
+ if ret['n'] == 0
25
+ self._version -= 1
26
+ raise InvalidVersion
27
+ end
28
+ ret['err'].nil?
29
+ end
30
+ end
31
+
32
+ class InvalidVersion < StandardError; end
33
+
34
+ module Addition
35
+ def self.included(model)
36
+ model.plugin VersionedUpdate
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ MongoMapper::Document.append_inclusions MongoMapper::Plugins::VersionedUpdate::Addition
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongomapper_plugins
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Timberlake
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-28 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "1.0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ description: A repository of MongoMapper plugins.
28
+ email: andrew@andrewtimberlake.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files:
34
+ - LICENSE
35
+ - README.rdoc
36
+ files:
37
+ - LICENSE
38
+ - lib/mongo_mapper/plugins/updating_modifiers.rb
39
+ - lib/mongo_mapper/plugins/versioned_update.rb
40
+ - README.rdoc
41
+ has_rdoc: true
42
+ homepage: http://github.com/andrewtimberlake/mongomapper_plugins
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.5.2
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: A repository of MongoMapper plugins.
69
+ test_files: []
70
+