on_change 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/lib/on_change/version.rb +3 -0
- data/lib/on_change.rb +40 -0
- data/on_change.gemspec +28 -0
- data/spec/active_record_spec.rb +82 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YjI5N2IxYTYxM2NiNDFmMDIwMjBmYmZkMmE2YjEyZGMyMzRiYTI4NA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MTJkNjA5MTMzNTZjNDk5NzZlNzZiZGExYjVlMDgwOGNiMTUyY2M1Yw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MzZmZGVjMTNjNWRlMjE2MzViNmM5MTdlOGE5NGI5NDYzN2ViYTE0ODkyNDQ5
|
10
|
+
NzgwZDQzNTRjMGM4NGE1NDFhYTFiYzIzZDU1Yzk2MDYyOTA2Njc1NzUzYTE1
|
11
|
+
YTkwNDA1NWE0ZjliZWMzMTg5NjkwNTdmZDE4M2RmMzNkNjQwYjQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTg2ZjIzZjQ0YmJkZGMxY2Y4NmY2ZWE1NzBiNjQ1ZGQ5ZGZkNzg4NDdmNTQx
|
14
|
+
ZjAxYzc4NDEyNjQ0Mjk3Zjg3OWVhYTcyZTVhODcyZDdhNmI3NTNjZDdhNGQ5
|
15
|
+
YzA2NWI3YzgyMTE0YjEyYjU4ZTk3NjMzODI5Y2ViYjUwOWQwY2M=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ronna
|
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,41 @@
|
|
1
|
+
# OnChange
|
2
|
+
|
3
|
+
Subscribe to attribute changes events for your active record
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'on_change'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install on_change
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
class Book < ActiveRecord::Base
|
24
|
+
on_change :title, :lookup_in_google_books
|
25
|
+
on_change :title, :price, do |model, attr_name, old_value,new_value|
|
26
|
+
model.do_something
|
27
|
+
end
|
28
|
+
def lookup_in_google_books(attr_name, old_value, new_value)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/on_change.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "on_change/version"
|
2
|
+
|
3
|
+
module OnChange
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
alias_method_chain :write_attribute, :callbacks
|
7
|
+
end
|
8
|
+
def run_attribute_change_callbacks(attr_name, *args)
|
9
|
+
on_change_callbacks = self.class.on_change_callbacks
|
10
|
+
on_change_callbacks.has_key?(attr_name.to_s) && on_change_callbacks[attr_name.to_s].each do |callback|
|
11
|
+
callback.is_a?(Proc) ? callback.call(self,attr_name,*args) : send(callback, attr_name, *args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def write_attribute_with_callbacks(attr_name, value)
|
16
|
+
run_attribute_change_callbacks(attr_name, read_attribute(attr_name), value)
|
17
|
+
write_attribute_without_callbacks(attr_name, value)
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
def on_change_callbacks
|
22
|
+
@on_change_callbacks ||= {}
|
23
|
+
end
|
24
|
+
def subscribe_attribute_changes(*args, &block)
|
25
|
+
callback = block_given? ? block : args.pop
|
26
|
+
args.each do |attr|
|
27
|
+
on_change_callbacks[attr.to_s] ||= []
|
28
|
+
on_change_callbacks[attr.to_s] << callback
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
def on_change(*args, &block)
|
33
|
+
subscribe_attribute_changes(*args, &block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class ActiveRecord::Base
|
39
|
+
include OnChange
|
40
|
+
end
|
data/on_change.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'on_change/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "on_change"
|
8
|
+
spec.version = OnChange::VERSION
|
9
|
+
spec.authors = ["Ronna"]
|
10
|
+
spec.email = [""]
|
11
|
+
spec.summary = %q{Register to on change events on your active record attributes}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
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_dependency "rails", "> 3.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'rails'
|
3
|
+
load 'lib/on_change.rb'
|
4
|
+
require 'pry'
|
5
|
+
ActiveRecord::Base.establish_connection(
|
6
|
+
adapter: 'sqlite3',
|
7
|
+
encoding: 'utf8',
|
8
|
+
database: ":memory:")
|
9
|
+
|
10
|
+
ActiveRecord::Schema.define do
|
11
|
+
create_table :books do |t|
|
12
|
+
t.string :title
|
13
|
+
t.integer :num_reads
|
14
|
+
t.string :author
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActiveRecord::Schema.define do
|
19
|
+
create_table :users do |t|
|
20
|
+
t.string :first_name
|
21
|
+
t.string :last_name
|
22
|
+
t.string :email
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Book < ActiveRecord::Base
|
27
|
+
on_change :title, :num_reads do |model, attr_name, old_value, new_value|
|
28
|
+
model.res1(attr_name, old_value, new_value)
|
29
|
+
end
|
30
|
+
on_change :num_reads, :author, :res2
|
31
|
+
def res1(x,y,z);end
|
32
|
+
def res2(x,y,z); end
|
33
|
+
end
|
34
|
+
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
on_change :first_name, :last_name, :hello_user
|
37
|
+
def hello_user(x,y,z); end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe OnChange do
|
41
|
+
|
42
|
+
it "should invoke on change callback for title" do
|
43
|
+
b = Book.new
|
44
|
+
expect(b).to receive(:res1).with("title", nil, "hello")
|
45
|
+
b.title = "hello"
|
46
|
+
end
|
47
|
+
it "should invoke on change callback for title a second time" do
|
48
|
+
b = Book.new
|
49
|
+
b.title = "hello"
|
50
|
+
expect(b).to receive(:res1).with("title", "hello", "world")
|
51
|
+
b.title = "world"
|
52
|
+
end
|
53
|
+
it "should invoke on change callback for title" do
|
54
|
+
b = Book.new
|
55
|
+
expect(b).to receive(:res1).with("title", nil, "hello")
|
56
|
+
b.title = "hello"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should invoke on 2 callbacks for num_reads" do
|
60
|
+
b = Book.new
|
61
|
+
expect(b).to receive(:res1).with("num_reads", nil, 1)
|
62
|
+
expect(b).to receive(:res2).with("num_reads", nil, 1)
|
63
|
+
b.num_reads = 1
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should invoke on 2 callbacks for num_reads" do
|
67
|
+
b = Book.new
|
68
|
+
expect(b).to receive(:res1).with("num_reads", nil, 1)
|
69
|
+
expect(b).to receive(:res2).with("num_reads", nil, 1)
|
70
|
+
b.num_reads = 1
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should not invoke callbacks for user" do
|
74
|
+
u = User.new
|
75
|
+
expect(u).to receive(:hello_user).with("first_name", nil, "john")
|
76
|
+
expect(u).to receive(:hello_user).with("last_name", nil, "doe")
|
77
|
+
u.first_name = "john"
|
78
|
+
u.last_name = "doe"
|
79
|
+
u.email = "john@doe.com"
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: on_change
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ronna
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-07 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: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>'
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.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.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: ''
|
84
|
+
email:
|
85
|
+
- ''
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/on_change.rb
|
96
|
+
- lib/on_change/version.rb
|
97
|
+
- on_change.gemspec
|
98
|
+
- spec/active_record_spec.rb
|
99
|
+
homepage: ''
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.2.2
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Register to on change events on your active record attributes
|
123
|
+
test_files:
|
124
|
+
- spec/active_record_spec.rb
|