closed_loop 0.1.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: b34de60db17b5f35bc766ec5d500aeeb7aaf71741084a8f2da94054941f70820
4
+ data.tar.gz: 98a53ad41899e0a53b92c5c0ee584ec272c5c86e91812f2683dc94b3640d56d0
5
+ SHA512:
6
+ metadata.gz: 6853892b0d41c33093d1a1bbae4cd64e34d9e5743fb56912426fc3b24ff326ccdab3c8afccd4f624100f2d8e904ae12193ffdfae0527b741422e1c2d7edc13e5
7
+ data.tar.gz: 97ea90aef5c24d89151881359b6a6c8b584116d264cb6ed1b2f192a897d3128e7afeb8b72df66a0d3cb37b85522924b055efdbd0f834ae854f85099920346260
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
12
+
13
+ # RubyMine IDE
14
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-12
4
+
5
+ - Initial release
data/Example.md ADDED
@@ -0,0 +1,88 @@
1
+ # Prerequisites
2
+
3
+ 1. ActiveRecord model with the status column
4
+ 2. `closed_loop` gem installed
5
+
6
+ # Configuration
7
+
8
+ ```Ruby
9
+ # app/lib/order_machine.rb
10
+
11
+ require 'closed_loop'
12
+
13
+ class OrderMachine < ClosedLoop::Machine::Instance
14
+ class RoleResolver < ClosedLoop::Machine::RoleResolver
15
+ def call
16
+ if !user
17
+ :system
18
+ elsif user.role_admin?
19
+ :admin
20
+ elsif target.buyer == user
21
+ :buyer
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def configure_all
29
+ # Naming convention - "[PROCESS_NAME__]STATE[_SUB_STATE]"
30
+
31
+ # Checkout > View cart state
32
+ transition(role: :buyer, from: :cart__created, to: :cart__reviewed)
33
+ transition(role: :buyer, from: :cart__reviewed, to: :cart__confirmed)
34
+
35
+ # Checkout > Billing state
36
+ transition(role: :buyer, from: :cart__confirmed, to: :billing_information__submitted)
37
+ transition(role: :buyer, from: :billing_information__information_submitted, to: :payment__method_selected)
38
+
39
+ # Checkout > Payment state
40
+ transition(role: :buyer, from: :payment__method_selected, to: :payment__pending)
41
+ transition(role: :system, from: :payment__pending, to: :payment__failed)
42
+ transition(role: :system, from: :payment__pending, to: :payment__confirmed)
43
+
44
+ # Shipping
45
+ transition(role: :admin, from: :payment__confirmed, to: :parcel__pending)
46
+ transition(role: :admin, from: :parcel__pending, to: :parcel__prepared)
47
+ transition(role: :admin, from: :parcel__prepared, to: :parcel__shipping_pending)
48
+ transition(role: :system, from: :parcel__shipping_pending, to: :parcel__shipped)
49
+ transition(role: :buyer, from: :parcel__shipped, to: :parcel__received)
50
+ transition(role: :buyer, from: :parcel__shipped, to: :parcel__lost)
51
+
52
+ # Shipping failed
53
+ transition(role: :admin, from: :parcel__lost, to: :parcel__pending)
54
+
55
+ transition(role: :admin, from: :parcel__lost, to: :refunded) do |target, _user|
56
+ SendRefundNotification.call(target)
57
+ end
58
+
59
+ constraint(role: :admin, from: :parcel__lost, to: :parcel__pending) do |target, _user|
60
+ # Only small size purchases could be reshipped
61
+ target.total_price < 15.00
62
+ end
63
+
64
+ # Re-usable callbacks extracted from transitions
65
+ callback(from: :parcel__shipped, to: %i[parcel__lost parcel__received]) do |target, _user|
66
+ # Push slack notifications on shipping status change
67
+ PushOrderStatusSlackNotification.call(target)
68
+ end
69
+
70
+ callback(to: %i[payment__confirmed payment__failed]) do |target, _user|
71
+ SendPaymentStatusNotification.call(target)
72
+ end
73
+ end
74
+ end
75
+ ```
76
+
77
+ ```Ruby
78
+ # In controller / service / interactor / background worker
79
+
80
+ OrderMachine.insantce.transition!(order, current_user, to: :parcel__received)
81
+
82
+ # invoke extra methods in the the same transition & ActiveRecord transaction:
83
+
84
+ OrderMachine.insantce.transition!(order, current_user, to: :parcel__received) do
85
+ order.items.each { |item| '#...' }
86
+ end
87
+
88
+ ```
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in closed_loop.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ closed_loop (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (13.0.6)
11
+ rspec (3.11.0)
12
+ rspec-core (~> 3.11.0)
13
+ rspec-expectations (~> 3.11.0)
14
+ rspec-mocks (~> 3.11.0)
15
+ rspec-core (3.11.0)
16
+ rspec-support (~> 3.11.0)
17
+ rspec-expectations (3.11.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.11.0)
20
+ rspec-mocks (3.11.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.11.0)
23
+ rspec-support (3.11.0)
24
+
25
+ PLATFORMS
26
+ x86_64-darwin-20
27
+
28
+ DEPENDENCIES
29
+ bundler
30
+ closed_loop!
31
+ rake
32
+ rspec
33
+
34
+ BUNDLED WITH
35
+ 2.2.15
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Vilius Luneckas
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # ClosedLoop
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/closed_loop`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'closed_loop'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install closed_loop
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/closed_loop.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "closed_loop"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/closed_loop/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'closed_loop'
7
+ spec.version = ClosedLoop::VERSION
8
+ spec.authors = ['Vilius Luneckas']
9
+ spec.email = ['vilius.luneckas@gmail.com']
10
+
11
+ spec.summary = 'State machine DSL'
12
+ spec.homepage = 'https://github.com/ViliusLuneckas/closed_loop'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec)/}) }
23
+ end
24
+
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'bundler'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'rspec'
32
+ end
@@ -0,0 +1,15 @@
1
+ module ClosedLoop
2
+ module Machine
3
+ class Base
4
+ ATTRIBUTES = %i[machine to from role].freeze
5
+
6
+ attr_reader(*(ATTRIBUTES + [:proc]))
7
+
8
+ def initialize(config, proc = nil)
9
+ config.slice(*ATTRIBUTES).each { |k, v| instance_variable_set("@#{k}", v) }
10
+
11
+ @proc = proc
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module ClosedLoop
2
+ module Machine
3
+ class Callback < Base
4
+ def perform!(target, user, options)
5
+ proc&.call(target, user, options)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ module ClosedLoop
2
+ module Machine
3
+ class Configuration
4
+ attr_reader :transitions, :callbacks, :machine, :constraints
5
+
6
+ def initialize(machine)
7
+ @machine = machine
8
+ @transitions = []
9
+ @callbacks = []
10
+ @constraints = []
11
+ end
12
+
13
+ def transition(config, &block)
14
+ @transitions << Transition.new(config.merge(machine: machine), block)
15
+ end
16
+
17
+ def callback(config, &block)
18
+ @callbacks << Callback.new(config.merge(machine: machine), block)
19
+ end
20
+
21
+ def constraint(config, &block)
22
+ @constraints << Constraint.new(config.merge(machine: machine), block)
23
+ end
24
+
25
+ def select_callbacks_for(transition)
26
+ @callbacks.select do |callback|
27
+ (callback.from.nil? || Array(callback.from).include?(transition.from)) &&
28
+ (callback.to.nil? || Array(callback.to).include?(transition.to))
29
+ end
30
+ end
31
+
32
+ def select_constraints_for(transition)
33
+ @constraints.select do |constraint|
34
+ (constraint.from.nil? || Array(constraint.from).include?(transition.from)) &&
35
+ (constraint.to.nil? || Array(constraint.to).include?(transition.to)) &&
36
+ (constraint.role == transition.role)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ module ClosedLoop
2
+ module Machine
3
+ class Constraint < Base
4
+ def allows?(*args)
5
+ proc.call(*args)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ module ClosedLoop
2
+ module Machine
3
+ class Instance
4
+ attr_reader :configuration
5
+
6
+ delegate :transition, :callback, :constraint, to: :@configuration
7
+
8
+ def initialize
9
+ @configuration = ClosedLoop::Machine::Configuration.new(self)
10
+
11
+ configure_all
12
+ end
13
+
14
+ def available_transitions(target, user)
15
+ configuration.transitions.select { |transition| transition.available?(target, user) }
16
+ end
17
+
18
+ def transition!(target, user, to:, &block)
19
+ available_transition = available_transitions(target, user).find do |transition|
20
+ transition.from == target.status.to_sym && transition.to == to.to_sym
21
+ end
22
+
23
+ if available_transition&.available?(target, user)
24
+ available_transition.perform!(target, user, &block)
25
+ else
26
+ raise("Transition #{self.class} #{target.status}->#{to} for #{target.id} by #{user} not available!")
27
+ end
28
+ end
29
+
30
+ def resolve_role(*args)
31
+ self.class.const_get('RoleResolver').new(*args).call
32
+ end
33
+
34
+ def configure_all
35
+ raise '#configure_all not implemented yet'
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ module ClosedLoop
2
+ module Machine
3
+ class RoleResolver
4
+ attr_reader :target, :user
5
+
6
+ def initialize(target, user)
7
+ @target = target
8
+ @user = user
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,42 @@
1
+ module ClosedLoop
2
+ module Machine
3
+ class Transition < Base
4
+ def times_used
5
+ @track_times_used ||= 0
6
+ end
7
+
8
+ def available?(target, user)
9
+ role == machine.resolve_role(target, user) &&
10
+ target.status.to_sym == from &&
11
+ none_constraints?(target, user)
12
+ end
13
+
14
+ def none_constraints?(target, user)
15
+ machine.configuration.select_constraints_for(self).all? do |constraint|
16
+ constraint.allows?(target, user, transition: self)
17
+ end
18
+ end
19
+
20
+ def perform!(target, user)
21
+ target.class.transaction do
22
+ target.status = to
23
+ target.last_transition_at = Time.current if target.respond_to?(:last_transition_at)
24
+
25
+ proc&.call(target, user, transition: self)
26
+
27
+ target.save!
28
+
29
+ machine.configuration.select_callbacks_for(self).each do |callback|
30
+ callback.perform!(target, user, transition: self)
31
+ end
32
+
33
+ raise 'Callback changes were not saved' if target.changed?
34
+
35
+ yield(target, user, transition: self) if block_given?
36
+ end
37
+
38
+ @track_times_used = times_used + 1
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "machine/base"
4
+ require_relative "machine/role_resolver"
5
+ require_relative "machine/configuration"
6
+ require_relative "machine/callback"
7
+ require_relative "machine/instance"
8
+ require_relative "machine/transition"
9
+
10
+ module ClosedLoop
11
+ module Machine
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ClosedLoop
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "closed_loop/version"
4
+ require_relative "closed_loop/machine"
5
+
6
+ module ClosedLoop
7
+ class Error < StandardError; end
8
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: closed_loop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vilius Luneckas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-12 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ description:
56
+ email:
57
+ - vilius.luneckas@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/main.yml"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - CHANGELOG.md
66
+ - Example.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - closed_loop.gemspec
75
+ - lib/closed_loop.rb
76
+ - lib/closed_loop/machine.rb
77
+ - lib/closed_loop/machine/base.rb
78
+ - lib/closed_loop/machine/callback.rb
79
+ - lib/closed_loop/machine/configuration.rb
80
+ - lib/closed_loop/machine/constraint.rb
81
+ - lib/closed_loop/machine/instance.rb
82
+ - lib/closed_loop/machine/role_resolver.rb
83
+ - lib/closed_loop/machine/transition.rb
84
+ - lib/closed_loop/version.rb
85
+ homepage: https://github.com/ViliusLuneckas/closed_loop
86
+ licenses: []
87
+ metadata:
88
+ homepage_uri: https://github.com/ViliusLuneckas/closed_loop
89
+ source_code_uri: https://github.com/ViliusLuneckas/closed_loop
90
+ changelog_uri: https://github.com/ViliusLuneckas/closed_loop/blob/main/CHANGELOG.md
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 2.4.0
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.2.15
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: State machine DSL
110
+ test_files: []