enum-transitions 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c14766eb62c8818cf3142933685a7dbadd825a42c00f34543aa14996ecacbba2
4
+ data.tar.gz: 052d0e5d8ac1d0d459aa70e3ca190eb18a1e435c4a811f3c114683121eaa0617
5
+ SHA512:
6
+ metadata.gz: 0de272af650ab10c23763fcdeb7b511a4049154e864d60fd352c9d71a98d00cd390dab7fb4a20a25d4c05fcdc5f1ebc2de7a468e5efd33814b3b029357f2a3d8
7
+ data.tar.gz: b966fe288ca193691e9dac6a9f64e5912a1f63692dc3c6e10d9a1eaed8ce420e3162c3e57e23e9ac4225f0457134321a67e2f2ff61d8b40ec15c74f86d7da8d2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in enum-transitions.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ enum-transitions (1.0.0)
5
+ activerecord (~> 5.0)
6
+ activesupport (~> 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.1.7)
12
+ activesupport (= 5.1.7)
13
+ activerecord (5.1.7)
14
+ activemodel (= 5.1.7)
15
+ activesupport (= 5.1.7)
16
+ arel (~> 8.0)
17
+ activesupport (5.1.7)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 0.7, < 2)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ arel (8.0.0)
23
+ concurrent-ruby (1.1.6)
24
+ diff-lcs (1.3)
25
+ i18n (1.6.0)
26
+ concurrent-ruby (~> 1.0)
27
+ minitest (5.14.0)
28
+ rake (12.3.3)
29
+ rspec (3.9.0)
30
+ rspec-core (~> 3.9.0)
31
+ rspec-expectations (~> 3.9.0)
32
+ rspec-mocks (~> 3.9.0)
33
+ rspec-core (3.9.1)
34
+ rspec-support (~> 3.9.1)
35
+ rspec-expectations (3.9.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-mocks (3.9.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-support (3.9.2)
42
+ thread_safe (0.3.6)
43
+ tzinfo (1.2.6)
44
+ thread_safe (~> 0.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 2.0)
51
+ enum-transitions!
52
+ rake (~> 12.3.3)
53
+ rspec (~> 3.0)
54
+
55
+ BUNDLED WITH
56
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Artem Nistratov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Enum::Transitions
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'enum-transitions', '~> 1.0.0
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install enum-transitions
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class Model < ApplicationRecord
23
+ include Enum::Transitions::DSL
24
+
25
+ enum state: [:pending, :active, :finihed, :failed]
26
+
27
+ transitions enum: :state do
28
+ allow pending => active
29
+
30
+ allow active => [finihed, failed] do
31
+ after do
32
+ # ...
33
+ end
34
+ end
35
+
36
+ after_leaving pending do
37
+ # ...
38
+ end
39
+ end
40
+ end
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/adone/enum-transitions.
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "enum/transitions"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'enum/transitions/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'enum-transitions'
7
+ spec.version = Enum::Transitions::VERSION
8
+ spec.authors = ['Artem Nistratov']
9
+ spec.email = ['artem.nistratov@gmail.com']
10
+
11
+ spec.summary = 'Simple state transitions for ActiveRecord enum columns'
12
+ spec.description = 'Simple state transitions for ActiveRecord enum columns'
13
+ spec.homepage = 'https://github.com/adone/enum-transitions'
14
+ spec.license = 'MIT'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/adone/enum-transitions'
18
+ # spec.metadata['changelog_uri'] = 'TODO: Put your gem's CHANGELOG.md URL here.'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 2.0'
28
+ spec.add_development_dependency 'rake', '~> 12.3.3'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+
31
+ spec.add_runtime_dependency 'activerecord', '~> 5.0'
32
+ spec.add_runtime_dependency 'activesupport', '~> 5.0'
33
+ end
@@ -0,0 +1,19 @@
1
+ require 'enum/transitions/version'
2
+ require 'active_support'
3
+
4
+ module Enum
5
+ module Transitions
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :DSL
9
+ autoload :Mixin
10
+ autoload :Naming
11
+ autoload :Machine
12
+ autoload :UpdateHandler
13
+ autoload :Configuration
14
+
15
+ module Errors
16
+ TransitionNotAllowed = Class.new(RuntimeError)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module Enum
2
+ module Transitions
3
+ class Configuration
4
+ attr_reader :enum, :states, :transitions, :callbacks
5
+
6
+ # @param enum [String]
7
+ # @param states [Array<String>]
8
+ def initialize(enum, states)
9
+ @enum = enum
10
+ @states = states
11
+ @transitions = {}
12
+ @callbacks = {}
13
+ end
14
+
15
+ def with_safe_nesting
16
+ @transitions.default_proc = ->(hash, key) { hash[key] = {} }
17
+ @callbacks.default_proc = ->(hash, key) { hash[key] = [] }
18
+ yield
19
+ ensure
20
+ @callbacks.default_proc = nil
21
+ @transitions.default_proc = nil
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Enum
2
+ module Transitions
3
+ module DSL
4
+ extend ActiveSupport::Concern
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :Proxy
8
+ autoload :Callbacks
9
+
10
+ class_methods do
11
+ def transitions(enum:, &block)
12
+ states = public_send(enum.to_s.pluralize).keys
13
+ config = Configuration.new(enum.to_s, states)
14
+ proxy = Proxy.embed(states).new(config)
15
+
16
+ config.with_safe_nesting do
17
+ Docile.dsl_eval(proxy, &block)
18
+ end
19
+
20
+ include Mixin.new(config)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module Enum
2
+ module Transitions
3
+ module DSL
4
+ class Callbacks
5
+ def initialize(config, transition)
6
+ @config = config
7
+ @transition = transition
8
+ end
9
+
10
+ def before(&block)
11
+ @config.callbacks[@transition] << [:before, block]
12
+ end
13
+
14
+ def after(&block)
15
+ @config.callbacks[@transition] << [:after, block]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,63 @@
1
+ module Enum
2
+ module Transitions
3
+ module DSL
4
+ class Proxy
5
+ include Naming
6
+
7
+ # @param states [Array<String>]
8
+ def self.embed(states)
9
+ Class.new(self) do
10
+ states.each do |state|
11
+ define_method(state) { state }
12
+ end
13
+ end
14
+ end
15
+
16
+ # @param config [Enum::Transitions::Configuration]
17
+ def initialize(config)
18
+ @config = config
19
+ end
20
+
21
+ # @param transitions[Hash{Array<Symbol>=>Array<Symbol>}]
22
+ def allow(transitions, &block)
23
+ transitions.each_pair do |sources, targets|
24
+ Array(sources).product(Array(targets)) do |source, target|
25
+ transition = transition_event(source, target)
26
+
27
+ @config.transitions[source][target] = transition
28
+ @config.callbacks[transition] ||= []
29
+ @config.callbacks[leaving_event(source)] ||= []
30
+ @config.callbacks[entering_event(target)] ||= []
31
+
32
+ Docile.dsl_eval(Callbacks.new(@config, transition), &block)
33
+ end
34
+ end
35
+ end
36
+
37
+ def before_leaving(*states, &block)
38
+ states.each do |state|
39
+ @config.callbacks[leaving_event(state)] << [:before, block]
40
+ end
41
+ end
42
+
43
+ def after_leaving(*states, &block)
44
+ states.each do |state|
45
+ @config.callbacks[leaving_event(state)] << [:after, block]
46
+ end
47
+ end
48
+
49
+ def before_entering(*states, &block)
50
+ states.each do |state|
51
+ @config.callbacks[entering_event(state)] << [:before, block]
52
+ end
53
+ end
54
+
55
+ def after_entering(*states, &block)
56
+ states.each do |state|
57
+ @config.callbacks[entering_event(state)] << [:after, block]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ module Enum
2
+ module Transitions
3
+ class Mixin < Module
4
+ include ActiveSupport::Concern
5
+
6
+ # @param config [Enum::Transitions::Configuration]
7
+ def initialize(config)
8
+ @_dependencies = []
9
+ @_enum = config.enum
10
+
11
+ included do
12
+ define_callbacks(*config.callbacks.keys)
13
+
14
+ config.callbacks.each_pair do |event, callbacks|
15
+ callbacks.each do |kind, block|
16
+ set_callback(event, kind, &block)
17
+ end
18
+ end
19
+
20
+ around_update UpdateHandler.new(config), if: :"#{config.enum}_changed?"
21
+ end
22
+ end
23
+
24
+ def inspect
25
+ "Enum::Transitions::Mixin[#{@_enum}]"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Enum
2
+ module Transitions
3
+ module Naming
4
+ def transition_event(source, target)
5
+ :"#{@config.enum}_from_#{source}_to_#{target}"
6
+ end
7
+
8
+ def leaving_event(state)
9
+ :"#{@config.enum}_leaving_#{state}"
10
+ end
11
+
12
+ def entering_event(state)
13
+ :"#{@config.enum}_entering_#{state}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ module Enum
2
+ module Transitions
3
+ class UpdateHandler
4
+ include Naming
5
+
6
+ # @param config [Enum::Transisions::Configuration]
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def around_update(model)
12
+ source, target = model.changes[@config.enum]
13
+ transition = @config.transitions.dig(source, target)
14
+
15
+ raise Errors::TransitionNotAllowed if transition.nil?
16
+
17
+ model.run_callbacks transition do
18
+ model.run_callbacks leaving_event(source) do
19
+ model.run_callbacks entering_event(target) do
20
+ yield
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Enum
2
+ module Transitions
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enum-transitions
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Artem Nistratov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Simple state transitions for ActiveRecord enum columns
84
+ email:
85
+ - artem.nistratov@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - enum-transitions.gemspec
100
+ - lib/enum/transitions.rb
101
+ - lib/enum/transitions/configuration.rb
102
+ - lib/enum/transitions/dsl.rb
103
+ - lib/enum/transitions/dsl/callbacks.rb
104
+ - lib/enum/transitions/dsl/proxy.rb
105
+ - lib/enum/transitions/mixin.rb
106
+ - lib/enum/transitions/naming.rb
107
+ - lib/enum/transitions/update_handler.rb
108
+ - lib/enum/transitions/version.rb
109
+ homepage: https://github.com/adone/enum-transitions
110
+ licenses:
111
+ - MIT
112
+ metadata:
113
+ homepage_uri: https://github.com/adone/enum-transitions
114
+ source_code_uri: https://github.com/adone/enum-transitions
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.0.6
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Simple state transitions for ActiveRecord enum columns
134
+ test_files: []