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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53a0f25050636262e9a70c54fbc1d38824faff7d
4
- data.tar.gz: f88f42d717458e68efdcaf2bd496ab9687cbbbdf
3
+ metadata.gz: 9a443d34e42386dc16b0868bef778d1e656fca8c
4
+ data.tar.gz: 10476e760bdb83cc72ee67a159ca44905e113a03
5
5
  SHA512:
6
- metadata.gz: 80ddc8acd2c3a7aa0515193ead91df37e4f67ef465a567219b32874956d2cc2ce4639f02b69a297979d698176cdea29b677e4e7d7419e00ca3c7f0347258c73a
7
- data.tar.gz: 87329a11b4e1c6e48b0b0fd904ce2ea296a395c345d84461b7117e512175e182e8635779ed698c9b990b8beb584e5cda5cb74e296ca3fc3fbc03a2b4525c62c7
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 that is at the beginning or
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 where a state can be changed
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,4 @@
1
+ module LinearWorkFlow
2
+ class InvalidStateError < StandardError; end
3
+ class ChangeStateError < StandardError; end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module LinearWorkFlow
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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.0
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: