rails_atomic_increment 0.2
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 +1 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +1 -0
- data/LICENSE +20 -0
- data/README.md +25 -0
- data/Rakefile +31 -0
- data/lib/rails_atomic_increment.rb +44 -0
- data/rails_atomic_increment.gemspec +26 -0
- data/test/test_helper.rb +0 -0
- metadata +83 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Gonçalo Silva
|
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.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Atomic increments allow you to avoid problems with a concurrent access to a counter:
|
2
|
+
http://www.alfreddd.com/2011/01/atomic-increment-in-rails.html
|
3
|
+
|
4
|
+
Install:
|
5
|
+
|
6
|
+
gem install rails_atomic_increment
|
7
|
+
|
8
|
+
|
9
|
+
Usage:
|
10
|
+
|
11
|
+
user = User.first
|
12
|
+
user.account_balance # => 100
|
13
|
+
user.atomic_increment!(:account_balance, 2)
|
14
|
+
user.account_balance # => 102
|
15
|
+
|
16
|
+
user.atomic_increment!('account_balance', 2, :reload) # reloads from DB
|
17
|
+
user.account_balance # could be > 104 if it was updated by another process
|
18
|
+
|
19
|
+
user.login_attempts # => 7
|
20
|
+
user.page_views # => 1000
|
21
|
+
user.increment_counters!([:login_attempts, :page_views]
|
22
|
+
user.login_attempts # => 8
|
23
|
+
user.page_views # => 1001
|
24
|
+
|
25
|
+
Copyright (c) 2011 Josh Shupack
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
begin
|
2
|
+
require "bundler"
|
3
|
+
Bundler.setup
|
4
|
+
rescue LoadError
|
5
|
+
$stderr.puts "You need to have Bundler installed to be able build this gem."
|
6
|
+
end
|
7
|
+
|
8
|
+
gemspec = eval(File.read(Dir["*.gemspec"].first))
|
9
|
+
|
10
|
+
|
11
|
+
desc "Validate the gemspec"
|
12
|
+
task :gemspec do
|
13
|
+
gemspec.validate
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Build gem locally"
|
17
|
+
task :build => :gemspec do
|
18
|
+
system "gem build #{gemspec.name}.gemspec"
|
19
|
+
FileUtils.mkdir_p "pkg"
|
20
|
+
FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Install gem locally"
|
24
|
+
task :install => :build do
|
25
|
+
system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Clean automatically generated files"
|
29
|
+
task :clean do
|
30
|
+
FileUtils.rm_rf "pkg"
|
31
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class ActiveRecord::Base
|
2
|
+
|
3
|
+
# These methods are to update counters atomically so we don't have any issues with counters being off because of concurrent increment or decriment requests
|
4
|
+
|
5
|
+
def atomic_increment!(attribute, by = 1, reload = false)
|
6
|
+
self.update_counters!({attribute => by}, reload)
|
7
|
+
end
|
8
|
+
|
9
|
+
def atomic_decrement!(attribute, by = 1, reload = false)
|
10
|
+
by = -by #invert the sign so we're subtracting the passed in value from the current value
|
11
|
+
self.update_counters!({attribute => by}, reload)
|
12
|
+
end
|
13
|
+
|
14
|
+
# this expects an array of counter attributes to update and turns it into a hash to pass to update_counters
|
15
|
+
def increment_counters!(counters, reload = false)
|
16
|
+
self.update_counters!(counters.inject({}) {|h,c| h.merge(c => 1)}, reload)
|
17
|
+
end
|
18
|
+
|
19
|
+
# this expects an array of counter attributes to update and turns it into a hash to pass to update_counters
|
20
|
+
def decrement_counters!(counters, reload = false)
|
21
|
+
self.update_counters!(counters.inject({}) {|h,c| h.merge(c => -1)}, reload)
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_counters!(counters, reload = false)
|
25
|
+
counters.each do |attribute, value|
|
26
|
+
raise "attribute '#{attribute}' can NOT be incremented because it was already changed and that change will be lost." if !new_record? && changed_attributes[attribute.to_s]
|
27
|
+
self[attribute] ||= 0
|
28
|
+
self[attribute] += value
|
29
|
+
@changed_attributes.delete(attribute.to_s) unless new_record? # mark this column as unchanged so it won't get updated in the DB if a save is performed on the object!
|
30
|
+
end
|
31
|
+
if new_record?
|
32
|
+
self.save
|
33
|
+
else
|
34
|
+
self.class.update_counters(self.id, counters)
|
35
|
+
end
|
36
|
+
self.reload if reload # if we care about the new numbers in the db after the update
|
37
|
+
end
|
38
|
+
|
39
|
+
# this is for database table maintenance - May only work on MySQL? Vacuum for Postgres
|
40
|
+
def self.optimize_table
|
41
|
+
connection.execute("OPTIMIZE TABLE #{quoted_table_name}")
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "rails_atomic_increment"
|
3
|
+
s.version = "0.2"
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.authors = ["Josh Shupack"]
|
6
|
+
s.email = ["yNaught@gmail.com"]
|
7
|
+
s.homepage = "http://github.com/imme5150/rails_atomic_increment"
|
8
|
+
s.summary = "Adds atomic_increment! and atomic_decrement! to ActiveRecord models"
|
9
|
+
s.description = "Allows you to use atomic inrement and decriment from the model instead of having to call the class. Much more object oriented"
|
10
|
+
s.rubyforge_project = s.name
|
11
|
+
|
12
|
+
s.required_rubygems_version = ">= 1.3.6"
|
13
|
+
|
14
|
+
# If you have runtime dependencies, add them here
|
15
|
+
s.add_runtime_dependency "rails", "> 2"
|
16
|
+
|
17
|
+
# If you have development dependencies, add them here
|
18
|
+
# s.add_development_dependency "another", "= 0.9"
|
19
|
+
|
20
|
+
# The list of files to be contained in the gem
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
# s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
23
|
+
# s.extensions = `git ls-files ext/extconf.rb`.split("\n")
|
24
|
+
|
25
|
+
s.require_path = 'lib'
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_atomic_increment
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
version: "0.2"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Josh Shupack
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2011-11-22 00:00:00 -08:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: rails
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 2
|
28
|
+
version: "2"
|
29
|
+
type: :runtime
|
30
|
+
version_requirements: *id001
|
31
|
+
description: Allows you to use atomic inrement and decriment from the model instead of having to call the class. Much more object oriented
|
32
|
+
email:
|
33
|
+
- yNaught@gmail.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files: []
|
39
|
+
|
40
|
+
files:
|
41
|
+
- .gitignore
|
42
|
+
- CHANGELOG.md
|
43
|
+
- Gemfile
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- lib/rails_atomic_increment.rb
|
48
|
+
- rails_atomic_increment.gemspec
|
49
|
+
- test/test_helper.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/imme5150/rails_atomic_increment
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 1
|
72
|
+
- 3
|
73
|
+
- 6
|
74
|
+
version: 1.3.6
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: rails_atomic_increment
|
78
|
+
rubygems_version: 1.3.6
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Adds atomic_increment! and atomic_decrement! to ActiveRecord models
|
82
|
+
test_files: []
|
83
|
+
|