guided_path 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.document +5 -0
  2. data/.rvmrc +52 -0
  3. data/Gemfile +18 -0
  4. data/Gemfile.lock +48 -0
  5. data/Guardfile +12 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.rdoc +19 -0
  8. data/Rakefile +46 -0
  9. data/VERSION +1 -0
  10. data/guided_path.gemspec +87 -0
  11. data/lib/guided_path/goto_node.rb +23 -0
  12. data/lib/guided_path/lead_in_node.rb +28 -0
  13. data/lib/guided_path/multimedia_node.rb +38 -0
  14. data/lib/guided_path/node.rb +33 -0
  15. data/lib/guided_path/parser/spec1.rb +206 -0
  16. data/lib/guided_path/program.rb +84 -0
  17. data/lib/guided_path/question_multiple_choice_node.rb +28 -0
  18. data/lib/guided_path/question_textbox_node.rb +31 -0
  19. data/lib/guided_path/script.rb +14 -0
  20. data/lib/guided_path/text_node.rb +26 -0
  21. data/lib/guided_path.rb +10 -0
  22. data/test/fixtures/spec1/empty_file.yml +0 -0
  23. data/test/fixtures/spec1/goto_jump_script_a.yml +7 -0
  24. data/test/fixtures/spec1/goto_jump_script_b.yml +6 -0
  25. data/test/fixtures/spec1/goto_node_jumps.yml +10 -0
  26. data/test/fixtures/spec1/goto_simple_jumps.yml +10 -0
  27. data/test/fixtures/spec1/image_captions.yml +10 -0
  28. data/test/fixtures/spec1/image_simple.yml +3 -0
  29. data/test/fixtures/spec1/leadin_multiple.yml +8 -0
  30. data/test/fixtures/spec1/multiple_simple.yml +5 -0
  31. data/test/fixtures/spec1/multiple_with_loops.yml +19 -0
  32. data/test/fixtures/spec1/multiple_with_sub_questions.yml +10 -0
  33. data/test/fixtures/spec1/sequences_three_textnodes.yml +7 -0
  34. data/test/fixtures/spec1/textbox_simple.yml +10 -0
  35. data/test/fixtures/spec1/textnode_simple_attribute_list.yml +3 -0
  36. data/test/fixtures/spec1/textnode_simple_hash.yml +1 -0
  37. data/test/fixtures/spec1/textnode_simple_string.yml +2 -0
  38. data/test/fixtures/spec1/yes_no_simple.yml +11 -0
  39. data/test/fixtures/spec1/youtube_captions.yml +10 -0
  40. data/test/fixtures/spec1/youtube_simple.yml +6 -0
  41. data/test/fixtures/test/a_fixture.yml +1 -0
  42. data/test/guided_path/parser/spec1/test_goto.rb +44 -0
  43. data/test/guided_path/parser/spec1/test_lead_in_nodes.rb +19 -0
  44. data/test/guided_path/parser/spec1/test_multimedia_nodes.rb +41 -0
  45. data/test/guided_path/parser/spec1/test_question_multiple.rb +37 -0
  46. data/test/guided_path/parser/spec1/test_question_textbox.rb +17 -0
  47. data/test/guided_path/parser/spec1/test_question_yes_no.rb +23 -0
  48. data/test/guided_path/parser/spec1/test_sequences.rb +22 -0
  49. data/test/guided_path/parser/spec1/test_text_nodes.rb +23 -0
  50. data/test/guided_path/program/test_program_output.rb +52 -0
  51. data/test/guided_path/program/test_program_script_handling.rb +29 -0
  52. data/test/guided_path/test_helper_functions.rb +16 -0
  53. data/test/guided_path/test_text_node.rb +26 -0
  54. data/test/helper.rb +32 -0
  55. metadata +280 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p194"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.14.3 (master)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ if [[ $- == *i* ]] # check for interactive shells
