micromachine 1.2.0 → 2.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 695548d56ffdf4666ad19b2fb55af4a647f2f364
4
- data.tar.gz: 4331c9df0cd25f86ccdadbfe6070fcfbd31a2d21
3
+ metadata.gz: a1e4cf4c889a6b914e2b7f2e6e94e14b6ca5e97f
4
+ data.tar.gz: 7c27986b29e696f35424ae9c31a4dbf1307bf63b
5
5
  SHA512:
6
- metadata.gz: b98dae4b90be60013ad87c90db67d6c232fcc5632d038d47e691c85efb6ac5f63296f06dd94725988c36adb1715470484b5c42d87ce54c4f5bebbc3755822015
7
- data.tar.gz: b7ed4f8bf6f5a2d62589e4acb964f0f5fec4b4a773cda49cbcc367bfacb077656fa3ec4504919218a3a5df55c6ad24296503f26504609139b342f370c6ee33d0
6
+ metadata.gz: be3ccf1c76220287fe4897368d10d1bfaead7adbe62b966912b54ff8660f2cc63d96536978c66358de841d2a7a8c16fdbac71cd01f69e4a4f941547128972c0c
7
+ data.tar.gz: eae3a5f96d87b1075b30c4de5c59d6c205ca39a066e7449b88791c14b1edc53526a87340b8c3b908a10fd4c38fc3e4861bd273ab1882c05c9119b56acfc4214f
@@ -1,6 +1,6 @@
1
1
  require 'micromachine'
2
2
 
3
- # This example can be run with ruby -I lib/ example/advanced.rb
3
+ # This example can be run with ruby -I lib/ ./examples/advanced.rb
4
4
 
5
5
  fsm = MicroMachine.new(:pending)
6
6
 
@@ -21,3 +21,12 @@ fsm.trigger(:ignore)
21
21
  fsm.trigger(:reset)
22
22
 
23
23
  fsm.trigger(:ignore)
24
+
25
+ puts "Should print all states: pending, confirmed, ignored"
26
+
27
+ puts fsm.states.join ", "
28
+
29
+ puts "Should print all events: confirm, ignore, reset"
30
+
31
+ puts fsm.events.join ", "
32
+
@@ -1,6 +1,6 @@
1
1
  require 'micromachine'
2
2
 
3
- # This example can be run with ruby -I lib/ example/basic.rb
3
+ # This example can be run with ruby -I lib/ ./examples/basic.rb
4
4
 
5
5
  fsm = MicroMachine.new(:pending)
6
6
 
@@ -1,6 +1,6 @@
1
1
  require 'micromachine'
2
2
 
3
- # This example can be run with ruby -I lib/ example/callbacks.rb
3
+ # This example can be run with ruby -I lib/ ./examples/callbacks.rb
4
4
 
5
5
  fsm = MicroMachine.new(:pending)
6
6
 
@@ -2,8 +2,8 @@ class MicroMachine
2
2
  InvalidEvent = Class.new(NoMethodError)
3
3
  InvalidState = Class.new(ArgumentError)
4
4
 
5
- attr :transitions_for
6
- attr :state
5
+ attr_reader :transitions_for
6
+ attr_reader :state
7
7
 
8
8
  def initialize(initial_state)
9
9
  @state = initial_state
@@ -20,27 +20,17 @@ class MicroMachine
20
20
  end
21
21
 
22
22
  def trigger(event)
23
- if trigger?(event)
24
- @state = transitions_for[event][@state]
25
- callbacks = @callbacks[@state] + @callbacks[:any]
26
- callbacks.each { |callback| callback.call(event) }
27
- true
28
- else
29
- false
30
- end
23
+ trigger?(event) and change(event)
31
24
  end
32
25
 
33
26
  def trigger!(event)
34
- if trigger(event)
35
- true
36
- else
27
+ trigger(event) or
37
28
  raise InvalidState.new("Event '#{event}' not valid from state '#{@state}'")
38
- end
39
29
  end
40
30
 
41
31
  def trigger?(event)
42
32
  raise InvalidEvent unless transitions_for.has_key?(event)
43
- transitions_for[event][state] ? true : false
33
+ transitions_for[event].has_key?(state)
44
34
  end
45
35
 
46
36
  def events
@@ -48,10 +38,15 @@ class MicroMachine
48
38
  end
49
39
 
50
40
  def states
51
- events.map { |e| transitions_for[e].to_a }.flatten.uniq
41
+ transitions_for.values.map(&:to_a).flatten.uniq
52
42
  end
53
43
 
54
- def ==(some_state)
55
- state == some_state
44
+ private
45
+
46
+ def change(event)
47
+ @state = transitions_for[event][@state]
48
+ callbacks = @callbacks[@state] + @callbacks[:any]
49
+ callbacks.each { |callback| callback.call(event) }
50
+ true
56
51
  end
57
52
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "micromachine"
3
- s.version = "1.2.0"
3
+ s.version = "2.0.0"
4
4
  s.summary = %{Minimal Finite State Machine.}
5
5
  s.description = %Q{There are many finite state machine implementations for Ruby, and they all provide a nice DSL for declaring events, exceptions, callbacks, and all kinds of niceties in general.\n\nBut if all you want is a finite state machine, look no further: this has less than 50 lines of code and provides everything a finite state machine must have, and nothing more.}
6
6
  s.author = ["Michel Martens"]
@@ -17,11 +17,3 @@ end
17
17
  test "returns an array with the defined states" do |machine|
18
18
  assert_equal [:pending, :confirmed, :ignored], machine.states
19
19
  end
20
-
21
- test "returns false if compared state is not equal to current" do |machine|
22
- assert !(machine == :confirmed)
23
- end
24
-
25
- test "returns true if compared state is equal to current" do |machine|
26
- assert machine == :pending
27
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micromachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-03 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest