flow_machine 0.2.1 → 0.2.2
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 +5 -5
 - data/README.md +2 -1
 - data/lib/flow_machine/version.rb +1 -1
 - data/lib/flow_machine/workflow/model_extension.rb +32 -0
 - data/lib/flow_machine/workflow.rb +2 -21
 - data/spec/flow_machine/workflow/model_extension_spec.rb +55 -0
 - data/spec/flow_machine/workflow_spec.rb +1 -2
 - metadata +12 -10
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 2 
     | 
    
         
            +
            SHA256:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 6cd2fd744bfa9fae413fd4c4f5c9d47d05f80520623fdc5d95dd8be0bf0e13b1
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: f8f593c69fe42dbce33166fa81c3a92886bb70fe81169f24f1620f6c7d7733d8
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 1459686da1123e9358ec5bbfc73d52e5db44058aa1f691b9a5ce36f0fddd01ba1a38f28eea8646974c0463843b1c5c2ad26b4d260649431bfade61294e0e9bd6
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 7006a927ea623460f60069e2ae9b082996fa571799651830692920924b61147fec783895093d3b1bf29dbebd76a4777606a61b658ffc3249012e7dfbf3d9d127
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -4,13 +4,14 @@ Build finite state machines in a backend-agnostic, class-centric way. 
     | 
|
| 
       4 
4 
     | 
    
         | 
| 
       5 
