-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatemachine_eg3.rb
55 lines (37 loc) · 951 Bytes
/
statemachine_eg3.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
require "statemachine"
class VendingMachineContext
attr_accessor :statemachine
def initialize
@amount_tendered = 0
end
def add_coins( of_value )
@amount_tendered += of_value
end
def check_amount_tendered
if @amount_tendered >= 100
@statemachine.paid
else
@statemachine.not_paid_yet
end
end
def prompt_money
puts "£ .#{ @amount_tendered }: more money please."
end
def prompt_selection
puts "Please make a selection."
end
end
$vending_machine = Statemachine.build do
trans( :accept_money, :coin, :coin_inserted, :add_coins )
state( :coin_inserted ) do
event( :not_paid_yet, :accept_money, :prompt_money )
event( :paid, :await_selection, :prompt_selection )
on_entry( :check_amount_tendered )
end
context( VendingMachineContext.new )
end
$vending_machine.context.statemachine = $vending_machine
def vm
$vending_machine
end