linear_work_flow 0.1.0 → 0.1.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 +2 -2
- data/lib/linear_work_flow/errors.rb +4 -0
- data/lib/linear_work_flow/version.rb +1 -1
- data/lib/linear_work_flow/work_flow.rb +71 -0
- data/linear_work_flow-0.1.0.gem +0 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a443d34e42386dc16b0868bef778d1e656fca8c
|
4
|
+
data.tar.gz: 10476e760bdb83cc72ee67a159ca44905e113a03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01186b7c858e17ffd03cf49c08eb7284c5a9dbedc58b0a424356285d858bab44fc06b4a085ba64f83aafa1f2b9f4a20557063731e1bbce28a8d88f949b90f2c7
|
7
|
+
data.tar.gz: 95cda2b8de3322f4e487168753547a818d0699d7f1a593e5bd1e839f91e3582a5a82d9bba983afcfba389e417f25d1ccd65295e8429f8101bb9727a764175b6a
|
data/README.md
CHANGED
@@ -65,7 +65,7 @@ work_flow.state == :two
|
|
65
65
|
|
66
66
|
### Check first and last
|
67
67
|
|
68
|
-
`first?` and `last?` methods can be used to check if
|
68
|
+
`first?` and `last?` methods can be used to check if the work flow is at the beginning or
|
69
69
|
end of the defined states.
|
70
70
|
|
71
71
|
```ruby
|
@@ -85,7 +85,7 @@ work_flow.state == last_state == :four
|
|
85
85
|
work_flow.last? == true
|
86
86
|
```
|
87
87
|
|
88
|
-
### Check
|
88
|
+
### Check whether a state can be changed
|
89
89
|
|
90
90
|
Use the `can?` method to check whether a work flow can be move forward or back.
|
91
91
|
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module LinearWorkFlow
|
2
|
+
class WorkFlow
|
3
|
+
|
4
|
+
def self.states
|
5
|
+
[
|
6
|
+
:first,
|
7
|
+
:last
|
8
|
+
]
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :index
|
12
|
+
|
13
|
+
def initialize(state=nil)
|
14
|
+
self.index = state ? states.index(state) : 0
|
15
|
+
raise(InvalidStateError, "State must be in: #{states.inspect}") unless self.index
|
16
|
+
end
|
17
|
+
class << self
|
18
|
+
alias_method :at, :new
|
19
|
+
end
|
20
|
+
|
21
|
+
def state
|
22
|
+
states[index]
|
23
|
+
end
|
24
|
+
|
25
|
+
def forward!
|
26
|
+
raise(ChangeStateError, "Cannot go forward from last state") if last?
|
27
|
+
self.index += 1
|
28
|
+
end
|
29
|
+
|
30
|
+
def back!
|
31
|
+
raise(ChangeStateError, "Cannot go back from first state") if first?
|
32
|
+
self.index -= 1
|
33
|
+
end
|
34
|
+
|
35
|
+
def last?
|
36
|
+
state == states.last
|
37
|
+
end
|
38
|
+
|
39
|
+
def first?
|
40
|
+
index == 0
|
41
|
+
end
|
42
|
+
|
43
|
+
def can?(action)
|
44
|
+
!!restore_after do
|
45
|
+
begin
|
46
|
+
send(actions[action])
|
47
|
+
rescue ChangeStateError
|
48
|
+
false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def actions
|
54
|
+
{
|
55
|
+
forward: :forward!,
|
56
|
+
back: :back!
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
def states
|
61
|
+
self.class.states
|
62
|
+
end
|
63
|
+
|
64
|
+
def restore_after
|
65
|
+
starting_index = index
|
66
|
+
result = yield
|
67
|
+
self.index = starting_index
|
68
|
+
result
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linear_work_flow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Nichols
|
@@ -69,7 +69,10 @@ files:
|
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
71
|
- lib/linear_work_flow.rb
|
72
|
+
- lib/linear_work_flow/errors.rb
|
72
73
|
- lib/linear_work_flow/version.rb
|
74
|
+
- lib/linear_work_flow/work_flow.rb
|
75
|
+
- linear_work_flow-0.1.0.gem
|
73
76
|
- linear_work_flow.gemspec
|
74
77
|
homepage: https://github.com/reggieb/linear_work_flow
|
75
78
|
licenses:
|