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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1c069619a0b94fd4ff58b8a911a21a801e733ae8
4
- data.tar.gz: 1be948578e766f84a8281c4941a8e03f2f36c95c
2
+ SHA256:
3
+ metadata.gz: d8c78386d3b7a2e1892b475f22d0f52fefead80d3da22c3b8d5e91234d86eca0
4
+ data.tar.gz: 4c79d6dcd35446eb584eff3cc1a5a38a1be9aeff8bf9524742830d7a9463fb8c
5
5
  SHA512:
6
- metadata.gz: 2694c30516924e1b32a4afcb724b245f5d49dfe33afb66b532e989b098d56c97f76e74059522779c0ebd0f494bd0b0f089177ec9d571ae1ae336ad128971abd8
7
- data.tar.gz: 88cf3f20ed7fcbc4e7e8d6b5623e0cd0f5d86eee01ab2d4283e11700d322755855b88eae6cbc897357e282a5aaee6954c999a80c342fe3bcc16442296f4d33df
6
+ metadata.gz: 70bf8086506bba355f2ab7ff16fa9115c61b2bb04558621ba34d57e83dcb5fc29807dcfc3833a493289a74b051797799594ae8a701d5cce546a6bc22a616e662
7
+ data.tar.gz: 878d1ae30630fc98adb4dcf4b8451ee25e6607ad21238b0441b8f87a45f90b35372316002db4b11b738c817013dae92192ee9603507cd6e1d712ac379bcd775d
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'changey/railtie' if defined?(Rails)
@@ -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
@@ -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 self.changey_tracks.empty?
28
+ if changey_tracks.empty?
26
29
  include InstanceMethods
27
30
  end
28
31
 
29
- unless options.has_key?(:changes_from) || options.has_key?(:changes_to)
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.has_key?(:changes_from) ? :from : :to
39
- track.expected_value = options.has_key?(:changes_from) ? options[:changes_from] : options[:changes_to]
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.has_key?(:to) || options.has_key?(:from)
42
- track.expected_other_value = options.has_key?(:to) ? options[:to] : options[:from]
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
- self.changey_tracks << track
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 def set_changey_tracks
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
- private def clear_changey_tracks
73
+ def clear_changey_tracks
68
74
  @changey_tracks_to_run = nil
69
75
  end
70
76
 
71
- private def run_changey_callbacks(event)
72
- if @changey_tracks_to_run
73
- @changey_tracks_to_run.each do |track, (was, now)|
74
- callbacks = track.send(event.to_s.pluralize)
75
- callbacks.each do |callback|
76
- if callback.is_a?(Proc)
77
- instance_exec(was, now, &callback)
78
- elsif callback.is_a?(Symbol)
79
- send(callback)
80
- end
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Changey
2
4
  class Railtie < Rails::Railtie
3
5
 
@@ -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 != Nothing
35
- if direction == :from && matches?(previous_value, expected_value) && matches?(current_value, expected_other_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) && matches?(previous_value, expected_other_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
- return false
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
- !!(expectation =~ value)
66
+ !(expectation =~ value).nil?
63
67
  when Proc
64
68
  expectation.call(value) == true
65
69
  when :anything
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Changey
2
- VERSION = '1.0.0'
4
+
5
+ VERSION = '1.1.0'
6
+
3
7
  end
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.0.0
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: 2016-08-08 00:00:00.000000000 Z
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: '6'
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: '6'
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: '0'
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
- rubyforge_project:
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: