delta_attributes 0.0.4 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +1 -1
- data/delta_attributes.gemspec +8 -7
- data/lib/delta_attributes.rb +9 -2
- data/lib/{delta_attributes → delta_attributes3_2}/crud.rb +0 -0
- data/lib/{delta_attributes → delta_attributes3_2}/main.rb +6 -6
- data/lib/{delta_attributes → delta_attributes3_2}/mysql.rb +0 -0
- data/lib/delta_attributes3_2/railtie.rb +9 -0
- data/lib/{delta_attributes → delta_attributes3_2}/to_sql.rb +0 -0
- data/lib/{delta_attributes → delta_attributes3_2}/update_manager.rb +0 -0
- data/lib/{delta_attributes → delta_attributes3_2}/update_statement.rb +0 -0
- data/lib/{delta_attributes → delta_attributes3_2}/visitor.rb +0 -0
- data/lib/delta_attributes4/bind_visitor.rb +25 -0
- data/lib/delta_attributes4/main.rb +39 -0
- data/lib/delta_attributes4/persistence.rb +36 -0
- data/lib/delta_attributes4/railtie.rb +9 -0
- data/lib/{delta_attributes → shared}/version.rb +1 -1
- metadata +59 -72
- data/lib/delta_attributes/railtie.rb +0 -8
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a71dc316af524b943bef7a6af497e90077d4c636
|
4
|
+
data.tar.gz: 44411f7fcf26f7784227866a2eefd07c63911fef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 418d3a188bb3b6cdc36eb1d918ae3fdc4fc6493c27fbf2dd12785f188c9fd5f83e6f09ea0b7723f44cfd7802905a5f911ec55b9e743bf951a51cb74231f074a2
|
7
|
+
data.tar.gz: 5d6e5763c209d8d4eda6de700930e12afa3e544cd282cc664d5e05c57c2f5e487501b739bc9261bcf00881fd2e5eb5c8409a9ff079f4f8665f3b56c55be893a7
|
data/README.md
CHANGED
@@ -78,7 +78,7 @@ Or install it yourself as:
|
|
78
78
|
It doesn't matter as delta is calculated as difference between original value received from database and new value
|
79
79
|
before update.
|
80
80
|
|
81
|
-
Tested with rails
|
81
|
+
Tested with rails 4.0, 3.2.9, 3.2.8, 3.2.0, 3.1.0
|
82
82
|
|
83
83
|
## Contributing
|
84
84
|
|
data/delta_attributes.gemspec
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require '
|
4
|
+
require 'shared/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
|
-
gem.name =
|
7
|
+
gem.name = 'delta_attributes'
|
8
8
|
gem.version = DeltaAttributes::VERSION
|
9
|
-
gem.authors = [
|
10
|
-
gem.email =
|
9
|
+
gem.authors = ['Oleh Novosad, Yuriy Lavryk, Arya']
|
10
|
+
gem.email = %w(oleh.novosad@gmail.com)
|
11
11
|
gem.description = %q{
|
12
12
|
This gem makes updating specified number fields by ActiveRecord in unusual way.
|
13
13
|
|
@@ -31,11 +31,12 @@ Gem::Specification.new do |gem|
|
|
31
31
|
Source code: https://github.com/izbor/delta_attributes
|
32
32
|
}
|
33
33
|
gem.summary = %q{ delta attributes }
|
34
|
-
gem.homepage =
|
34
|
+
gem.homepage = 'https://github.com/izbor/delta_attributes'
|
35
35
|
|
36
36
|
gem.files = `git ls-files`.split($/)
|
37
37
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
38
38
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
39
|
-
gem.require_paths =
|
40
|
-
gem.add_dependency
|
39
|
+
gem.require_paths = %w(lib)
|
40
|
+
gem.add_dependency 'rails', '>= 3.2'
|
41
|
+
gem.license = 'MIT'
|
41
42
|
end
|
data/lib/delta_attributes.rb
CHANGED
@@ -1,2 +1,9 @@
|
|
1
|
-
require
|
2
|
-
|
1
|
+
require 'shared/version'
|
2
|
+
|
3
|
+
if defined?(Rails)
|
4
|
+
|
5
|
+
load 'delta_attributes3_2/railtie.rb' if Rails::VERSION::MAJOR == 3
|
6
|
+
|
7
|
+
load 'delta_attributes4/railtie.rb' if Rails::VERSION::MAJOR == 4
|
8
|
+
|
9
|
+
end
|
File without changes
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'set'
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
2
|
+
require 'delta_attributes3_2/update_statement'
|
3
|
+
require 'delta_attributes3_2/update_manager'
|
4
|
+
require 'delta_attributes3_2/to_sql'
|
5
|
+
require 'delta_attributes3_2/mysql'
|
6
|
+
require 'delta_attributes3_2/crud'
|
7
|
+
require 'delta_attributes3_2/visitor'
|
8
8
|
|
9
9
|
module ActiveRecord
|
10
10
|
# = Active Record Persistence
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Arel
|
2
|
+
module Visitors
|
3
|
+
module BindVisitor
|
4
|
+
private
|
5
|
+
|
6
|
+
def visit_Arel_Nodes_Assignment(o)
|
7
|
+
if o.right.is_a? Arel::Nodes::BindParam
|
8
|
+
if o.left && o.left.expr && o.left.expr.relation \
|
9
|
+
&& o.left.expr.relation.engine \
|
10
|
+
&& o.left.expr.relation.engine.respond_to?(:delta_attributes) \
|
11
|
+
&& o.left.expr.relation.engine.delta_attributes.include?(o.left.name)
|
12
|
+
|
13
|
+
l = visit o.left
|
14
|
+
"#{l} = #{l} + #{visit o.right}"
|
15
|
+
else
|
16
|
+
"#{visit o.left} = #{visit o.right}"
|
17
|
+
end
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'delta_attributes4/bind_visitor'
|
2
|
+
require 'delta_attributes4/persistence'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
# = Active Record Persistence
|
6
|
+
module Persistence
|
7
|
+
|
8
|
+
class InvalidDeltaColumn < StandardError;
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(base) #:nodoc:
|
12
|
+
base.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
def delta_attributes(*args)
|
18
|
+
@_delta_attributes ||= Set.new
|
19
|
+
|
20
|
+
return @_delta_attributes if args.empty?
|
21
|
+
|
22
|
+
args.each do |attribute|
|
23
|
+
|
24
|
+
if self.columns_hash[attribute.to_s].blank?
|
25
|
+
raise InvalidDeltaColumn.new("#{self.to_s} model doesn't have attribute with name '#{attribute}.")
|
26
|
+
end
|
27
|
+
|
28
|
+
if self.columns_hash[attribute.to_s] && !self.columns_hash[attribute.to_s].number?
|
29
|
+
raise InvalidDeltaColumn.new("Delta attributes only work with number attributes, column `#{attribute}` is not a number.")
|
30
|
+
end
|
31
|
+
|
32
|
+
@_delta_attributes.add(attribute.to_s)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
# = Active Record Persistence
|
3
|
+
module Persistence
|
4
|
+
|
5
|
+
# Updates the associated record with values matching those of the instance attributes.
|
6
|
+
# Returns the number of affected rows.
|
7
|
+
def update_record(attribute_names = @attributes.keys)
|
8
|
+
attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
|
9
|
+
if attributes_with_values.empty?
|
10
|
+
0
|
11
|
+
else
|
12
|
+
klass = self.class
|
13
|
+
column_hash = klass.connection.schema_cache.columns_hash klass.table_name
|
14
|
+
db_columns_with_values = attributes_with_values.map { |attr,value|
|
15
|
+
real_column = column_hash[attr.name]
|
16
|
+
|
17
|
+
v = value
|
18
|
+
if self.class.respond_to?(:delta_attributes) && self.class.delta_attributes.include?(attr.name)
|
19
|
+
if @changed_attributes.include?(attr.name)
|
20
|
+
v = value - @changed_attributes[attr.name]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
[real_column, v]
|
25
|
+
}
|
26
|
+
bind_attrs = attributes_with_values.dup
|
27
|
+
bind_attrs.keys.each_with_index do |column, i|
|
28
|
+
real_column = db_columns_with_values[i].first
|
29
|
+
bind_attrs[column] = klass.connection.substitute_at(real_column, i)
|
30
|
+
end
|
31
|
+
stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id_was || id)).arel.compile_update(bind_attrs)
|
32
|
+
klass.connection.update stmt, 'SQL', db_columns_with_values
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
CHANGED
@@ -1,49 +1,43 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: delta_attributes
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 4
|
10
|
-
version: 0.0.4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
13
|
-
- Oleh
|
6
|
+
authors:
|
7
|
+
- Oleh Novosad, Yuriy Lavryk, Arya
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2013-08-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
22
14
|
name: rails
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
|
-
segments:
|
31
|
-
- 3
|
32
|
-
- 1
|
33
|
-
- 0
|
34
|
-
version: 3.1.0
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
35
20
|
type: :runtime
|
36
|
-
|
37
|
-
|
38
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
description: "\n This gem makes updating specified number fields by ActiveRecord
|
28
|
+
in unusual way.\n\n Instead of generating sql script to update value in usual
|
29
|
+
way like this:\n\n UPDATE users\n SET money = 10\n WHERE id = 1;\n\n
|
30
|
+
\ It replaces it with\n\n UPDATE users\n SET money = money + delta\n
|
31
|
+
\ WHERE id = 1;\n\n where delta is difference between old value and new value
|
32
|
+
of that field.\n\n This solves problem with simultaneous updating of the same
|
33
|
+
field by different threads\n (problem known as race condition).\n\n Source
|
34
|
+
code: https://github.com/izbor/delta_attributes\n "
|
35
|
+
email:
|
39
36
|
- oleh.novosad@gmail.com
|
40
37
|
executables: []
|
41
|
-
|
42
38
|
extensions: []
|
43
|
-
|
44
39
|
extra_rdoc_files: []
|
45
|
-
|
46
|
-
files:
|
40
|
+
files:
|
47
41
|
- .gitignore
|
48
42
|
- Gemfile
|
49
43
|
- LICENSE.txt
|
@@ -51,48 +45,41 @@ files:
|
|
51
45
|
- Rakefile
|
52
46
|
- delta_attributes.gemspec
|
53
47
|
- lib/delta_attributes.rb
|
54
|
-
- lib/
|
55
|
-
- lib/
|
56
|
-
- lib/
|
57
|
-
- lib/
|
58
|
-
- lib/
|
59
|
-
- lib/
|
60
|
-
- lib/
|
61
|
-
- lib/
|
62
|
-
- lib/
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
48
|
+
- lib/delta_attributes3_2/crud.rb
|
49
|
+
- lib/delta_attributes3_2/main.rb
|
50
|
+
- lib/delta_attributes3_2/mysql.rb
|
51
|
+
- lib/delta_attributes3_2/railtie.rb
|
52
|
+
- lib/delta_attributes3_2/to_sql.rb
|
53
|
+
- lib/delta_attributes3_2/update_manager.rb
|
54
|
+
- lib/delta_attributes3_2/update_statement.rb
|
55
|
+
- lib/delta_attributes3_2/visitor.rb
|
56
|
+
- lib/delta_attributes4/bind_visitor.rb
|
57
|
+
- lib/delta_attributes4/main.rb
|
58
|
+
- lib/delta_attributes4/persistence.rb
|
59
|
+
- lib/delta_attributes4/railtie.rb
|
60
|
+
- lib/shared/version.rb
|
61
|
+
homepage: https://github.com/izbor/delta_attributes
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
67
65
|
post_install_message:
|
68
66
|
rdoc_options: []
|
69
|
-
|
70
|
-
require_paths:
|
67
|
+
require_paths:
|
71
68
|
- lib
|
72
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
none: false
|
83
|
-
requirements:
|
84
|
-
- - ">="
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
hash: 3
|
87
|
-
segments:
|
88
|
-
- 0
|
89
|
-
version: "0"
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
90
79
|
requirements: []
|
91
|
-
|
92
80
|
rubyforge_project:
|
93
|
-
rubygems_version:
|
81
|
+
rubygems_version: 2.0.3
|
94
82
|
signing_key:
|
95
|
-
specification_version:
|
83
|
+
specification_version: 4
|
96
84
|
summary: delta attributes
|
97
85
|
test_files: []
|
98
|
-
|