flow_machine 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/flow_machine/version.rb +1 -1
- data/lib/flow_machine/workflow.rb +9 -3
- data/spec/flow_machine/workflow_state_spec.rb +15 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f2854b32591eca32664799e2989ca782d48a210
|
4
|
+
data.tar.gz: 67dc2d6d227bfd8d9d6e206cabd68fe9744a671f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66e5f0523da68517a158f700e6a8513ca0fc39988e105ac047c6e23d44a1ed389471f95f7ce6af1f577305cbd6d0b888053e25dc5d3296e16e5e26d4f30eb211
|
7
|
+
data.tar.gz: e439a8fc84df57039721fb849e39037f045b0d6ed82711710091c03506271ee529f94401500be86aba05866e1e941aae0174ffaf9df37729f685372eb4f27f1a
|
data/README.md
CHANGED
@@ -12,6 +12,10 @@ After exploring several of the existing Ruby state machine options, they all see
|
|
12
12
|
|
13
13
|
See [UPGRADE.md](UPGRADE.md) for details
|
14
14
|
|
15
|
+
## Requirements
|
16
|
+
|
17
|
+
Ruby 2.0+
|
18
|
+
|
15
19
|
## Simple Usage
|
16
20
|
|
17
21
|
```ruby
|
@@ -75,7 +79,7 @@ Calling the transition with a failing guard will result in the object not being
|
|
75
79
|
|
76
80
|
#### guard_errors
|
77
81
|
|
78
|
-
After calling `may_xxx?`, the workflow will have an array of the guard methods that failed.
|
82
|
+
After calling `may_xxx?`, the workflow will have an array of the guard methods that failed. To avoid additional dependencies, the developer is responsible for converting these to human readable messages (using I18n or the like). This may include `:invalid_event` in the case where a transition from the current state is not defined.
|
79
83
|
|
80
84
|
```ruby
|
81
85
|
class DraftState < FlowMachine::WorkflowState
|
data/lib/flow_machine/version.rb
CHANGED
@@ -91,10 +91,16 @@ module FlowMachine
|
|
91
91
|
end
|
92
92
|
|
93
93
|
# Defines an instance method on Workflow that delegates to the
|
94
|
-
# current state
|
94
|
+
# current state. If the current state does not support the method, then
|
95
|
+
# add an :invalid_event error to the guard_errors.
|
95
96
|
def define_state_method(method_name)
|
96
97
|
define_method method_name do |*args|
|
97
|
-
current_state.respond_to?(method_name)
|
98
|
+
if current_state.respond_to?(method_name)
|
99
|
+
current_state.send(method_name, *args)
|
100
|
+
else
|
101
|
+
self.guard_errors = [:invalid_event]
|
102
|
+
false
|
103
|
+
end
|
98
104
|
end
|
99
105
|
end
|
100
106
|
|
@@ -114,7 +120,7 @@ module FlowMachine
|
|
114
120
|
|
115
121
|
# extend Forwardable
|
116
122
|
# def_delegators :current_state, :guard_errors
|
117
|
-
delegate :guard_errors, to: :current_state
|
123
|
+
delegate :guard_errors, :guard_errors=, to: :current_state
|
118
124
|
|
119
125
|
def initialize(object, options = {})
|
120
126
|
@object = object
|
@@ -42,12 +42,13 @@ RSpec.describe FlowMachine::WorkflowState do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
describe '
|
45
|
+
describe 'invalid transitions' do
|
46
46
|
let(:state_class2) do
|
47
47
|
Class.new(described_class) do
|
48
48
|
def self.state_name; :test2; end
|
49
49
|
end
|
50
50
|
end
|
51
|
+
|
51
52
|
before :each do
|
52
53
|
state_class.event(:event1) { transition to: :test2 }
|
53
54
|
workflow_class.refresh_state_methods!
|
@@ -58,6 +59,18 @@ RSpec.describe FlowMachine::WorkflowState do
|
|
58
59
|
expect(object).to receive(:state).and_return :test2
|
59
60
|
expect(workflow.event1).to be false
|
60
61
|
end
|
62
|
+
|
63
|
+
it 'sets an invalid_event error when trying to transition to the curent state' do
|
64
|
+
expect(object).to receive(:state).and_return :test2
|
65
|
+
workflow.event1
|
66
|
+
expect(workflow.guard_errors).to eq([:invalid_event])
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'sets an invalid_event error when trying to may to the current state' do
|
70
|
+
expect(object).to receive(:state).and_return :test2
|
71
|
+
expect(workflow).not_to be_may_event1
|
72
|
+
expect(workflow.guard_errors).to eq([:invalid_event])
|
73
|
+
end
|
61
74
|
end
|
62
75
|
|
63
76
|
describe 'guards' do
|
@@ -70,7 +83,7 @@ RSpec.describe FlowMachine::WorkflowState do
|
|
70
83
|
state.event1
|
71
84
|
end
|
72
85
|
|
73
|
-
|
86
|
+
describe 'may?' do
|
74
87
|
it 'is able to transition if the guard returns true' do
|
75
88
|
expect(state).to receive(:guard1?).and_return true
|
76
89
|
expect(state.may_event1?).to be true
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Hanggi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
100
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.4.8
|
102
102
|
signing_key:
|
103
103
|
specification_version: 4
|
104
104
|
summary: A class-based state machine.
|
@@ -110,3 +110,4 @@ test_files:
|
|
110
110
|
- spec/flow_machine/workflow_spec.rb
|
111
111
|
- spec/flow_machine/workflow_state_spec.rb
|
112
112
|
- spec/spec_helper.rb
|
113
|
+
has_rdoc:
|