minimus 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +14 -0
- data/lib/minimus.rb +16 -4
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6b58165f60db00f21039267f08075739d8f95334ffffcbcc0c8b93487685cd8
|
4
|
+
data.tar.gz: b95be46b53e44edb6ea551d6c64154964ba3c36cac788f3c02f9263408c55667
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df5bd943f10fb2c5db66e8bbc58529cfe6cb4e71585f23a4879ea79be27df120f38be4c3cc343022ab010d5b58ae8d955990e857f60d023769a4dfaeb564ee5b
|
7
|
+
data.tar.gz: 4d7472ebc3c811faeb30d29102d27746695a0e446fc595fd65e60f40d97768b71a974f1f5bc4613b8b7db72eb8a2b8a3a0afd0ee4d03e648ff72049e1eec0554
|
data/README.md
CHANGED
@@ -47,6 +47,20 @@ state.move(:updated) do
|
|
47
47
|
puts "Callback"
|
48
48
|
end
|
49
49
|
```
|
50
|
+
|
51
|
+
If you want to be slightly more unpredictable you can define your transitions:
|
52
|
+
```ruby
|
53
|
+
state.can(:new, possible: :destroyed)
|
54
|
+
state.current_state
|
55
|
+
=> :new
|
56
|
+
state.move(:destroyed)
|
57
|
+
state.current_state
|
58
|
+
=> :destroyed
|
59
|
+
```
|
60
|
+
You can also supply an array of possibilities:
|
61
|
+
```ruby
|
62
|
+
state.can(:new, possible: [:created, :destroyed])
|
63
|
+
```
|
50
64
|
## Development
|
51
65
|
|
52
66
|
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.
|
data/lib/minimus.rb
CHANGED
@@ -4,13 +4,14 @@ class Minimus
|
|
4
4
|
|
5
5
|
TransitionError = Class.new(StandardError)
|
6
6
|
|
7
|
-
attr_reader :states, :initial_state
|
7
|
+
attr_reader :states, :initial_state, :possibilities
|
8
8
|
attr_accessor :current_state
|
9
9
|
|
10
10
|
def initialize(*states)
|
11
11
|
@states = states
|
12
12
|
@initial_state = states.first
|
13
13
|
@current_state = initial_state
|
14
|
+
@possibilities = {}
|
14
15
|
end
|
15
16
|
|
16
17
|
def move(new_state)
|
@@ -22,17 +23,28 @@ class Minimus
|
|
22
23
|
|
23
24
|
def move!(new_state)
|
24
25
|
unless move_possible?(new_state)
|
25
|
-
raise TransitionError, %
|
26
|
-
#{current_state} to #{new_state}
|
26
|
+
raise TransitionError, %(unallowed transition from
|
27
|
+
#{current_state} to #{new_state})
|
27
28
|
end
|
28
29
|
self.current_state = new_state
|
29
30
|
|
30
31
|
yield if block_given?
|
31
32
|
end
|
32
33
|
|
34
|
+
def can(state, possible:)
|
35
|
+
possible_array = Array(possible)
|
36
|
+
return unless possible_array.all? { |value| states.include?(value) }
|
37
|
+
|
38
|
+
possibilities[state] = possible_array
|
39
|
+
end
|
40
|
+
|
33
41
|
private
|
34
42
|
|
35
43
|
def move_possible?(new_state)
|
36
|
-
|
44
|
+
if state_possibilities = possibilities[current_state]
|
45
|
+
state_possibilities.include?(new_state)
|
46
|
+
else
|
47
|
+
states[states.index(current_state) + 1] == new_state
|
48
|
+
end
|
37
49
|
end
|
38
50
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimus
|
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
|
- Stefan Slaveykov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|