increment_with_sql 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 ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ gemfiles/*.lock
data/.travis.yml ADDED
@@ -0,0 +1,34 @@
1
+
2
+ addons:
3
+ postgresql: "9.3"
4
+
5
+ before_script:
6
+ - mysql -e 'create database increment_with_sql;'
7
+ - psql -c 'create database increment_with_sql;' -U postgres
8
+
9
+ rvm:
10
+ - 1.9.3
11
+ - 2.0.0
12
+ - 2.1.1
13
+ - rbx-2
14
+ - jruby
15
+ env:
16
+ - DATABASE=sqlite
17
+ - DATABASE=mysql
18
+ - DATABASE=postgres
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: rbx-2
22
+ - rvm: jruby
23
+
24
+ gemfile:
25
+ - gemfiles/3.2.gemfile
26
+ - gemfiles/4.0.gemfile
27
+ - gemfiles/4.1.gemfile
28
+ - gemfiles/4.2.gemfile
29
+
30
+ install:
31
+ - "travis_retry bundle install"
32
+
33
+ script: "bundle exec rake test"
34
+
data/Appraisals ADDED
@@ -0,0 +1,21 @@
1
+
2
+ appraise "3.2" do
3
+ gem "activerecord", "~> 3.2.18"
4
+ gem "increment_with_sql", :path => "../"
5
+ end
6
+
7
+ appraise "4.0" do
8
+ gem "activerecord", "~> 4.0.0"
9
+ gem "increment_with_sql", :path => "../"
10
+ end
11
+
12
+ appraise "4.1" do
13
+ gem "activerecord", "~> 4.1.0.beta"
14
+ gem "increment_with_sql", :path => "../"
15
+ end
16
+
17
+ appraise "4.2" do
18
+ gem "activerecord", "~> 4.2.0.beta"
19
+ gem "increment_with_sql", :path => "../"
20
+ end
21
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in increment_with_sql.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Benjamin Vetter
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,31 @@
1
+ # IncrementWithSql
2
+
3
+ [![Build Status](https://secure.travis-ci.org/mrkamel/increment_with_sql.png?branch=master)](http://travis-ci.org/mrkamel/increment_with_sql)
4
+ [![Dependency Status](https://gemnasium.com/mrkamel/increment_with_sql.png?travis)](https://gemnasium.com/mrkamel/increment_with_sql)
5
+ [![Gem Version](https://badge.fury.io/rb/increment_with_sql.svg)](http://badge.fury.io/rb/increment_with_sql)
6
+
7
+ Provides `#increment_with_sql!` and `#decrement_with_sql!` for ActiveRecord::Base
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'increment_with_sql'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install increment_with_sql
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/increment_with_sql/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "lib"
6
+ t.pattern = "test/**/*_test.rb"
7
+ t.verbose = true
8
+ end
9
+
@@ -0,0 +1,26 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.2.18"
6
+ gem "increment_with_sql", :path => "../"
7
+
8
+ platforms :jruby do
9
+ gem "activerecord-jdbcmysql-adapter"
10
+ gem "activerecord-jdbcsqlite3-adapter"
11
+ gem "activerecord-jdbcpostgresql-adapter"
12
+ end
13
+
14
+ platforms :ruby do
15
+ gem "sqlite3"
16
+ gem "mysql2"
17
+ gem "pg"
18
+ end
19
+
20
+ platforms :rbx do
21
+ gem "racc"
22
+ gem "rubysl", "~> 2.0"
23
+ gem "psych"
24
+ end
25
+
26
+ gemspec :path => "../"
@@ -0,0 +1,26 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.0.0"
6
+ gem "increment_with_sql", :path => "../"
7
+
8
+ platforms :jruby do
9
+ gem "activerecord-jdbcmysql-adapter"
10
+ gem "activerecord-jdbcsqlite3-adapter"
11
+ gem "activerecord-jdbcpostgresql-adapter"
12
+ end
13
+
14
+ platforms :ruby do
15
+ gem "sqlite3"
16
+ gem "mysql2"
17
+ gem "pg"
18
+ end
19
+
20
+ platforms :rbx do
21
+ gem "racc"
22
+ gem "rubysl", "~> 2.0"
23
+ gem "psych"
24
+ end
25
+
26
+ gemspec :path => "../"
@@ -0,0 +1,26 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.1.0.beta"
6
+ gem "increment_with_sql", :path => "../"
7
+
8
+ platforms :jruby do
9
+ gem "activerecord-jdbcmysql-adapter"
10
+ gem "activerecord-jdbcsqlite3-adapter"
11
+ gem "activerecord-jdbcpostgresql-adapter"
12
+ end
13
+
14
+ platforms :ruby do
15
+ gem "sqlite3"
16
+ gem "mysql2"
17
+ gem "pg"
18
+ end
19
+
20
+ platforms :rbx do
21
+ gem "racc"
22
+ gem "rubysl", "~> 2.0"
23
+ gem "psych"
24
+ end
25
+
26
+ gemspec :path => "../"
@@ -0,0 +1,26 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2.0.beta"
6
+ gem "increment_with_sql", :path => "../"
7
+
8
+ platforms :jruby do
9
+ gem "activerecord-jdbcmysql-adapter"
10
+ gem "activerecord-jdbcsqlite3-adapter"
11
+ gem "activerecord-jdbcpostgresql-adapter"
12
+ end
13
+
14
+ platforms :ruby do
15
+ gem "sqlite3"
16
+ gem "mysql2"
17
+ gem "pg"
18
+ end
19
+
20
+ platforms :rbx do
21
+ gem "racc"
22
+ gem "rubysl", "~> 2.0"
23
+ gem "psych"
24
+ end
25
+
26
+ gemspec :path => "../"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'increment_with_sql/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "increment_with_sql"
8
+ spec.version = IncrementWithSql::VERSION
9
+ spec.authors = ["Benjamin Vetter"]
10
+ spec.email = ["vetter@plainpicture.de"]
11
+ spec.summary = %q{Provides increment_with_sql! and decrement_with_sql! for ActiveRecord models}
12
+ spec.description = %q{Provides increment_with_sql! and decrement_with_sql! for ActiveRecord models}
13
+ spec.homepage = "https://github.com/mrkamel/increment_with_sql"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "activerecord", ">= 3.0.0"
24
+ spec.add_development_dependency "appraisal"
25
+ spec.add_development_dependency "minitest"
26
+ end
27
+
@@ -0,0 +1,3 @@
1
+ module IncrementWithSql
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+
2
+ require "increment_with_sql/version"
3
+ require "active_record"
4
+
5
+ module IncrementWithSql
6
+ class NotPersistedError < StandardError; end
7
+ class InvalidAttributeError < StandardError; end
8
+
9
+ def increment_with_sql!(attribute, by = 1)
10
+ raise(NotPersistedError, "Record not persisted") if new_record?
11
+ raise(InvalidAttributeError, "Invalid attribute #{attribute}") unless attribute_names.include?(attribute.to_s)
12
+
13
+ self.class.transaction do
14
+ self.class.where(:id => id).update_all("#{self.class.connection.quote_column_name attribute} = CASE WHEN #{self.class.connection.quote_column_name attribute} IS NULL THEN 0 ELSE #{self.class.connection.quote_column_name attribute} END + #{by.to_i}")
15
+
16
+ send "#{attribute}=", self.class.where(:id => id).select(attribute).first.send(attribute)
17
+
18
+ if respond_to?(:clear_attribute_changes, true)
19
+ send :clear_attribute_changes, attribute
20
+ else
21
+ changed_attributes.except! attribute.to_s
22
+ end
23
+ end
24
+
25
+ self
26
+ end
27
+
28
+ def decrement_with_sql!(attribute, by = 1)
29
+ increment_with_sql! attribute, by * -1
30
+ end
31
+ end
32
+
33
+ ActiveRecord::Base.send :include, IncrementWithSql
34
+
data/test/database.yml ADDED
@@ -0,0 +1,17 @@
1
+
2
+ sqlite:
3
+ adapter: sqlite3
4
+ database: ":memory:"
5
+
6
+ mysql:
7
+ adapter: mysql2
8
+ database: increment_with_sql
9
+ username: root
10
+ encoding: utf8
11
+
12
+ postgres:
13
+ adapter: postgresql
14
+ database: increment_with_sql
15
+ username: postgres
16
+ encoding: utf8
17
+
@@ -0,0 +1,65 @@
1
+
2
+ require File.expand_path("../test_helper", __FILE__)
3
+
4
+ class IncrementWithSqlTest < IncrementWithSql::TestCase
5
+ def test_new_record
6
+ assert_raises IncrementWithSql::NotPersistedError do
7
+ Item.new.increment_with_sql! :manual_version
8
+ end
9
+ end
10
+
11
+ def test_invalid_attribute
12
+ assert_raises IncrementWithSql::InvalidAttributeError do
13
+ Item.create!.increment_with_sql! :unknown_version
14
+ end
15
+ end
16
+
17
+ def test_increment_with_sql!
18
+ assert_equal 1, Item.create!.increment_with_sql!(:manual_version).manual_version
19
+ assert_equal 4, Item.create!(:manual_version => 2).increment_with_sql!(:manual_version, 2).manual_version
20
+ end
21
+
22
+ def test_decrement_with_sql!
23
+ assert_equal -1, Item.create!.decrement_with_sql!(:manual_version).manual_version
24
+ assert_equal 2, Item.create!(:manual_version => 4).decrement_with_sql!(:manual_version, 2).manual_version
25
+ end
26
+
27
+ def test_indifferent_access
28
+ assert_equal 2, Item.create!(:manual_version => 1).increment_with_sql!(:manual_version).manual_version
29
+ assert_equal 2, Item.create!(:manual_version => 1).increment_with_sql!("manual_version").manual_version
30
+ end
31
+
32
+ def test_manual_changes
33
+ assert Item.create!(:manual_version => 1).increment_with_sql!(:manual_version).changes.blank?
34
+ refute Item.create!(:manual_version => 1).increment_with_sql!(:manual_version).manual_version_changed?
35
+
36
+ item = Item.create!(:manual_version => 2).increment_with_sql!(:manual_version)
37
+ Item.update_all :manual_version => 0
38
+ item.save!
39
+
40
+ assert_equal 0, Item.find(item.id).manual_version
41
+ end
42
+
43
+ def test_automatic_changes
44
+ item = Item.create!
45
+ item.update_attributes! :content => "Content"
46
+
47
+ assert_equal 2, item.automatic_version
48
+ assert item.changes.blank?
49
+ refute item.content_changed?
50
+ refute item.automatic_version_changed?
51
+ end
52
+
53
+ def test_mixed_changes
54
+ item = Item.create!(:manual_version => 1)
55
+ item.content = "Content"
56
+ item.increment_with_sql! :manual_version
57
+
58
+ assert_equal 2, item.manual_version
59
+
60
+ refute item.changes.blank?
61
+ refute item.manual_version_changed?
62
+ assert item.content_changed?
63
+ end
64
+ end
65
+
@@ -0,0 +1,39 @@
1
+
2
+ require "increment_with_sql"
3
+
4
+ begin
5
+ require "minitest"
6
+
7
+ class IncrementWithSql::TestCase < MiniTest::Test; end
8
+ rescue LoadError
9
+ require "minitest/unit"
10
+
11
+ class IncrementWithSql::TestCase < MiniTest::Unit::TestCase; end
12
+ end
13
+
14
+ require "minitest/autorun"
15
+ require "active_record"
16
+ require "yaml"
17
+
18
+ DATABASE = ENV["DATABASE"] || "sqlite"
19
+
20
+ ActiveRecord::Base.establish_connection YAML.load_file(File.expand_path("../database.yml", __FILE__))[DATABASE]
21
+
22
+ ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS items"
23
+
24
+ ActiveRecord::Base.connection.create_table :items do |t|
25
+ t.integer :automatic_version
26
+ t.integer :manual_version
27
+ t.string :content
28
+ end
29
+
30
+ class Item < ActiveRecord::Base
31
+ after_save { increment_with_sql! :automatic_version }
32
+ end
33
+
34
+ class IncrementWithSql::TestCase
35
+ def teardown
36
+ Item.delete_all
37
+ end
38
+ end
39
+
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: increment_with_sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benjamin Vetter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-04-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.7'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '10.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: '10.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activerecord
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.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: 3.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: appraisal
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Provides increment_with_sql! and decrement_with_sql! for ActiveRecord
95
+ models
96
+ email:
97
+ - vetter@plainpicture.de
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - .travis.yml
104
+ - Appraisals
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - gemfiles/3.2.gemfile
110
+ - gemfiles/4.0.gemfile
111
+ - gemfiles/4.1.gemfile
112
+ - gemfiles/4.2.gemfile
113
+ - increment_with_sql.gemspec
114
+ - lib/increment_with_sql.rb
115
+ - lib/increment_with_sql/version.rb
116
+ - test/database.yml
117
+ - test/increment_with_sql_test.rb
118
+ - test/test_helper.rb
119
+ homepage: https://github.com/mrkamel/increment_with_sql
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.23
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Provides increment_with_sql! and decrement_with_sql! for ActiveRecord models
144
+ test_files:
145
+ - test/database.yml
146
+ - test/increment_with_sql_test.rb
147
+ - test/test_helper.rb