baby_bots 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/lib/baby_bots/baby_bot.rb +17 -2
- data/lib/baby_bots/state.rb +5 -1
- data/lib/baby_bots/version.rb +1 -1
- data/spec/baby_bot_spec.rb +16 -1
- metadata +3 -2
data/.gitignore
ADDED
data/lib/baby_bots/baby_bot.rb
CHANGED
@@ -24,7 +24,8 @@ module BabyBots
|
|
24
24
|
# Accepts an optional hash of states.
|
25
25
|
def initialize(states={})
|
26
26
|
# Hash of state names to state objects
|
27
|
-
@states =
|
27
|
+
@states = {}
|
28
|
+
if !states.empty? then build states end
|
28
29
|
# Initial state
|
29
30
|
@start = nil
|
30
31
|
# Current state
|
@@ -97,7 +98,7 @@ module BabyBots
|
|
97
98
|
end
|
98
99
|
|
99
100
|
# calculate the next state
|
100
|
-
if cooked_event
|
101
|
+
if !cooked_event.nil?
|
101
102
|
next_state = @states[curr.table[cooked_event]]
|
102
103
|
else
|
103
104
|
next_state = @states[curr.table[event]]
|
@@ -139,6 +140,20 @@ module BabyBots
|
|
139
140
|
@curr = @start
|
140
141
|
end
|
141
142
|
|
143
|
+
# Equality is based on if all the states are the same, and if
|
144
|
+
# the machines are currently in the same state.
|
145
|
+
def ==(another_baby)
|
146
|
+
if @curr != another_baby.curr
|
147
|
+
return false
|
148
|
+
end
|
149
|
+
|
150
|
+
@states.keys.each do |k|
|
151
|
+
if @states[k] != another_baby.states[k] then return false end
|
152
|
+
end
|
153
|
+
|
154
|
+
return true
|
155
|
+
end
|
156
|
+
|
142
157
|
private
|
143
158
|
|
144
159
|
# A wrapper around send, using the arity restrictions assumed by BabyBots.
|
data/lib/baby_bots/state.rb
CHANGED
@@ -21,7 +21,6 @@ module BabyBots
|
|
21
21
|
|
22
22
|
# Adds a transition to the transition table. Table format is
|
23
23
|
# event => transition, where event is the "input" into the state.
|
24
|
-
#
|
25
24
|
# Transitions are allowed to be deleted by being set to NOWHERE.
|
26
25
|
def add_transition(event, transition)
|
27
26
|
if transition == NOWHERE
|
@@ -44,6 +43,11 @@ module BabyBots
|
|
44
43
|
def remove_transition(event)
|
45
44
|
@table.delete(event)
|
46
45
|
end
|
46
|
+
|
47
|
+
# Equality is based on the same transition table.
|
48
|
+
def ==(another_state)
|
49
|
+
if @table == another_state.table then return true else return false end
|
50
|
+
end
|
47
51
|
end
|
48
52
|
|
49
53
|
end
|
data/lib/baby_bots/version.rb
CHANGED
data/spec/baby_bot_spec.rb
CHANGED
@@ -59,7 +59,7 @@ describe BabyBots::BabyBot do
|
|
59
59
|
|
60
60
|
it "should be able to be initialized with a state table" do
|
61
61
|
test = BabyBots::BabyBot.new(TEST_MACHINE1)
|
62
|
-
test.states.should ==
|
62
|
+
test.states.should == {:loading => $loading, :ready => $ready, :run => $run}
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should have states be able to be added using add_state" do
|
@@ -72,6 +72,21 @@ describe BabyBots::BabyBot do
|
|
72
72
|
test.states.should == {:loading => $loading, :ready => $ready, :run => $run}
|
73
73
|
end
|
74
74
|
|
75
|
+
it "should be able to have states built using the build method" do
|
76
|
+
test = BabyBots::BabyBot.new
|
77
|
+
test.build(TEST_MACHINE1)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should allow states to be overwritten using build" do
|
81
|
+
test = BabyBots::BabyBot.new(TEST_MACHINE1)
|
82
|
+
test.build({ :loading => {1 => :run, 2 => :loading} })
|
83
|
+
|
84
|
+
test2 = BabyBots::BabyBot.new({ :loading => {1 => :run, 2 => :loading},
|
85
|
+
:ready => {1 => :run, :else => :loading},
|
86
|
+
:run => {:else => :run} })
|
87
|
+
test.states.should == test2.states
|
88
|
+
end
|
89
|
+
|
75
90
|
it "should run the example, with the starting state being loading" do
|
76
91
|
test = BB.new
|
77
92
|
test.start.state.should == :loading
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: baby_bots
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-07 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: A tiny finite-state automata library.
|
15
15
|
email: justinanthonyhamilton@gmail.com
|
@@ -17,6 +17,7 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- .gitignore
|
20
21
|
- Gemfile
|
21
22
|
- LICENSE
|
22
23
|
- README.md
|