5 
     | 
    
         
             
            The basic features will work with any PORO, and more features and callbacks are available when used with an ORM like `ActiveRecord` and/or `ActiveModel::Dirty`.
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
      
 7 
     | 
    
         
            +
             _Circle CI Status:_ [](https://circleci.com/gh/tablexi/flow_machine)
         
     | 
| 
       7 
8 
     | 
    
         
             
            ## *Raison d'être*
         
     | 
| 
       8 
9 
     | 
    
         | 
| 
       9 
10 
     | 
    
         
             
            After exploring several of the existing Ruby state machine options, they all seem too tightly coupled to an ORM models and tend to pollute the object model's code far too much. The goal of this gem is to provide a clean, testable interface for working with a state machine that decouples as much as possible from the model object itself.
         
     | 
| 
       10 
11 
     | 
    
         | 
| 
       11 
12 
     | 
    
         
             
            ## Upgrading
         
     | 
| 
       12 
13 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
      
 14 
     | 
    
         
            +
            [CHANGELOG.md](CHANGELOG.md) will contain changes that need to be made at each version
         
     | 
| 
       14 
15 
     | 
    
         | 
| 
       15 
16 
     | 
    
         
             
            ## Requirements
         
     | 
| 
       16 
17 
     | 
    
         | 
    
        data/lib/flow_machine/version.rb
    CHANGED
    
    
| 
         @@ -0,0 +1,32 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module FlowMachine
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Workflow
         
     | 
| 
      
 3 
     | 
    
         
            +
                module ModelExtension
         
     | 
| 
      
 4 
     | 
    
         
            +
                  # resolves to
         
     | 
| 
      
 5 
     | 
    
         
            +
                  # class Model
         
     | 
| 
      
 6 
     | 
    
         
            +
                  #   def self.published
         
     | 
| 
      
 7 
     | 
    
         
            +
                  #     where(status: 'published')
         
     | 
| 
      
 8 
     | 
    
         
            +
                  #   end
         
     | 
| 
      
 9 
     | 
    
         
            +
                  #
         
     | 
| 
      
 10 
     | 
    
         
            +
                  #   def published?
         
     | 
| 
      
 11 
     | 
    
         
            +
                  #     self.status == 'published'
         
     | 
| 
      
 12 
     | 
    
         
            +
                  #   end
         
     | 
| 
      
 13 
     | 
    
         
            +
                  # end
         
     | 
| 
      
 14 
     | 
    
         
            +
                  def create_scopes_on(content_class)
         
     | 
| 
      
 15 
     | 
    
         
            +
                    state_method = self.state_method
         
     | 
| 
      
 16 
     | 
    
         
            +
                    state_names.each do |status|
         
     | 
| 
      
 17 
     | 
    
         
            +
                      # Don't add the scope classes if `where` is not defined since it means
         
     | 
| 
      
 18 
     | 
    
         
            +
                      # you're likely not in a ORM class.
         
     | 
| 
      
 19 
     | 
    
         
            +
                      if content_class.respond_to?(:where)
         
     | 
| 
      
 20 
     | 
    
         
            +
                        content_class.singleton_class.send(:define_method, status) do
         
     | 
| 
      
 21 
     | 
    
         
            +
                          where(state_method => status)
         
     | 
| 
      
 22 
     | 
    
         
            +
                        end
         
     | 
| 
      
 23 
     | 
    
         
            +
                      end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                      content_class.send(:define_method, "#{status}?") do
         
     | 
| 
      
 26 
     | 
    
         
            +
                        public_send(state_method) == status
         
     | 
| 
      
 27 
     | 
    
         
            +
                      end
         
     | 
| 
      
 28 
     | 
    
         
            +
                    end
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
                end
         
     | 
| 
      
 31 
     | 
    
         
            +
              end
         
     | 
| 
      
 32 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -1,6 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require "active_support/core_ext/module/delegation"
         
     | 
| 
       2 
2 
     | 
    
         
             
            require "active_support/core_ext/object/try"
         
     | 
| 
       3 
3 
     | 
    
         
             
            require "flow_machine/workflow/factory_methods"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "flow_machine/workflow/model_extension"
         
     | 
| 
       4 
5 
     | 
    
         | 
| 
       5 
6 
     | 
    
         
             
            module FlowMachine
         
     | 
| 
       6 
7 
     | 
    
         
             
              module Workflow
         
     | 
| 
         @@ -8,31 +9,11 @@ module FlowMachine 
     | 
|
| 
       8 
9 
     | 
    
         | 
| 
       9 
10 
     | 
    
         
             
                def self.included(base)
         
     | 
| 
       10 
11 
     | 
    
         
             
                  base.extend(ClassMethods)
         
     | 
| 
      
 12 
     | 
    
         
            +
                  base.extend(ModelExtension)
         
     | 
| 
       11 
13 
     | 
    
         
             
                  base.send(:attr_reader, :object)
         
     | 
| 
       12 
14 
     | 
    
         
             
                end
         
     | 
| 
       13 
15 
     | 
    
         | 
| 
       14 
16 
     | 
    
         
             
                module ClassMethods
         
     | 
| 
       15 
     | 
    
         
            -
                  # resolves to
         
     | 
| 
       16 
     | 
    
         
            -
                  # class Model
         
     | 
| 
       17 
     | 
    
         
            -
                  #   def self.published
         
     | 
| 
       18 
     | 
    
         
            -
                  #     where(status: 'published')
         
     | 
| 
       19 
     | 
    
         
            -
                  #   end
         
     | 
| 
       20 
     | 
    
         
            -
                  #
         
     | 
| 
       21 
     | 
    
         
            -
                  #   def published?
         
     | 
| 
       22 
     | 
    
         
            -
                  #     self.status == 'published'
         
     | 
| 
       23 
     | 
    
         
            -
                  #   end
         
     | 
| 
       24 
     | 
    
         
            -
                  # end
         
     | 
| 
       25 
     | 
    
         
            -
                  def create_scopes_on(content_class)
         
     | 
| 
       26 
     | 
    
         
            -
                    self.state_names.each do |status|
         
     | 
| 
       27 
     | 
    
         
            -
                      content_class.singleton_class.send(:define_method, status) do
         
     | 
| 
       28 
     | 
    
         
            -
                        where(status: status)
         
     | 
| 
       29 
     | 
    
         
            -
                      end
         
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
                      content_class.send(:define_method, "#{status}?") do
         
     | 
| 
       32 
     | 
    
         
            -
                        self.status == status
         
     | 
| 
       33 
     | 
    
         
            -
                      end
         
     | 
| 
       34 
     | 
    
         
            -
                    end
         
     | 
| 
       35 
     | 
    
         
            -
                  end
         
     | 
| 
       36 
17 
     | 
    
         | 
| 
       37 
18 
     | 
    
         
             
                  attr_accessor :callbacks
         
     | 
| 
       38 
19 
     | 
    
         | 
| 
         @@ -0,0 +1,55 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'ostruct'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            RSpec.describe FlowMachine::Workflow::ModelExtension do
         
     | 
| 
      
 4 
     | 
    
         
            +
              class Test1State < FlowMachine::WorkflowState
         
     | 
| 
      
 5 
     | 
    
         
            +
              end
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              class Test2State < FlowMachine::WorkflowState
         
     | 
| 
      
 8 
     | 
    
         
            +
              end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
              class TestWorkflow
         
     | 
| 
      
 11 
     | 
    
         
            +
                include FlowMachine::Workflow
         
     | 
| 
      
 12 
     | 
    
         
            +
                state Test1State
         
     | 
| 
      
 13 
     | 
    
         
            +
                state Test2State
         
     | 
| 
      
 14 
     | 
    
         
            +
              end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
              describe '.create_scopes_on' do
         
     | 
| 
      
 17 
     | 
    
         
            +
                class PersistedModel < Struct.new(:state)
         
     | 
| 
      
 18 
     | 
    
         
            +
                  def self.where(opts)
         
     | 
| 
      
 19 
     | 
    
         
            +
                    [new(opts[:state])]
         
     | 
| 
      
 20 
     | 
    
         
            +
                  end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                  TestWorkflow.create_scopes_on(self)
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                class UnPersistedModel < Struct.new(:state)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  TestWorkflow.create_scopes_on(self)
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                it 'adds the predicate model for state 1' do
         
     | 
| 
      
 30 
     | 
    
         
            +
                  expect(PersistedModel.new('test1')).to be_test1
         
     | 
| 
      
 31 
     | 
    
         
            +
                  expect(PersistedModel.new('test2')).not_to be_test1
         
     | 
| 
      
 32 
     | 
    
         
            +
                end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                it 'adds the predicate model for state 2' do
         
     | 
| 
      
 35 
     | 
    
         
            +
                  expect(PersistedModel.new('test1')).not_to be_test2
         
     | 
| 
      
 36 
     | 
    
         
            +
                  expect(PersistedModel.new('test2')).to be_test2
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                it 'adds a scope for test1' do
         
     | 
| 
      
 40 
     | 
    
         
            +
                  expect(PersistedModel.test1).to be_an(Array)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  expect(PersistedModel.test1).to be_one
         
     | 
| 
      
 42 
     | 
    
         
            +
                  expect(PersistedModel.test1.first).to eq(PersistedModel.new('test1'))
         
     | 
| 
      
 43 
     | 
    
         
            +
                end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                it 'adds a scope for test2' do
         
     | 
| 
      
 46 
     | 
    
         
            +
                  expect(PersistedModel.test2).to be_an(Array)
         
     | 
| 
      
 47 
     | 
    
         
            +
                  expect(PersistedModel.test2).to be_one
         
     | 
| 
      
 48 
     | 
    
         
            +
                  expect(PersistedModel.test2.first).to eq(PersistedModel.new('test2'))
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                it 'does not add scopes if where is not defined' do
         
     | 
| 
      
 52 
     | 
    
         
            +
                  expect(UnPersistedModel).not_to respond_to(:test1)
         
     | 
| 
      
 53 
     | 
    
         
            +
                end
         
     | 
| 
      
 54 
     | 
    
         
            +
              end
         
     | 
| 
      
 55 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -121,8 +121,7 @@ RSpec.describe FlowMachine::Workflow do 
     | 
|
| 
       121 
121 
     | 
    
         | 
| 
       122 
122 
     | 
    
         
             
                  it 'does not call for invalid states' do
         
     | 
| 
       123 
123 
     | 
    
         
             
                    expect(workflow).not_to receive(:after_transition_callback)
         
     | 
| 
       124 
     | 
    
         
            -
                     
     | 
| 
       125 
     | 
    
         
            -
                    expect { workflow.transition to: :invalid_state }.to raise_error
         
     | 
| 
      
 124 
     | 
    
         
            +
                    expect { workflow.transition to: :invalid_state }.to raise_error(ArgumentError)
         
     | 
| 
       126 
125 
     | 
    
         
             
                  end
         
     | 
| 
       127 
126 
     | 
    
         | 
| 
       128 
127 
     | 
    
         
             
                  it 'does not call for ending in the same state' do
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: flow_machine
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.2. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.2
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Jason Hanggi
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date:  
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2019-01-11 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activesupport
         
     | 
| 
         @@ -30,14 +30,14 @@ dependencies: 
     | 
|
| 
       30 
30 
     | 
    
         
             
                requirements:
         
     | 
| 
       31 
31 
     | 
    
         
             
                - - "~>"
         
     | 
| 
       32 
32 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       33 
     | 
    
         
            -
                    version: 3. 
     | 
| 
      
 33 
     | 
    
         
            +
                    version: 3.8.0
         
     | 
| 
       34 
34 
     | 
    
         
             
              type: :development
         
     | 
| 
       35 
35 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       36 
36 
     | 
    
         
             
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       37 
37 
     | 
    
         
             
                requirements:
         
     | 
| 
       38 
38 
     | 
    
         
             
                - - "~>"
         
     | 
| 
       39 
39 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       40 
     | 
    
         
            -
                    version: 3. 
     | 
| 
      
 40 
     | 
    
         
            +
                    version: 3.8.0
         
     | 
| 
       41 
41 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       42 
42 
     | 
    
         
             
              name: rspec-its
         
     | 
| 
       43 
43 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
         @@ -69,10 +69,12 @@ files: 
     | 
|
| 
       69 
69 
     | 
    
         
             
            - lib/flow_machine/version.rb
         
     | 
| 
       70 
70 
     | 
    
         
             
            - lib/flow_machine/workflow.rb
         
     | 
| 
       71 
71 
     | 
    
         
             
            - lib/flow_machine/workflow/factory_methods.rb
         
     | 
| 
      
 72 
     | 
    
         
            +
            - lib/flow_machine/workflow/model_extension.rb
         
     | 
| 
       72 
73 
     | 
    
         
             
            - lib/flow_machine/workflow_state.rb
         
     | 
| 
       73 
74 
     | 
    
         
             
            - lib/tasks/workflow_tasks.rake
         
     | 
| 
       74 
75 
     | 
    
         
             
            - spec/flow_machine/factory_methods_spec.rb
         
     | 
| 
       75 
76 
     | 
    
         
             
            - spec/flow_machine/multiple_workflow_spec.rb
         
     | 
| 
      
 77 
     | 
    
         
            +
            - spec/flow_machine/workflow/model_extension_spec.rb
         
     | 
| 
       76 
78 
     | 
    
         
             
            - spec/flow_machine/workflow_callback_spec.rb
         
     | 
| 
       77 
79 
     | 
    
         
             
            - spec/flow_machine/workflow_change_callback_spec.rb
         
     | 
| 
       78 
80 
     | 
    
         
             
            - spec/flow_machine/workflow_spec.rb
         
     | 
| 
         @@ -98,16 +100,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       98 
100 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       99 
101 
     | 
    
         
             
            requirements: []
         
     | 
| 
       100 
102 
     | 
    
         
             
            rubyforge_project: 
         
     | 
| 
       101 
     | 
    
         
            -
            rubygems_version: 2. 
     | 
| 
      
 103 
     | 
    
         
            +
            rubygems_version: 2.7.7
         
     | 
| 
       102 
104 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       103 
105 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       104 
106 
     | 
    
         
             
            summary: A class-based state machine.
         
     | 
| 
       105 
107 
     | 
    
         
             
            test_files:
         
     | 
| 
      
 108 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 109 
     | 
    
         
            +
            - spec/flow_machine/workflow_spec.rb
         
     | 
| 
      
 110 
     | 
    
         
            +
            - spec/flow_machine/workflow_state_spec.rb
         
     | 
| 
       106 
111 
     | 
    
         
             
            - spec/flow_machine/factory_methods_spec.rb
         
     | 
| 
       107 
     | 
    
         
            -
            - spec/flow_machine/multiple_workflow_spec.rb
         
     | 
| 
       108 
112 
     | 
    
         
             
            - spec/flow_machine/workflow_callback_spec.rb
         
     | 
| 
      
 113 
     | 
    
         
            +
            - spec/flow_machine/workflow/model_extension_spec.rb
         
     | 
| 
       109 
114 
     | 
    
         
             
            - spec/flow_machine/workflow_change_callback_spec.rb
         
     | 
| 
       110 
     | 
    
         
            -
            - spec/flow_machine/ 
     | 
| 
       111 
     | 
    
         
            -
            - spec/flow_machine/workflow_state_spec.rb
         
     | 
| 
       112 
     | 
    
         
            -
            - spec/spec_helper.rb
         
     | 
| 
       113 
     | 
    
         
            -
            has_rdoc: 
         
     | 
| 
      
 115 
     | 
    
         
            +
            - spec/flow_machine/multiple_workflow_spec.rb
         
     |