rms 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rms.rb +94 -0
  2. metadata +46 -0
@@ -0,0 +1,94 @@
1
+
2
+
3
+ # Usage:
4
+ # require 'rms'
5
+ # class Blub < StateMachine
6
+ # def initialize
7
+ # super :welcome
8
+ # @name = 'World'
9
+ # end
10
+ # def welcome_enter(env)
11
+ # puts 'Enter Name: '
12
+ # end
13
+ # def welcome(env)
14
+ # @name = env[:line] if env[:line] != ''
15
+ # puts "Hello #{@name}"
16
+ # end
17
+ # end
18
+ # bla = Blub.new
19
+ # # prints out 'Enter Name: '
20
+ # loop do
21
+ # line = gets.strip
22
+ # bla.process({:line => line})
23
+ # end
24
+
25
+ class StateMachine
26
+ def initialize(start_state=:start, error_state=:error)
27
+ @error_state = error_state
28
+ @state = :none
29
+ switch(start_state, {})
30
+ end
31
+
32
+ def switch(new_state, env)
33
+ # call the enter method...
34
+ begin
35
+ m_enter = method((new_state.to_s + '_enter').to_sym)
36
+ m_enter.call(env)
37
+ rescue NameError
38
+ # nope didnt exist
39
+ end
40
+ # now check if there is such a method(with the name new_state)
41
+ @state_method = method(new_state.to_sym)
42
+ @state = new_state.to_sym
43
+ end
44
+
45
+ def process(env)
46
+ unless @state_method
47
+ begin
48
+ @state_method = method(@state)
49
+ rescue NameError
50
+ switch(@error_state, env)
51
+ end
52
+ end
53
+ @state_method.call(env)
54
+ end
55
+ end
56
+
57
+ if __FILE__ == 'rms.rb'
58
+ require 'readline'
59
+ # check it out ;)
60
+ class Blub < StateMachine
61
+ def initialize
62
+ super
63
+ end
64
+ def start_enter(env)
65
+ puts 'Enter your username...'
66
+ end
67
+ def start(env)
68
+ if env[:line] == 'user'
69
+ switch('auth', {:username => 'user'})
70
+ else
71
+ puts 'User not valid...'
72
+ puts 'Enter your username...'
73
+ end
74
+ end
75
+ def auth_enter(env)
76
+ puts "Welcome #{env[:username]}"
77
+ puts "Please enter your password..."
78
+ end
79
+ def auth(env)
80
+ if env[:line] == 'blub'
81
+ puts "Welcome..."
82
+ else
83
+ puts "wrong password..."
84
+ switch(:start, {})
85
+ end
86
+ end
87
+ end
88
+ b = Blub.new
89
+ puts 'entering main-loop...'
90
+ loop do
91
+ line = Readline::readline('-> ')
92
+ b.process({:line => line})
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: rms
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.2"
7
+ date: 2007-07-04 00:00:00 +02:00
8
+ summary: A simple StateMachine
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: "true"
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Tassilo Schweyer
31
+ files:
32
+ - lib/rms.rb
33
+ test_files: []
34
+
35
+ rdoc_options: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ requirements: []
44
+
45
+ dependencies: []
46
+