set_counters 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 +17 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +9 -0
- data/lib/set_counters/version.rb +3 -0
- data/lib/set_counters.rb +44 -0
- data/set_counters.gemspec +19 -0
- data/test/test_set_counters.rb +55 -0
- metadata +61 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Philip Hallstrom
|
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,42 @@
|
|
1
|
+
# SetCounters
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/set_counters)
|
4
|
+
|
5
|
+
Similar to ARModel.update\_counters, ARModel.set\_counters simply sets multiple
|
6
|
+
fields to their integer values. No adding/substracting, just setting.
|
7
|
+
Yes, this could be done using update\_all, but this seems nicer to me.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'set_counters'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install set_counters
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
# For the Post with id of 5, set the comment_count to 1, and
|
26
|
+
# set the action_count to 2
|
27
|
+
|
28
|
+
Post.set_counters 5, :comment_count => 1, :action_count => 2
|
29
|
+
|
30
|
+
# Executes the following SQL:
|
31
|
+
# UPDATE posts
|
32
|
+
# SET comment_count = 1,
|
33
|
+
# action_count = 2
|
34
|
+
# WHERE id = 5
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/set_counters.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'set_counters/version'
|
3
|
+
|
4
|
+
module SetCounters
|
5
|
+
module Extensions
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# A generic "counter setter" implementation, based on update_counters.
|
12
|
+
# It simply does a direct SQL update for the record with the given ID,
|
13
|
+
# setting the given hash of counters to the amount given by the
|
14
|
+
# corresponding value:
|
15
|
+
#
|
16
|
+
# ==== Parameters
|
17
|
+
#
|
18
|
+
# * +id+ - The id of the object you wish to update a counter on.
|
19
|
+
# * +counters+ - An Array of Hashes containing the names of the fields
|
20
|
+
# to update as keys and the amount to set the field to as values.
|
21
|
+
# Values will be forced to an integer value.
|
22
|
+
#
|
23
|
+
# ==== Examples
|
24
|
+
#
|
25
|
+
# # For the Post with id of 5, set the comment_count to 1, and
|
26
|
+
# # set the action_count to 2
|
27
|
+
# Post.set_counters 5, :comment_count => 1, :action_count => 2
|
28
|
+
# # Executes the following SQL:
|
29
|
+
# # UPDATE posts
|
30
|
+
# # SET comment_count = 1,
|
31
|
+
# # action_count = 2
|
32
|
+
# # WHERE id = 5
|
33
|
+
def set_counters(id, counters)
|
34
|
+
updates = counters.inject([]) { |list, (counter_name, value)|
|
35
|
+
list << "#{connection.quote_column_name(counter_name)} = #{value.to_i}"
|
36
|
+
}.join(", ")
|
37
|
+
update_all(updates, "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ActiveRecord::Base.send :include, SetCounters::Extensions
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'set_counters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "set_counters"
|
8
|
+
gem.version = SetCounters::VERSION
|
9
|
+
gem.authors = ["Philip Hallstrom"]
|
10
|
+
gem.email = ["philip@pjkh.com"]
|
11
|
+
gem.description = %q{Easy setting of multiple counters in Active Record models.}
|
12
|
+
gem.summary = %q{Easy setting of multiple counters in Active Record models.}
|
13
|
+
gem.homepage = "https://github.com/phallstrom/set_counters"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_record'
|
4
|
+
require 'set_counters'
|
5
|
+
|
6
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
7
|
+
|
8
|
+
def setup_db
|
9
|
+
silence_stream(STDOUT) do
|
10
|
+
ActiveRecord::Schema.define(:version => 1) do
|
11
|
+
create_table :widgets do |t|
|
12
|
+
t.column :x, :integer
|
13
|
+
t.column :y, :integer
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def teardown_db
|
20
|
+
silence_stream(STDOUT) do
|
21
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
22
|
+
ActiveRecord::Base.connection.drop_table(table)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Widget < ActiveRecord::Base
|
28
|
+
def self.table_name() "widgets" end
|
29
|
+
end
|
30
|
+
|
31
|
+
class SetCountersTest < Test::Unit::TestCase
|
32
|
+
|
33
|
+
def setup
|
34
|
+
setup_db
|
35
|
+
@w1 = Widget.create!(:x => 0, :y => 0)
|
36
|
+
@w2 = Widget.create!(:x => 0, :y => 0)
|
37
|
+
end
|
38
|
+
|
39
|
+
def teardown
|
40
|
+
teardown_db
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_set_counters
|
44
|
+
assert_equal 0, @w1.x
|
45
|
+
assert_equal 0, @w1.y
|
46
|
+
assert_equal 0, @w2.x
|
47
|
+
assert_equal 0, @w2.y
|
48
|
+
Widget.set_counters(@w2.id, :x => 1, :y => 2)
|
49
|
+
assert_equal 0, @w1.x
|
50
|
+
assert_equal 0, @w1.y
|
51
|
+
assert_equal 1, Widget.find(@w2.id).x
|
52
|
+
assert_equal 2, Widget.find(@w2.id).y
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: set_counters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Philip Hallstrom
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Easy setting of multiple counters in Active Record models.
|
15
|
+
email:
|
16
|
+
- philip@pjkh.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/set_counters.rb
|
27
|
+
- lib/set_counters/version.rb
|
28
|
+
- set_counters.gemspec
|
29
|
+
- test/test_set_counters.rb
|
30
|
+
homepage: https://github.com/phallstrom/set_counters
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
hash: 1413843189226588890
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
hash: 1413843189226588890
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.24
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Easy setting of multiple counters in Active Record models.
|
60
|
+
test_files:
|
61
|
+
- test/test_set_counters.rb
|