delta_attributes 0.0.1 → 0.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 CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .idea
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This gem makes updating specified number fields by ActiveRecord in unusual way.
4
4
 
5
- Instead to generate sql script to update value in usual way like this:
5
+ Instead of generating sql script to update value in usual way like this:
6
6
 
7
7
  UPDATE users
8
8
  SET money = 10
@@ -38,11 +38,23 @@ Or install it yourself as:
38
38
  To mark numeric field to be updated by "field = field + d" way you just need to add field
39
39
  name to delta_attributes like this:
40
40
 
41
- class User < ActiveRecord::Base
41
+ class User < ActiveRecord::Base
42
42
 
43
- delta_attributes :money
43
+ delta_attributes :money
44
44
 
45
- end
45
+ end
46
+
47
+ Now you can:
48
+
49
+ u = User.first # money = 3, id = 1
50
+ u.money = 5
51
+ u.save
52
+
53
+ will generate
54
+
55
+ UPDATE users SET money = money + 2 where id = 1
56
+
57
+ Tested with rails 3.2.8.
46
58
 
47
59
  ## Contributing
48
60
 
@@ -6,12 +6,12 @@ require 'delta_attributes/version'
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "delta_attributes"
8
8
  gem.version = DeltaAttributes::VERSION
9
- gem.authors = ["izbor"]
9
+ gem.authors = ["Oleh Novosad, Yuriy Lavryk, Arya"]
10
10
  gem.email = ["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
 
14
- Instead to generate sql script to update value in usual way like this:
14
+ Instead of generating sql script to update value in usual way like this:
15
15
 
16
16
  UPDATE users
17
17
  SET money = 10
@@ -25,7 +25,9 @@ Gem::Specification.new do |gem|
25
25
 
26
26
  where d is difference between old value and new value of that field.
27
27
 
28
- This solves problem with simultaneous updating of same field.
28
+ This solves problem with simultaneous updating of the same field by different threads.
29
+
30
+ Source code: https://github.com/izbor/delta_attributes
29
31
  }
30
32
  gem.summary = %q{ delta attributes }
31
33
  gem.homepage = ""
@@ -7,7 +7,7 @@ module Arel
7
7
  def visit_Arel_Nodes_UpdateStatement o
8
8
  [
9
9
  "UPDATE #{visit o.relation}",
10
- ("SET #{o.values_changed.map { |value| visit value }.join ', '}" unless o.values.empty?),
10
+ ("SET #{o.values_changed.map { |value| visit value }.join ', '}" unless o.values_changed.empty?),
11
11
  ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?),
12
12
  ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
13
13
  (visit(o.limit) if o.limit),
@@ -16,10 +16,32 @@ module Arel
16
16
  "#{visit o.arel_node.left} = #{delta_string}"
17
17
  end
18
18
 
19
- #alias_method_chain :visit_Arel_Nodes_Assignment, :deltas
20
-
21
19
  alias_method :visit_Arel_Nodes_Assignment_without_deltas, :visit_Arel_Nodes_Assignment
22
20
  alias_method :visit_Arel_Nodes_Assignment, :visit_Arel_Nodes_Assignment_with_deltas
21
+
22
+ def visit_Arel_Nodes_UpdateStatement o
23
+ if o.orders.empty? && o.limit.nil?
24
+ wheres = o.wheres
25
+ else
26
+ key = o.key
27
+ unless key
28
+ warn(<<-eowarn) if $VERBOSE
29
+ (#{caller.first}) Using UpdateManager without setting UpdateManager#key is
30
+ deprecated and support will be removed in ARel 4.0.0. Please set the primary
31
+ key on UpdateManager using UpdateManager#key=
32
+ eowarn
33
+ key = o.relation.primary_key
34
+ end
35
+
36
+ wheres = [Nodes::In.new(key, [build_subselect(key, o)])]
37
+ end
38
+
39
+ [
40
+ "UPDATE #{visit o.relation}",
41
+ ("SET #{o.values_changed.map { |value| visit value }.join ', '}" unless o.values_changed.empty?),
42
+ ("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?),
43
+ ].compact.join ' '
44
+ end
23
45
  end
24
46
  end
25
47
  end
@@ -1,3 +1,3 @@
1
1
  module DeltaAttributes
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,44 +1,49 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: delta_attributes
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
6
11
  platform: ruby
7
- authors:
8
- - izbor
12
+ authors:
13
+ - Oleh Novosad, Yuriy Lavryk, Arya
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-11-18 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-11-20 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
15
22
  name: rails
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 3.2.8
22
- type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
- requirements:
26
+ requirements:
27
27
  - - ~>
28
- - !ruby/object:Gem::Version
28
+ - !ruby/object:Gem::Version
29
+ hash: 31
30
+ segments:
31
+ - 3
32
+ - 2
33
+ - 8
29
34
  version: 3.2.8
30
- description: ! "\n This gem makes updating specified number fields by ActiveRecord
31
- in unusual way.\n\n Instead to generate sql script to update value in usual way
32
- like this:\n\n UPDATE users\n SET money = 10\n WHERE id = 1;\n\n
33
- \ It replaces it with\n\n UPDATE users\n SET money = money + d\n WHERE
34
- id = 1;\n\n where d is difference between old value and new value of that field.\n\n
35
- \ This solves problem with simultaneous updating of same field.\n "
36
- email:
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: "\n This gem makes updating specified number fields by ActiveRecord in unusual way.\n\n Instead of generating sql script to update value in usual way like this:\n\n UPDATE users\n SET money = 10\n WHERE id = 1;\n\n It replaces it with\n\n UPDATE users\n SET money = money + d\n WHERE id = 1;\n\n where d is difference between old value and new value of that field.\n\n This solves problem with simultaneous updating of the same field by different threads.\n\n Source code: https://github.com/izbor/delta_attributes\n "
38
+ email:
37
39
  - oleh.novosad@gmail.com
38
40
  executables: []
41
+
39
42
  extensions: []
43
+
40
44
  extra_rdoc_files: []
41
- files:
45
+
46
+ files:
42
47
  - .gitignore
43
48
  - Gemfile
44
49
  - LICENSE.txt
@@ -55,28 +60,39 @@ files:
55
60
  - lib/delta_attributes/update_statement.rb
56
61
  - lib/delta_attributes/version.rb
57
62
  - lib/delta_attributes/visitor.rb
58
- homepage: ''
63
+ has_rdoc: true
64
+ homepage: ""
59
65
  licenses: []
66
+
60
67
  post_install_message:
61
68
  rdoc_options: []
62
- require_paths:
69
+
70
+ require_paths:
63
71
  - lib
64
- required_ruby_version: !ruby/object:Gem::Requirement
72
+ required_ruby_version: !ruby/object:Gem::Requirement
65
73
  none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
82
  none: false
72
- requirements:
73
- - - ! '>='
74
- - !ruby/object:Gem::Version
75
- version: '0'
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
76
90
  requirements: []
91
+
77
92
  rubyforge_project:
78
- rubygems_version: 1.8.24
93
+ rubygems_version: 1.5.3
79
94
  signing_key:
80
95
  specification_version: 3
81
96
  summary: delta attributes
82
97
  test_files: []
98
+