changey 0.0.0
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.
- checksums.yaml +7 -0
- data/lib/changey.rb +0 -0
- data/lib/changey/block_dsl.rb +23 -0
- data/lib/changey/dsl.rb +75 -0
- data/lib/changey/railtie.rb +12 -0
- data/lib/changey/track.rb +54 -0
- data/lib/changey/version.rb +3 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8760b67cbf8d8eec056fd80ead4b86c2133f31b4
|
4
|
+
data.tar.gz: 1d2fc324b094b89e5d9863056a9434347f864922
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 929f25e36a6ac959fc940a4f7d25d1141086aafbfd016e77939f1254e8d84cba9ec3f3e83241ceef860a9bd2c7253e1a6cbac9d44315f2ce695a347e6022a89b
|
7
|
+
data.tar.gz: 16e86288fd1a978e8675f22aca134b14e47d67743909f14bac1a4bba9772a4e5d584a8b3c831a75c4a9c0faf5f6dc5b0eaa7da73ba64d5340b257d3710bcfb33
|
data/lib/changey.rb
ADDED
File without changes
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Changey
|
2
|
+
class BlockDSL
|
3
|
+
def initialize(track)
|
4
|
+
@track = track
|
5
|
+
end
|
6
|
+
|
7
|
+
def validate(name = nil, &block)
|
8
|
+
@track.validates << (block || name)
|
9
|
+
end
|
10
|
+
|
11
|
+
def before_save(name = nil, &block)
|
12
|
+
@track.before_saves << (block || name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def after_save(name = nil, &block)
|
16
|
+
@track.after_saves << (block || name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def after_commit(name = nil, &block)
|
20
|
+
@track.after_commits << (block || name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/changey/dsl.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'changey/track'
|
2
|
+
require 'changey/block_dsl'
|
3
|
+
|
4
|
+
module Changey
|
5
|
+
module DSL
|
6
|
+
|
7
|
+
class InvalidDSL < StandardError; end
|
8
|
+
class MissingChangeValue < InvalidDSL; end
|
9
|
+
class MissingBlock < InvalidDSL; end
|
10
|
+
|
11
|
+
def self.load(load_into)
|
12
|
+
load_into.extend ClassMethods
|
13
|
+
load_into.send :include, InstanceMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def changey_tracks
|
18
|
+
@changey_tracks ||= []
|
19
|
+
end
|
20
|
+
|
21
|
+
def when_attribute(attribute_name, options = {}, &block)
|
22
|
+
unless options.has_key?(:changes_from) || options.has_key?(:changes_to)
|
23
|
+
raise MissingChangeValue, "Attribute #{attribute_name} must specify a 'changes_to' or a 'changes_from' value"
|
24
|
+
end
|
25
|
+
|
26
|
+
unless block_given?
|
27
|
+
raise MissingBlock, "Attribute #{attribute_name} must specify a block"
|
28
|
+
end
|
29
|
+
|
30
|
+
track = Track.new(attribute_name)
|
31
|
+
track.direction = options.has_key?(:changes_from) ? :from : :to
|
32
|
+
track.expected_value = options.has_key?(:changes_from) ? options[:changes_from] : options[:changes_to]
|
33
|
+
|
34
|
+
if options.has_key?(:to) || options.has_key?(:from)
|
35
|
+
track.expected_other_value = options.has_key?(:to) ? options[:to] : options[:from]
|
36
|
+
end
|
37
|
+
|
38
|
+
BlockDSL.new(track).instance_eval(&block)
|
39
|
+
self.changey_tracks << track
|
40
|
+
track
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module InstanceMethods
|
45
|
+
|
46
|
+
def self.included(base)
|
47
|
+
base.before_validation { set_changey_tracks }
|
48
|
+
base.validate { run_changey_callbacks(:validate) }
|
49
|
+
base.before_save { run_changey_callbacks(:before_save) }
|
50
|
+
base.after_save { run_changey_callbacks(:after_save) }
|
51
|
+
base.after_commit { run_changey_callbacks(:after_commit) }
|
52
|
+
end
|
53
|
+
|
54
|
+
private def set_changey_tracks
|
55
|
+
@changey_tracks_to_run = self.class.changey_tracks.select { |t| t.run?(self) }.each_with_object({}) do |track, hash|
|
56
|
+
hash[track] = [send("#{track.attribute}_was"), send(track.attribute)]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private def run_changey_callbacks(event)
|
61
|
+
@changey_tracks_to_run.each do |track, (was, now)|
|
62
|
+
callbacks = track.send(event.to_s.pluralize)
|
63
|
+
callbacks.each do |callback|
|
64
|
+
if callback.is_a?(Proc)
|
65
|
+
instance_exec(was, now, &callback)
|
66
|
+
elsif callback.is_a?(Symbol)
|
67
|
+
send(callback)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Changey
|
2
|
+
class Track
|
3
|
+
|
4
|
+
class Nothing
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :attribute
|
8
|
+
attr_accessor :direction # :from or :to
|
9
|
+
attr_accessor :expected_value # The value expected
|
10
|
+
attr_accessor :expected_other_value # The value expected in the opposite state
|
11
|
+
|
12
|
+
attr_accessor :validates
|
13
|
+
attr_accessor :before_saves
|
14
|
+
attr_accessor :after_saves
|
15
|
+
attr_accessor :after_commits
|
16
|
+
|
17
|
+
def initialize(attribute)
|
18
|
+
@attribute = attribute
|
19
|
+
@expected_other_value = Nothing
|
20
|
+
@validates = []
|
21
|
+
@before_saves = []
|
22
|
+
@after_saves = []
|
23
|
+
@after_commits = []
|
24
|
+
end
|
25
|
+
|
26
|
+
def run?(record)
|
27
|
+
return false unless record.send("#{attribute}_changed?")
|
28
|
+
|
29
|
+
previous_value = record.send("#{attribute}_was")
|
30
|
+
current_value = record.send(attribute)
|
31
|
+
|
32
|
+
if expected_other_value != Nothing
|
33
|
+
if direction == :from && previous_value == expected_value && current_value == expected_other_value
|
34
|
+
return true
|
35
|
+
end
|
36
|
+
|
37
|
+
if direction == :to && current_value == expected_value && previous_value == expected_other_value
|
38
|
+
return true
|
39
|
+
end
|
40
|
+
else
|
41
|
+
if direction == :from && previous_value == expected_value
|
42
|
+
return true
|
43
|
+
end
|
44
|
+
|
45
|
+
if direction == :to && current_value == expected_value
|
46
|
+
return true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
return false
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: changey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Cooke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
description: An Active Record extension to provide state callbacks
|
34
|
+
email:
|
35
|
+
- me@adamcooke.io
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- lib/changey.rb
|
41
|
+
- lib/changey/block_dsl.rb
|
42
|
+
- lib/changey/dsl.rb
|
43
|
+
- lib/changey/railtie.rb
|
44
|
+
- lib/changey/track.rb
|
45
|
+
- lib/changey/version.rb
|
46
|
+
homepage: https://github.com/adamcooke/changey
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata: {}
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 2.4.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: An Active Record extension to provide state callbacks
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|