class GuidedPath::Program
Attributes
nodes[R]
parser[R]
starting_node[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/guided_path/program.rb, line 7 def initialize(options = {}) @parser = options[:parser] || GuidedPath::Parser::Spec1 @scripts = options[:scripts] || raise(ArgumentError, "Must specify scripts.") raise(ArgumentError, "Scripts must be an hash") unless @scripts.kind_of?(Hash) raise(ArgumentError, "Must pass along at least one script") if @scripts.empty? @starting_script = options[:starting_script] || raise(ArgumentError, "Must specify starting script.") raise(ArgumentError, "Starting script not present in script hash") unless @scripts.has_key?(@starting_script) parse_scripts postprocess_scripts end
Public Instance Methods
add_node(options)
click to toggle source
# File lib/guided_path/program.rb, line 22 def add_node(options) script_name = options[:script_name] || raise(ArgumentError, "Script name missing") node = options[:node] || raise(ArgumentError, "Script node") full_label = nil if node.next_node.kind_of?(String) node.next_node = "#{script_name}#~##{node.next_node}".strip unless node.next_node.include?("#~#") end if node.label full_label = "#{script_name}#~##{node.label}" raise "Manually specified label #{full_label} already exists!" if nodes.has_key?(full_label) else begin full_label = "#{script_name}#~##{node.class.to_s.gsub("GuidedPath::","")}~#{rand(1000000)}" end while nodes.has_key?(full_label) end node.label = full_label @nodes[full_label] = node if @starting_node.nil? && @starting_script == script_name @starting_node = node end return node end
to_hash()
click to toggle source
# File lib/guided_path/program.rb, line 49 def to_hash node_hash = {} @nodes.each_pair { |label, node| node_hash[label] = node.to_hash } output = {:starting_node => nil } output[:starting_node] = @starting_node.label if @starting_node output[:nodes] = node_hash return output end
Private Instance Methods
parse_scripts()
click to toggle source
# File lib/guided_path/program.rb, line 74 def parse_scripts @nodes = {} @starting_node = nil # parse the starting script first @scripts.each do |script_name, script| @parser.new(program: self, script_name: script_name, script: script) end end
postprocess_scripts()
click to toggle source
# File lib/guided_path/program.rb, line 62 def postprocess_scripts # check for label based gotos and change to full nodes. @nodes.each do |label, node| if node.next_node.kind_of?(String) next_node = @nodes[node.next_node] node.next_node = next_node end end end