hifsm 0.6.0 → 0.6.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: e8c840d5a3b6429146d55b87579db8d29ca6c6c6
4
- data.tar.gz: 2130df37d77e426940c430a7b9a29abc4619838e
3
+ metadata.gz: b1387604547449d9ca598e37da2f65819b7a04af
4
+ data.tar.gz: e3701a877c217a7a6bafe2c20805a5a30c1f0ef0
5
5
  SHA512:
6
- metadata.gz: a182af19cfb5cc12291e395c4041934d9ae5c25997d865ab573807bbf18b27efcc7c131038b0f3bd18f8fae5d5d4440ce4d08bece8ea0b8efd8dd3c3bdb668bb
7
- data.tar.gz: b9e9e923ea20dfbf4cb335a65c297b877397559cfc2bc2b12be2a7ea1d34b9837fec772e8c857431a863fd7a1dac48d35601d232741bb6f39f3b8e081e9597d6
6
+ metadata.gz: c9edb501d065c107cefda523991a00b5bd093859f5e46f6403fa1ffab03e2fed90fa5f52bd21e8450aa308d7c62f948ef39c7a7b3caa2c23964220c19e8aa808
7
+ data.tar.gz: 8851be66fe4d6f2523ef1f949510667c5ab4dd9fc9a330b72f28fdcdbc35d400e753ec073464e7b4911abc92c72922e39efc2531b6d904223d05b7070f4ff063
data/README.md CHANGED
@@ -227,6 +227,16 @@ Order.processing_delivering.first.cancel!.save # save is never called inisde hi
227
227
 
228
228
  ```
229
229
 
230
+ ## Get possible transitions from current state
231
+
232
+ The machine instance has `valid_events` method, with accepts arguments, that are passed to all event guards to find out if it is possible to fire the event.
233
+
234
+ ```ruby
235
+ monster.state_machine.valid_events # -> ['reached', 'sight', 'acquire', ...]
236
+ ```
237
+
238
+ The order of events is not guaranteed, events for parent states are included.
239
+
230
240
  ## Testing
231
241
 
232
242
  Only 'public' API is unit-tested, internal implementation may be freely changed, so don't rely on it.
@@ -13,6 +13,8 @@ module Hifsm
13
13
  @state = initial_state.enter!
14
14
  end
15
15
 
16
+ # Public API
17
+
16
18
  def act!(*args)
17
19
  @state.act!(@target, *args)
18
20
  end
@@ -21,10 +23,19 @@ module Hifsm
21
23
  @state
22
24
  end
23
25
 
26
+ def valid_events(*args)
27
+ @state.valid_events(@target, *args)
28
+ end
29
+
24
30
  def states
25
31
  @fsm.states
26
32
  end
27
33
 
34
+ def to_s
35
+ @state.to_s
36
+ end
37
+
38
+ # internals
28
39
  def all_states
29
40
  @fsm.all_states.reject(&:sub_fsm).collect(&:to_s)
30
41
  end
@@ -34,9 +45,5 @@ module Hifsm
34
45
  @state = new_state
35
46
  end
36
47
  end
37
-
38
- def to_s
39
- @state.to_s
40
- end
41
48
  end
42
49
  end
@@ -50,6 +50,17 @@ module Hifsm
50
50
  @sub_fsm.get_state!(name)
51
51
  end
52
52
 
53
+ def valid_events(target, *args)
54
+ own_events = events.find_all do |event_name|
55
+ @transitions[event_name].any? {|event| event.guard?(target, *args)}
56
+ end
57
+ if @parent
58
+ (own_events + @parent.valid_events(target, *args)).uniq
59
+ else
60
+ own_events.uniq
61
+ end
62
+ end
63
+
53
64
  def fire(target, event_name, *args, &new_state_callback)
54
65
  event_name = event_name.to_s
55
66
  @transitions[event_name].each do |ev|
@@ -1,3 +1,3 @@
1
1
  module Hifsm
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -37,4 +37,13 @@ class TestBeforeReturningFalse < Minitest::Test
37
37
  # assert nothing raised
38
38
  end
39
39
 
40
+ def test_valid_events_include_parent_state_events
41
+ assert_equal ['lock', 'open'], @door.state_machine.valid_events.sort
42
+ end
43
+
44
+ def test_valid_events_include_parent_state_events
45
+ @door.lock
46
+ assert_equal ['open'], @door.state_machine.valid_events.sort
47
+ end
48
+
40
49
  end
@@ -53,4 +53,19 @@ class TestEventGuard < Minitest::Test
53
53
  assert_equal '3', wall.last_hit_strength
54
54
  end
55
55
 
56
+ def test_valid_events_for_thick_wall_and_low_power
57
+ wall = Wall.new(10)
58
+ assert_equal [], wall.state_machine.valid_events
59
+ end
60
+
61
+ def test_valid_events_for_thick_wall_high_power
62
+ wall = Wall.new(10)
63
+ assert_equal ['break'], wall.state_machine.valid_events(3)
64
+ end
65
+
66
+ def test_valid_events_for_thin_wall
67
+ wall = Wall.new(4)
68
+ assert_equal ['break'], wall.state_machine.valid_events
69
+ end
70
+
56
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hifsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Meremyanin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-10 00:00:00.000000000 Z
11
+ date: 2014-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler