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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +14 -0
  3. data/lib/minimus.rb +16 -4
  4. data/lib/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c2c45e4939846a21da793f985ffc84a6db92766b91d1249c1dcd43f3ac6cdec
4
- data.tar.gz: 5d2a2b58cf8b7b4b30e3c6d1f50c9e5ec4bede1c729bd8bc6f2b96b57620993d
3
+ metadata.gz: f6b58165f60db00f21039267f08075739d8f95334ffffcbcc0c8b93487685cd8
4
+ data.tar.gz: b95be46b53e44edb6ea551d6c64154964ba3c36cac788f3c02f9263408c55667
5
5
  SHA512:
6
- metadata.gz: bdd01e32223bfed0751f890745e31494834b5e799758e99d77910b58947bae33eb55556f1451904283cc1e05cc79a8d7011147122fd1797e09e34c98e2d71087
7
- data.tar.gz: acb5b90260359fe305d3628ccd1783273c25573560593f0a25f0ad5a631b7ff7f97002036a5ca075d1cab8c2458981e04cdb7714c1a8ff74966adc3e084471c3
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.
@@ -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, %Q{unallowed transition from
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
- states[states.index(current_state) + 1] == new_state
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
@@ -1,3 +1,3 @@
1
1
  class Minimus
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
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.0
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-02-21 00:00:00.000000000 Z
11
+ date: 2018-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler