with_lock_version 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +24 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gemfiles/rails_4_1.gemfile +8 -0
- data/gemfiles/rails_4_2.gemfile +5 -0
- data/gemfiles/rails_5_2.gemfile +5 -0
- data/lib/with_lock_version.rb +10 -0
- data/lib/with_lock_version/active_record_ext.rb +45 -0
- data/lib/with_lock_version/version.rb +3 -0
- data/with_lock_version.gemspec +27 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61345b1c79f51bf6ec291af01d7faebd5a7c957b
|
4
|
+
data.tar.gz: 815cb84a73de817896f3b42fffe9b91e637a598c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87e2ec27a7b63455c18a9f5851e0a2e033cb266bea42585d115d6a6ae24ffe9217e20983950d9ed97457499212feabb74b53e8a30f24c4ec7c83e9a29a73a116
|
7
|
+
data.tar.gz: d099b269b4db2d8909a656dbc09bd78ea3e2b679a069fcdef6159efaaacd41a1654c809fb61814a0d069a80a1d14ff8af744d5816c0f7b1f8fa607738f7153a4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
sudo: false
|
4
|
+
services:
|
5
|
+
- mysql
|
6
|
+
rvm:
|
7
|
+
- 2.2.4
|
8
|
+
- 2.3.0
|
9
|
+
- ruby-head
|
10
|
+
gemfile:
|
11
|
+
- gemfiles/rails_4_1.gemfile
|
12
|
+
- gemfiles/rails_4_2.gemfile
|
13
|
+
- gemfiles/rails_5_2.gemfile
|
14
|
+
before_install: gem install bundler -v 1.11.2
|
15
|
+
before_script:
|
16
|
+
- cp spec/database.yml.travis spec/database.yml
|
17
|
+
- mysql -e 'create database with_lock_version_test;'
|
18
|
+
script:
|
19
|
+
- bundle exec rspec
|
20
|
+
matrix:
|
21
|
+
allow_failures:
|
22
|
+
# NOTE: There are unstable versions
|
23
|
+
- rvm: ruby-head
|
24
|
+
- gemfile: gemfiles/rails_5_2.gemfile
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Akira Kusumoto
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# WithLockVersion
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/bluerabbit/with_lock_version.svg?branch=master)](https://travis-ci.org/bluerabbit/with_lock_version)
|
4
|
+
|
5
|
+
ActiveRecordでlock_versionカラムを使った楽観的ロックをする際に楽になるhelper methodを提供するgemです。
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'with_lock_version'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install with_lock_version
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
create_table :orders do |t|
|
27
|
+
t.integer :lock_version, default: 0, null: false
|
28
|
+
t.timestamps
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
class Order < ActiveRecord::Base
|
34
|
+
with_lock_versionable # required lock_version and timestamp columns
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
order = Order.find(id)
|
41
|
+
order.with_lock_version(params[:lock_version]) do
|
42
|
+
# something
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
lock_versionが一致しない場合はActiveRecord::StaleObjectErrorをraiseします。
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
```
|
51
|
+
%cp spec/database.yml.sample spec/database.yml
|
52
|
+
```
|
53
|
+
|
54
|
+
```
|
55
|
+
%bundle exec rspec
|
56
|
+
```
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bluerabbit/with_lock_version.
|
61
|
+
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "with_lock_version"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module WithLockVersion
|
2
|
+
module ActiveRecordExt
|
3
|
+
def self.included(model)
|
4
|
+
model.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def with_lock_versionable
|
9
|
+
include WithLockVersion::ActiveRecordExt::Core
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Core
|
14
|
+
def self.included(model)
|
15
|
+
model.extend ClassMethods
|
16
|
+
end
|
17
|
+
|
18
|
+
def with_lock_version(target_lock_version)
|
19
|
+
unless lock_version.to_i == target_lock_version.to_i
|
20
|
+
raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
with_lock do
|
24
|
+
unless lock_version.to_i == target_lock_version.to_i
|
25
|
+
raise_error
|
26
|
+
end
|
27
|
+
|
28
|
+
touch # Update lock_version + 1
|
29
|
+
|
30
|
+
yield
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def raise_error
|
37
|
+
if ActiveRecord::StaleObjectError.method(:new).arity == 0
|
38
|
+
raise ActiveRecord::StaleObjectError
|
39
|
+
else
|
40
|
+
raise ActiveRecord::StaleObjectError.new(self, "with_lock_version mismatch")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -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 'with_lock_version/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "with_lock_version"
|
8
|
+
spec.version = WithLockVersion::VERSION
|
9
|
+
spec.authors = ["Akira Kusumoto"]
|
10
|
+
spec.email = ["akirakusumo10@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Optimistic lock helper on ActiveRecord."
|
13
|
+
spec.description = "Optimistic lock helper on ActiveRecord."
|
14
|
+
spec.homepage = "https://github.com/bluerabbit/with_lock_version"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/})}
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f)}
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "rails"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "mysql2"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: with_lock_version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akira Kusumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mysql2
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Optimistic lock helper on ActiveRecord.
|
84
|
+
email:
|
85
|
+
- akirakusumo10@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- gemfiles/rails_4_1.gemfile
|
100
|
+
- gemfiles/rails_4_2.gemfile
|
101
|
+
- gemfiles/rails_5_2.gemfile
|
102
|
+
- lib/with_lock_version.rb
|
103
|
+
- lib/with_lock_version/active_record_ext.rb
|
104
|
+
- lib/with_lock_version/version.rb
|
105
|
+
- with_lock_version.gemspec
|
106
|
+
homepage: https://github.com/bluerabbit/with_lock_version
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.6.13
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Optimistic lock helper on ActiveRecord.
|
130
|
+
test_files: []
|