tuvi 0.0.8 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +22 -24
- data/bin/tuvi +8 -0
- data/example_program.rb +7 -7
- data/lib/tuvi/application_runner.rb +41 -0
- data/lib/tuvi/step.rb +14 -12
- data/lib/tuvi/version.rb +1 -1
- data/lib/tuvi.rb +4 -34
- data/spec/tuvi/application_runner_spec.rb +27 -0
- data/spec/tuvi/step_spec.rb +0 -6
- data/spec/tuvi_spec.rb +25 -28
- metadata +10 -5
data/README.md
CHANGED
@@ -15,31 +15,31 @@ Here is a basic example program followed by an explanation:
|
|
15
15
|
require 'tuvi'
|
16
16
|
|
17
17
|
step 1 do
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
say "Welcome to KrazyMaze. Will you ever get out? BWAAHAAA! You have a choice to make: Go right or left"
|
19
|
+
response "right" => 2
|
20
|
+
response "left" => 3
|
21
21
|
end
|
22
22
|
|
23
23
|
step 2 do
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
say "You're in the middle of the maze. Go right or left or straight."
|
25
|
+
response "right" => 3
|
26
|
+
response "left" => 4
|
27
|
+
response "straight" => 3
|
28
28
|
end
|
29
29
|
|
30
30
|
step 3 do
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
say "You've encountered a monster! You can either go straight, or punch the monster."
|
32
|
+
response "straight" => 2
|
33
|
+
response "punch" => 5
|
34
34
|
end
|
35
35
|
|
36
36
|
step 4 do
|
37
|
-
|
37
|
+
say "Drat. You've reached the end of the maze! I'll get you next time..."
|
38
38
|
stop
|
39
39
|
end
|
40
40
|
|
41
41
|
step 5 do
|
42
|
-
|
42
|
+
say "The monster was stronger than you! You died."
|
43
43
|
stop
|
44
44
|
end
|
45
45
|
|
@@ -48,15 +48,13 @@ Here is a basic example program followed by an explanation:
|
|
48
48
|
|
49
49
|
First, create a ruby file and on the first line require the Tuvi library with `require 'tuvi'`
|
50
50
|
|
51
|
-
Then, create a class with any name you desire. The first thing to do inside this class is: `extend Tuvi`.
|
52
|
-
|
53
51
|
From here on in, the Tuvi language is used. As you can see, the program is divided into steps, which represent a step in the program where the program provides some instructions, and either prompts the user for input or simply exits.
|
54
52
|
|
55
|
-
Every step needs a `
|
53
|
+
Every step needs a `say`, which sets the computer's instructions for that step.
|
56
54
|
|
57
|
-
Every step can establish one or more lines declaring an `
|
55
|
+
Every step can establish one or more lines declaring an `response` which is followed first by a valid user response, a hashrocket arrow, and then by the step number that that particular user response should lead to.
|
58
56
|
|
59
|
-
Some steps, instead of having
|
57
|
+
Some steps, instead of having responses, can simply end the program using the `stop` keyword.
|
60
58
|
|
61
59
|
Finally, after all the steps have been declared (but still within the class), the `run` keyword actually runs the steps.
|
62
60
|
|
@@ -66,9 +64,9 @@ You can also add any custom Ruby code by placing it block passed to the `code` m
|
|
66
64
|
|
67
65
|
step 1 do
|
68
66
|
code {$steps = 1}
|
69
|
-
|
70
|
-
|
71
|
-
|
67
|
+
say "Welcome to KrazyMaze. Will you ever get out? BWAAHAAA! You have a choice to make: Go right or left"
|
68
|
+
response "right" => 2
|
69
|
+
response "left" => 3
|
72
70
|
end
|
73
71
|
|
74
72
|
step 2 do
|
@@ -76,10 +74,10 @@ You can also add any custom Ruby code by placing it block passed to the `code` m
|
|
76
74
|
$steps += 1
|
77
75
|
puts "You have taken #{$steps} steps."
|
78
76
|
end
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
77
|
+
say "You're in the middle of the maze. Go right or left or straight."
|
78
|
+
response "right" => 3
|
79
|
+
response "left" => 4
|
80
|
+
response "straight" => 3
|
83
81
|
end
|
84
82
|
|
85
83
|
|
data/bin/tuvi
ADDED
data/example_program.rb
CHANGED
@@ -2,20 +2,20 @@ require_relative 'lib/tuvi.rb'
|
|
2
2
|
|
3
3
|
step 1 do
|
4
4
|
code {$x = "hello"}
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
say "This is Step 1. Type yes to go to Step 2. Type no to go to Step 3."
|
6
|
+
response "Yes" => 2
|
7
|
+
response "No" => 3
|
8
8
|
end
|
9
9
|
|
10
10
|
step 2 do
|
11
11
|
code {puts $x}
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
say "This is Step 2. Type hi to go to Step 3. Type bye to go to step 1."
|
13
|
+
response "Hi" => 3
|
14
|
+
response "Bye" => 1
|
15
15
|
end
|
16
16
|
|
17
17
|
step 3 do
|
18
|
-
|
18
|
+
say "You've reached the end!"
|
19
19
|
stop
|
20
20
|
end
|
21
21
|
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class ApplicationRunner
|
2
|
+
|
3
|
+
def initialize(steps)
|
4
|
+
@steps = steps
|
5
|
+
end
|
6
|
+
|
7
|
+
def run
|
8
|
+
current_step_id = 1
|
9
|
+
while true do
|
10
|
+
current_step_id = execute_step(current_step_id)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute_step(step_id)
|
15
|
+
current_step = @steps[step_id]
|
16
|
+
current_step.code_blocks.each do |block|
|
17
|
+
block.call
|
18
|
+
end
|
19
|
+
puts
|
20
|
+
puts current_step.get_say
|
21
|
+
exit if current_step.exit_program
|
22
|
+
puts current_step.formatted_responses
|
23
|
+
input = gets.downcase.chomp
|
24
|
+
exit_program if input == "exit"
|
25
|
+
determine_next_step(current_step, input)
|
26
|
+
end
|
27
|
+
|
28
|
+
def determine_next_step(current_step, input)
|
29
|
+
if current_step.response_paths[input]
|
30
|
+
return current_step.response_paths[input]
|
31
|
+
end
|
32
|
+
|
33
|
+
puts "Sorry, I don't understand that response. Please try again:"
|
34
|
+
current_step.id
|
35
|
+
end
|
36
|
+
|
37
|
+
def exit_program
|
38
|
+
puts "Bye!"
|
39
|
+
exit
|
40
|
+
end
|
41
|
+
end
|
data/lib/tuvi/step.rb
CHANGED
@@ -1,32 +1,34 @@
|
|
1
1
|
class Step
|
2
2
|
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :id, :response_paths, :exit_program, :code_blocks
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
@
|
7
|
-
@
|
5
|
+
def initialize(id, &block)
|
6
|
+
@id = id
|
7
|
+
@response_paths = {}
|
8
8
|
@code_blocks = []
|
9
9
|
instance_eval(&block) if block_given?
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
@
|
12
|
+
def say(text)
|
13
|
+
@say = text
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
@
|
16
|
+
def get_say
|
17
|
+
@say
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
20
|
+
def response(response_path)
|
21
|
+
response = response_path.first[0].downcase
|
22
|
+
next_step = response_path.first[1]
|
23
|
+
@response_paths[response] = next_step
|
22
24
|
end
|
23
25
|
|
24
26
|
def stop
|
25
27
|
@exit_program = true
|
26
28
|
end
|
27
29
|
|
28
|
-
def
|
29
|
-
"You can type one of the following: [#{@
|
30
|
+
def formatted_responses
|
31
|
+
"You can type one of the following: [#{@response_paths.keys.join(", ")}]. Enter 'exit' to quit."
|
30
32
|
end
|
31
33
|
|
32
34
|
def code(&block)
|
data/lib/tuvi/version.rb
CHANGED
data/lib/tuvi.rb
CHANGED
@@ -1,46 +1,16 @@
|
|
1
1
|
require_relative "tuvi/version"
|
2
2
|
require_relative "tuvi/step"
|
3
|
-
|
3
|
+
require_relative "tuvi/application_runner"
|
4
4
|
|
5
5
|
module Tuvi
|
6
6
|
|
7
|
-
def step(
|
7
|
+
def step(id, &block)
|
8
8
|
@steps ||= {}
|
9
|
-
@steps[
|
9
|
+
@steps[id] = Step.new(id, &block)
|
10
10
|
end
|
11
11
|
|
12
12
|
def run
|
13
|
-
|
14
|
-
while true do
|
15
|
-
current_step_position = execute_step(current_step_position)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def execute_step(step_position)
|
20
|
-
current_step = @steps[step_position]
|
21
|
-
current_step.code_blocks.each do |block|
|
22
|
-
block.call
|
23
|
-
end
|
24
|
-
puts current_step.get_message
|
25
|
-
exit if current_step.exit_program
|
26
|
-
puts current_step.formatted_answers
|
27
|
-
input = gets.downcase.chomp
|
28
|
-
exit_program if input == "exit"
|
29
|
-
determine_next_step(current_step, input)
|
30
|
-
end
|
31
|
-
|
32
|
-
def determine_next_step(current_step, input)
|
33
|
-
if current_step.answer_paths[input]
|
34
|
-
return current_step.answer_paths[input]
|
35
|
-
end
|
36
|
-
|
37
|
-
puts "Sorry, I don't understand that answer. Please try again:"
|
38
|
-
current_step.position
|
39
|
-
end
|
40
|
-
|
41
|
-
def exit_program
|
42
|
-
puts "Bye!"
|
43
|
-
exit
|
13
|
+
ApplicationRunner.new(@steps).run
|
44
14
|
end
|
45
15
|
|
46
16
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApplicationRunner do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@step = Step.new(1) do
|
7
|
+
@response_paths = {"yes" => 2}
|
8
|
+
end
|
9
|
+
@application_runner = ApplicationRunner.new([@step])
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "Running the Tuvi program" do
|
13
|
+
|
14
|
+
describe "determine_next_step" do
|
15
|
+
|
16
|
+
it "should determine the next step based on response paths" do
|
17
|
+
@application_runner.determine_next_step(@step, "yes").should == 2
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should set the next step to be the current step if input is invalid" do
|
21
|
+
@application_runner.determine_next_step(@step, "blah").should == 1
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/spec/tuvi/step_spec.rb
CHANGED
data/spec/tuvi_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe Tuvi do
|
|
10
10
|
@program = Program.new
|
11
11
|
end
|
12
12
|
|
13
|
-
describe "
|
13
|
+
describe "Step syntax" do
|
14
14
|
|
15
15
|
describe "step" do
|
16
16
|
|
@@ -19,50 +19,47 @@ describe Tuvi do
|
|
19
19
|
@program.instance_eval{@steps[1]}.class.should == Step
|
20
20
|
end
|
21
21
|
|
22
|
-
it "should assign number parameter to step's
|
22
|
+
it "should assign number parameter to step's id" do
|
23
23
|
@program.step(1)
|
24
|
-
@program.instance_eval{@steps[1]}.
|
24
|
+
@program.instance_eval{@steps[1]}.id.should == 1
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
@program.step(1){message "Hello!"}
|
29
|
-
@program.instance_eval{@steps[1]}.get_message.should == "Hello!"
|
30
|
-
end
|
27
|
+
end
|
31
28
|
|
32
|
-
|
33
|
-
@program.step(1){answer "yes", 2}
|
34
|
-
@program.instance_eval{@steps[1]}.answer_paths["yes"].should == 2
|
35
|
-
end
|
29
|
+
describe "say" do
|
36
30
|
|
37
|
-
it "should
|
38
|
-
@program.step(1){
|
39
|
-
@program.instance_eval{@steps[1]}.
|
31
|
+
it "should assign a say based on say passed in block" do
|
32
|
+
@program.step(1){say "Hello!"}
|
33
|
+
@program.instance_eval{@steps[1]}.get_say.should == "Hello!"
|
40
34
|
end
|
35
|
+
|
41
36
|
end
|
42
37
|
|
43
|
-
describe "
|
38
|
+
describe "response" do
|
44
39
|
|
45
|
-
it "should
|
46
|
-
@program.step(1){
|
47
|
-
|
48
|
-
@program.determine_next_step(current_step, "yes").should == 2
|
40
|
+
it "should create an response path with the key and the key should be downcased" do
|
41
|
+
@program.step(1){response "Yes" => 2}
|
42
|
+
@program.instance_eval{@steps[1]}.response_paths["yes"].should == 2
|
49
43
|
end
|
50
44
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "stop" do
|
48
|
+
|
49
|
+
it "should mark a step as exit_program if 'stop' is passed in block" do
|
50
|
+
@program.step(1){stop}
|
51
|
+
@program.instance_eval{@steps[1]}.exit_program.should be_true
|
55
52
|
end
|
56
53
|
|
57
54
|
end
|
58
55
|
|
59
|
-
|
56
|
+
describe "code" do
|
60
57
|
|
61
|
-
|
58
|
+
it "should add blocks to the step's code_blocks" do
|
59
|
+
@program.step(1){code {puts "Hello"} }
|
60
|
+
@program.instance_eval{@steps[1]}.code_blocks[0].should be_a Proc
|
61
|
+
end
|
62
62
|
|
63
|
-
it "should add blocks to the step's code_blocks" do
|
64
|
-
@program.step(1){code {puts "Hello"} }
|
65
|
-
@program.instance_eval{@steps[1]}.code_blocks[0].should be_a Proc
|
66
63
|
end
|
67
64
|
|
68
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tuvi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
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:
|
12
|
+
date: 2014-01-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -62,7 +62,8 @@ dependencies:
|
|
62
62
|
description: Tuvi is a computer language for kids to create text-based games and applications.
|
63
63
|
email:
|
64
64
|
- jaywngrw@gmail.com
|
65
|
-
executables:
|
65
|
+
executables:
|
66
|
+
- tuvi
|
66
67
|
extensions: []
|
67
68
|
extra_rdoc_files: []
|
68
69
|
files:
|
@@ -72,12 +73,15 @@ files:
|
|
72
73
|
- LICENSE.txt
|
73
74
|
- README.md
|
74
75
|
- Rakefile
|
76
|
+
- bin/tuvi
|
75
77
|
- example_program.rb
|
76
78
|
- lib/.DS_Store
|
77
79
|
- lib/tuvi.rb
|
80
|
+
- lib/tuvi/application_runner.rb
|
78
81
|
- lib/tuvi/step.rb
|
79
82
|
- lib/tuvi/version.rb
|
80
83
|
- spec/spec_helper.rb
|
84
|
+
- spec/tuvi/application_runner_spec.rb
|
81
85
|
- spec/tuvi/step_spec.rb
|
82
86
|
- spec/tuvi_spec.rb
|
83
87
|
- tuvi.gemspec
|
@@ -96,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
100
|
version: '0'
|
97
101
|
segments:
|
98
102
|
- 0
|
99
|
-
hash:
|
103
|
+
hash: -2205103365390497425
|
100
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
105
|
none: false
|
102
106
|
requirements:
|
@@ -105,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
109
|
version: '0'
|
106
110
|
segments:
|
107
111
|
- 0
|
108
|
-
hash:
|
112
|
+
hash: -2205103365390497425
|
109
113
|
requirements: []
|
110
114
|
rubyforge_project:
|
111
115
|
rubygems_version: 1.8.25
|
@@ -115,5 +119,6 @@ summary: Tuvi is a Ruby DSL which supplies a declarative format which dictates t
|
|
115
119
|
flow of the program as a series of steps.
|
116
120
|
test_files:
|
117
121
|
- spec/spec_helper.rb
|
122
|
+
- spec/tuvi/application_runner_spec.rb
|
118
123
|
- spec/tuvi/step_spec.rb
|
119
124
|
- spec/tuvi_spec.rb
|