29
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
30
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
31
+ fi
32
+ else
33
+ # If the environment file has not yet been created, use the RVM CLI to select.
34
+ rvm --create use "$environment_id" || {
35
+ echo "Failed to create RVM environment '${environment_id}'."
36
+ return 1
37
+ }
38
+ fi
39
+
40
+ # If you use bundler, this might be useful to you:
41
+ # if [[ -s Gemfile ]] && {
42
+ # ! builtin command -v bundle >/dev/null ||
43
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
44
+ # }
45
+ # then
46
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
47
+ # gem install bundler
48
+ # fi
49
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
50
+ # then
51
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
52
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'activemodel'
6
+ gem 'activesupport'
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ gem 'guard'
11
+ gem 'json'
12
+ gem 'ruby_gntp'
13
+ gem 'rb-fsevent'
14
+ gem "minitest"
15
+ gem 'guard-minitest'
16
+ gem "rdoc"
17
+ gem "bundler"
18
+ gem "jeweler"
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.3)
5
+ activesupport (= 3.2.3)
6
+ builder (~> 3.0.0)
7
+ activesupport (3.2.3)
8
+ i18n (~> 0.6)
9
+ multi_json (~> 1.0)
10
+ builder (3.0.0)
11
+ ffi (1.0.11)
12
+ git (1.2.5)
13
+ guard (1.0.2)
14
+ ffi (>= 0.5.0)
15
+ thor (~> 0.14.6)
16
+ guard-minitest (0.5.0)
17
+ guard (>= 0.4)
18
+ i18n (0.6.0)
19
+ jeweler (1.8.3)
20
+ bundler (~> 1.0)
21
+ git (>= 1.2.5)
22
+ rake
23
+ rdoc
24
+ json (1.7.1)
25
+ minitest (2.12.1)
26
+ multi_json (1.3.2)
27
+ rake (0.9.2.2)
28
+ rb-fsevent (0.9.1)
29
+ rdoc (3.12)
30
+ json (~> 1.4)
31
+ ruby_gntp (0.3.4)
32
+ thor (0.14.6)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ activemodel
39
+ activesupport
40
+ bundler
41
+ guard
42
+ guard-minitest
43
+ jeweler
44
+ json
45
+ minitest
46
+ rb-fsevent
47
+ rdoc
48
+ ruby_gntp
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+
5
+ guard 'minitest' do
6
+ # with Minitest::Unit
7
+ watch(%r|^test/(.*)\/?test_(.*)\.rb|)
8
+ watch(%r|^lib/(.*)|) { "test"}
9
+ watch(%r|^test/helper\.rb|) { "test" }
10
+
11
+ end
12
+
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 James
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = guided_path
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to guided_path
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 James. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "guided_path"
18
+ gem.homepage = "http://github.com/statisticalfool/guided_path"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Parser for making interactive exercises in a human readable DSL}
21
+ gem.description = %Q{Parser for making interactive exercises in a human readable DSL}
22
+ gem.email = "github@jamesstuart.org"
23
+ gem.authors = ["James"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+
36
+ task :default => :test
37
+
38
+ require 'rdoc/task'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "guided_path #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,87 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "guided_path"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James"]
12
+ s.date = "2012-05-28"
13
+ s.description = "Parser for making interactive exercises in a human readable DSL"
14
+ s.email = "github@jamesstuart.org"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "Guardfile",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/guided_path.rb",
29
+ "lib/guided_path/node.rb",
30
+ "lib/guided_path/parser/spec1.rb",
31
+ "lib/guided_path/program.rb",
32
+ "lib/guided_path/script.rb",
33
+ "lib/guided_path/text_node.rb",
34
+ "test/fixtures/spec1/empty_file.yml",
35
+ "test/fixtures/spec1/sequences_three_textnodes.yml",
36
+ "test/fixtures/spec1/textnode_simple_attribute_list.yml",
37
+ "test/fixtures/spec1/textnode_simple_hash.yml",
38
+ "test/fixtures/spec1/textnode_simple_string.yml",
39
+ "test/fixtures/test/a_fixture.yml",
40
+ "test/guided_path/parser/spec1/test_sequences.rb",
41
+ "test/guided_path/parser/spec1/test_text_nodes.rb",
42
+ "test/guided_path/program/test_program_output.rb",
43
+ "test/guided_path/program/test_program_script_handling.rb",
44
+ "test/guided_path/test_helper_functions.rb",
45
+ "test/guided_path/test_text_node.rb",
46
+ "test/helper.rb"
47
+ ]
48
+ s.homepage = "http://github.com/statisticalfool/guided_path"
49
+ s.licenses = ["MIT"]
50
+ s.require_paths = ["lib"]
51
+ s.rubygems_version = "1.8.24"
52
+ s.summary = "Parser for making interactive exercises in a human readable DSL"
53
+
54
+ if s.respond_to? :specification_version then
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ s.add_runtime_dependency(%q<psych>, [">= 0"])
59
+ s.add_runtime_dependency(%q<activemodel>, [">= 0"])
60
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
61
+ s.add_runtime_dependency(%q<guard>, [">= 0"])
62
+ s.add_runtime_dependency(%q<json>, [">= 0"])
63
+ s.add_runtime_dependency(%q<rdoc>, [">= 0"])
64
+ s.add_runtime_dependency(%q<bundler>, [">= 0"])
65
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
66
+ else
67
+ s.add_dependency(%q<psych>, [">= 0"])
68
+ s.add_dependency(%q<activemodel>, [">= 0"])
69
+ s.add_dependency(%q<activesupport>, [">= 0"])
70
+ s.add_dependency(%q<guard>, [">= 0"])
71
+ s.add_dependency(%q<json>, [">= 0"])
72
+ s.add_dependency(%q<rdoc>, [">= 0"])
73
+ s.add_dependency(%q<bundler>, [">= 0"])
74
+ s.add_dependency(%q<jeweler>, [">= 0"])
75
+ end
76
+ else
77
+ s.add_dependency(%q<psych>, [">= 0"])
78
+ s.add_dependency(%q<activemodel>, [">= 0"])
79
+ s.add_dependency(%q<activesupport>, [">= 0"])
80
+ s.add_dependency(%q<guard>, [">= 0"])
81
+ s.add_dependency(%q<json>, [">= 0"])
82
+ s.add_dependency(%q<rdoc>, [">= 0"])
83
+ s.add_dependency(%q<bundler>, [">= 0"])
84
+ s.add_dependency(%q<jeweler>, [">= 0"])
85
+ end
86
+ end
87
+
@@ -0,0 +1,23 @@
1
+ require "active_model"
2
+ require "active_support/core_ext/hash"
3
+
4
+ module GuidedPath
5
+ class GotoNode < Node
6
+
7
+
8
+ def initialize(args = {})
9
+ super
10
+ args = args.symbolize_keys
11
+ raise("Must specify target for a goto node") if @next_node.empty?
12
+ end
13
+
14
+
15
+ def to_hash
16
+ output = {}
17
+ output = super
18
+ output[:type] = 'goto'
19
+ output
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ require "active_model"
2
+ require "active_support/core_ext/hash"
3
+
4
+ module GuidedPath
5
+ class LeadInNode < Node
6
+
7
+ attr_reader :text, :pause
8
+
9
+ def initialize(args = {})
10
+ super
11
+ args = args.symbolize_keys
12
+
13
+ raise(ArgumentError, "Must specify a value for the node") unless args[:text]
14
+ @text = args[:text].to_s
15
+ @pause = args[:pause].to_s
16
+ end
17
+
18
+
19
+ def to_hash
20
+ output = super
21
+ output[:type] = 'leadin'
22
+ output[:text] = @text
23
+ output[:pause] = @pause
24
+ output
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ require "active_model"
2
+ require "active_support/core_ext/hash"
3
+
4
+ module GuidedPath
5
+ class MultimediaNode < Node
6
+
7
+ CATEGORIES = %w{image youtube}
8
+ attr_reader :source, :url, :caption
9
+
10
+ def initialize(args = {})
11
+ super
12
+ args = args.symbolize_keys
13
+
14
+ raise(ArgumentError, "Must specify a value for the node") unless args[:url]
15
+ @url = args[:url].to_s
16
+
17
+ raise(ArgumentError, "Must specify a source") unless args[:url]
18
+ @source = args[:source].to_s
19
+ raise(ArgumentError, "Must specify a source in #{CATEGORIES.join(', ')}") unless CATEGORIES.include?(@source)
20
+ @caption = args[:caption]
21
+ if @caption.kind_of?(String)
22
+ @caption = {text: @caption, position: 'bottom'}
23
+ elsif @caption.kind_of?(Hash)
24
+ @caption = {text: @caption['text'], position: @caption['position'] || 'top'}
25
+ end
26
+ end
27
+
28
+
29
+ def to_hash
30
+ output = super
31
+ output[:type] = @source
32
+ output[:url] = @url
33
+ output[:caption] = @caption
34
+ output
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ require "active_model"
2
+
3
+ module GuidedPath
4
+ class Node
5
+ attr_accessor :label
6
+ attr_reader :next_node
7
+
8
+ def initialize(args = {})
9
+
10
+ raise(ArgumentError, "Options must be a hash") unless args.kind_of?(Hash)
11
+ args = args.symbolize_keys
12
+ @label = args[:label]
13
+
14
+ @next_node = (args[:next_node] || args[:goto])
15
+ @next_node = @next_node.to_s.strip if @next_node
16
+
17
+ end
18
+
19
+ def next_node=(value)
20
+ @next_node = value
21
+ end
22
+
23
+ def to_hash
24
+ output = {}
25
+ if @next_node
26
+ output[:next_node] = @next_node.respond_to?(:label) ? @next_node.label : @next_node.to_s
27
+ end
28
+
29
+ output
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,206 @@
1
+ module GuidedPath
2
+ module Parser
3
+ class Spec1
4
+ def initialize(options = {})
5
+ @program = options[:program] || raise(ArgumentError, "Cannot run a parser without a program specified")
6
+ @script = preprocess_script(options[:script])
7
+ @script_name = options[:script_name] || raise(ArgumentError, 'Must specify a script name')
8
+ parse_script(@script)
9
+ end
10
+
11
+ private
12
+
13
+ def parse_script(script)
14
+ if (node = find_content_node(script))
15
+ return add_node(node:node)
16
+ elsif script.kind_of?(Array)
17
+ parse_sequence_node(script)
18
+ end
19
+ end
20
+
21
+ def parse_sequence_node(script)
22
+ last_node = nil
23
+ last_added_nodes = []
24
+ script.each do |node|
25
+ existing_nodes = @program.nodes.keys
26
+ new_node = parse_script(node)
27
+ added_nodes = @program.nodes.reject { |label, n| existing_nodes.include?(label) || n.next_node }
28
+
29
+ last_added_nodes.each { |label, n| n.next_node = new_node }
30
+
31
+ last_node = new_node
32
+ last_added_nodes = added_nodes
33
+ end
34
+
35
+
36
+ return last_node
37
+ end
38
+
39
+ def find_content_node(script)
40
+
41
+ new_node = nil
42
+ new_node ||= find_goto_node(script)
43
+ new_node ||= find_lead_in_node(script)
44
+ new_node ||= find_question_node(script)
45
+ new_node ||= find_multimedia_node(script)
46
+ new_node ||= find_simple_textnode(script)
47
+ new_node ||= find_hash_textnode(script)
48
+ return new_node
49
+ end
50
+
51
+ def find_lead_in_node(script)
52
+
53
+ if script.kind_of?(Hash) && (next_node = (script["lead_in"]))
54
+ LeadInNode.new(next_node)
55
+ else
56
+ nil
57
+ end
58
+ end
59
+
60
+ MULTIMEDIA_CATEGORIES = %w{image youtube}
61
+ def find_multimedia_node(script)
62
+
63
+ if script.kind_of?(Hash)
64
+ if (multimedia_category = script.keys.detect { |k| MULTIMEDIA_CATEGORIES.include?(k) })
65
+ mm_params = script[multimedia_category]
66
+ mm_params['source'] = multimedia_category
67
+ new_node ||= MultimediaNode.new(mm_params)
68
+ return new_node
69
+ end
70
+ end
71
+ end
72
+
73
+ def find_goto_node(script)
74
+
75
+ if script.kind_of?(Hash) && (next_node = (script["goto"] || script["next_node"]))
76
+ GotoNode.new(next_node: next_node)
77
+ else
78
+ nil
79
+ end
80
+ end
81
+
82
+ def add_node(options = {})
83
+ @program.add_node({:script_name => @script_name}.merge(options))
84
+ end
85
+
86
+
87
+ def parse_question_textbox(script)
88
+
89
+ no_start = @program.starting_node.nil?
90
+ raise "Question formed invalidly. Did you indent the question enough?\n\n#{script.inspect}" unless script.values.length == 1
91
+ question_script = script.values.first
92
+ raise "Question node should be a hash underneath." unless question_script.kind_of?(Hash)
93
+
94
+ question_script['answers']['~not listed~'] ||= "Sorry, the answer you gave was not listed."
95
+ question_script['answers']['~blank~'] ||= "Sorry, you must provide an answer."
96
+ question_script['answers'].each do |name, subscript|
97
+ question_script['answers'][name] = parse_script(subscript)
98
+ end
99
+
100
+ new_node = QuestionTextBoxNode.new(question_script)
101
+
102
+ if no_start
103
+ @program.starting_node = new_node
104
+ end
105
+
106
+ return new_node
107
+ end
108
+ def parse_question_yes_no(script)
109
+ no_start = @program.starting_node.nil?
110
+
111
+ raise "Question formed invalidly. Did you indent the question enough?\n\n#{script.inspect}" unless script.values.length == 1
112
+ question_script = script.values.first
113
+ raise "Question node should be a hash underneath." unless question_script.kind_of?(Hash)
114
+
115
+ yes_label = ['yes', 'Yes', 'true', 'True', true].detect { |label| question_script.has_key?(label)}
116
+ no_label = ['no','No','false','False', false].detect { |label| question_script.has_key?(label)}
117
+
118
+ raise "No yes node found" if yes_label.nil?
119
+ raise "No no node found" if no_label.nil?
120
+
121
+ question_script['answers'] = {
122
+ 'Yes' => parse_script(question_script[yes_label]),
123
+ 'No' => parse_script(question_script[no_label])
124
+ }
125
+
126
+
127
+ new_node = QuestionMultipleChoiceNode.new(question_script)
128
+
129
+ if no_start
130
+ @program.starting_node = new_node
131
+ end
132
+
133
+ return new_node
134
+
135
+ end
136
+
137
+ def parse_question_multiple_choice(script)
138
+ no_start = @program.starting_node.nil?
139
+
140
+ raise "Question formed invalidly. Did you indent the question enough?\n\n#{script.inspect}" unless script.values.length == 1
141
+ question_script = script.values.first
142
+ raise "Question node should be a hash underneath." unless question_script.kind_of?(Hash)
143
+ raise "No answers node found in:\n\n#{script.inspect}" unless question_script.has_key?('answers')
144
+ question_script['answers'].each do |name, subscript|
145
+ question_script['answers'][name] = parse_script(subscript)
146
+ end
147
+ new_node = QuestionMultipleChoiceNode.new(question_script)
148
+ if no_start
149
+ @program.starting_node = new_node
150
+ end
151
+
152
+ return new_node
153
+ end
154
+
155
+ def find_question_node(script)
156
+ return nil unless script.kind_of?(Hash)
157
+
158
+ if (question_hash = (script['q'] || script['question']))
159
+ case question_hash['type']
160
+ when 'yes/no'
161
+ parse_question_yes_no(script)
162
+ when 'multiple choice'
163
+ parse_question_multiple_choice(script)
164
+ when 'textbox'
165
+ parse_question_textbox(script)
166
+ else
167
+ parse_question_multiple_choice(script)
168
+ end
169
+ elsif script['multiple choice q'] || script['multiple choice question']
170
+ parse_question_multiple_choice(script)
171
+ elsif script['yes/no q'] || script['yes/no question']
172
+ parse_question_yes_no(script)
173
+ elsif script['textbox q'] || script['textbox question']
174
+ parse_question_textbox(script)
175
+ else
176
+ nil
177
+ end
178
+ end
179
+
180
+
181
+ def find_simple_textnode(script)
182
+ if script.kind_of?(String) || script.kind_of?(Symbol)
183
+ return TextNode.new(value: script)
184
+ end
185
+
186
+ return nil
187
+ end
188
+
189
+ def find_hash_textnode(script)
190
+ if script.kind_of?(Hash) && (text_node = script['text'])
191
+ new_node = find_simple_textnode(text_node)
192
+ new_node ||= TextNode.new(text_node) if text_node.kind_of?(Hash)
193
+ return new_node
194
+ end
195
+ end
196
+
197
+
198
+ def preprocess_script(raw_script)
199
+ raise "Must specify script" unless raw_script
200
+ YAML.load(raw_script)
201
+ end
202
+
203
+
204
+ end
205
+ end
206
+ end