changey 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/changey.rb +2 -0
- data/lib/changey/block_dsl.rb +4 -0
- data/lib/changey/dsl.rb +27 -19
- data/lib/changey/railtie.rb +2 -0
- data/lib/changey/track.rb +11 -7
- data/lib/changey/version.rb +5 -1
- metadata +20 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d8c78386d3b7a2e1892b475f22d0f52fefead80d3da22c3b8d5e91234d86eca0
|
4
|
+
data.tar.gz: 4c79d6dcd35446eb584eff3cc1a5a38a1be9aeff8bf9524742830d7a9463fb8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70bf8086506bba355f2ab7ff16fa9115c61b2bb04558621ba34d57e83dcb5fc29807dcfc3833a493289a74b051797799594ae8a701d5cce546a6bc22a616e662
|
7
|
+
data.tar.gz: 878d1ae30630fc98adb4dcf4b8451ee25e6607ad21238b0441b8f87a45f90b35372316002db4b11b738c817013dae92192ee9603507cd6e1d712ac379bcd775d
|
data/lib/changey.rb
CHANGED
data/lib/changey/block_dsl.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Changey
|
2
4
|
class BlockDSL
|
5
|
+
|
3
6
|
def initialize(track)
|
4
7
|
@track = track
|
5
8
|
end
|
@@ -19,5 +22,6 @@ module Changey
|
|
19
22
|
def after_commit(name = nil, &block)
|
20
23
|
@track.after_commits << (block || name)
|
21
24
|
end
|
25
|
+
|
22
26
|
end
|
23
27
|
end
|
data/lib/changey/dsl.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'changey/track'
|
2
4
|
require 'changey/block_dsl'
|
3
5
|
|
@@ -13,6 +15,7 @@ module Changey
|
|
13
15
|
end
|
14
16
|
|
15
17
|
module ClassMethods
|
18
|
+
|
16
19
|
def changey_tracks
|
17
20
|
@changey_tracks ||= []
|
18
21
|
end
|
@@ -22,11 +25,11 @@ module Changey
|
|
22
25
|
end
|
23
26
|
|
24
27
|
def when_attribute(attribute_name, options = {}, &block)
|
25
|
-
if
|
28
|
+
if changey_tracks.empty?
|
26
29
|
include InstanceMethods
|
27
30
|
end
|
28
31
|
|
29
|
-
unless options.
|
32
|
+
unless options.key?(:changes_from) || options.key?(:changes_to)
|
30
33
|
raise MissingChangeValue, "Attribute #{attribute_name} must specify a 'changes_to' or a 'changes_from' value"
|
31
34
|
end
|
32
35
|
|
@@ -35,17 +38,18 @@ module Changey
|
|
35
38
|
end
|
36
39
|
|
37
40
|
track = Track.new(attribute_name)
|
38
|
-
track.direction = options.
|
39
|
-
track.expected_value = options.
|
41
|
+
track.direction = options.key?(:changes_from) ? :from : :to
|
42
|
+
track.expected_value = options.key?(:changes_from) ? options[:changes_from] : options[:changes_to]
|
40
43
|
|
41
|
-
if options.
|
42
|
-
track.expected_other_value = options.
|
44
|
+
if options.key?(:to) || options.key?(:from)
|
45
|
+
track.expected_other_value = options.key?(:to) ? options[:to] : options[:from]
|
43
46
|
end
|
44
47
|
|
45
48
|
BlockDSL.new(track).instance_eval(&block)
|
46
|
-
|
49
|
+
changey_tracks << track
|
47
50
|
track
|
48
51
|
end
|
52
|
+
|
49
53
|
end
|
50
54
|
|
51
55
|
module InstanceMethods
|
@@ -58,30 +62,34 @@ module Changey
|
|
58
62
|
base.after_commit { run_changey_callbacks(:after_commit) }
|
59
63
|
end
|
60
64
|
|
61
|
-
private
|
65
|
+
private
|
66
|
+
|
67
|
+
def set_changey_tracks
|
62
68
|
@changey_tracks_to_run = self.class.changey_tracks.select { |t| t.run?(self) }.each_with_object({}) do |track, hash|
|
63
69
|
hash[track] = [send("#{track.attribute}_was"), send(track.attribute)]
|
64
70
|
end
|
65
71
|
end
|
66
72
|
|
67
|
-
|
73
|
+
def clear_changey_tracks
|
68
74
|
@changey_tracks_to_run = nil
|
69
75
|
end
|
70
76
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
77
|
+
def run_changey_callbacks(event)
|
78
|
+
return unless @changey_tracks_to_run
|
79
|
+
|
80
|
+
@changey_tracks_to_run.each do |track, (was, now)|
|
81
|
+
callbacks = track.send(event.to_s.pluralize)
|
82
|
+
callbacks.each do |callback|
|
83
|
+
case callback
|
84
|
+
when Proc
|
85
|
+
instance_exec(was, now, &callback)
|
86
|
+
when Symbol
|
87
|
+
send(callback)
|
81
88
|
end
|
82
89
|
end
|
83
90
|
end
|
84
91
|
end
|
92
|
+
|
85
93
|
end
|
86
94
|
|
87
95
|
end
|
data/lib/changey/railtie.rb
CHANGED
data/lib/changey/track.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Changey
|
2
4
|
class Track
|
3
5
|
|
@@ -31,25 +33,27 @@ module Changey
|
|
31
33
|
previous_value = record.send("#{attribute}_was")
|
32
34
|
current_value = record.send(attribute)
|
33
35
|
|
34
|
-
if expected_other_value
|
35
|
-
if direction == :from && matches?(previous_value, expected_value)
|
36
|
+
if expected_other_value == Nothing
|
37
|
+
if direction == :from && matches?(previous_value, expected_value)
|
36
38
|
return true
|
37
39
|
end
|
38
40
|
|
39
|
-
if direction == :to && matches?(current_value, expected_value)
|
41
|
+
if direction == :to && matches?(current_value, expected_value)
|
40
42
|
return true
|
41
43
|
end
|
42
44
|
else
|
43
|
-
if direction == :from && matches?(previous_value, expected_value)
|
45
|
+
if direction == :from && matches?(previous_value, expected_value) &&
|
46
|
+
matches?(current_value, expected_other_value)
|
44
47
|
return true
|
45
48
|
end
|
46
49
|
|
47
|
-
if direction == :to && matches?(current_value, expected_value)
|
50
|
+
if direction == :to && matches?(current_value, expected_value) &&
|
51
|
+
matches?(previous_value, expected_other_value)
|
48
52
|
return true
|
49
53
|
end
|
50
54
|
end
|
51
55
|
|
52
|
-
|
56
|
+
false
|
53
57
|
end
|
54
58
|
|
55
59
|
private
|
@@ -59,7 +63,7 @@ module Changey
|
|
59
63
|
when Array
|
60
64
|
expectation.include?(value)
|
61
65
|
when Regexp
|
62
|
-
|
66
|
+
!(expectation =~ value).nil?
|
63
67
|
when Proc
|
64
68
|
expectation.call(value) == true
|
65
69
|
when :anything
|
data/lib/changey/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: changey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cooke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,21 @@ dependencies:
|
|
29
29
|
version: '4.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: appraisal
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
description: An Active Record extension to provide state callbacks
|
34
48
|
email:
|
35
49
|
- me@adamcooke.io
|
@@ -55,17 +69,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
69
|
requirements:
|
56
70
|
- - ">="
|
57
71
|
- !ruby/object:Gem::Version
|
58
|
-
version: '
|
72
|
+
version: '2.4'
|
59
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
74
|
requirements:
|
61
75
|
- - ">="
|
62
76
|
- !ruby/object:Gem::Version
|
63
77
|
version: '0'
|
64
78
|
requirements: []
|
65
|
-
|
66
|
-
rubygems_version: 2.4.5
|
79
|
+
rubygems_version: 3.0.3
|
67
80
|
signing_key:
|
68
81
|
specification_version: 4
|
69
82
|
summary: An Active Record extension to provide state callbacks
|
70
83
|
test_files: []
|
71
|
-
has_rdoc:
|