Class: GuidedPath::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/guided_path/program.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Program

Returns a new instance of Program.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



4
5
6
# File 'lib/guided_path/program.rb', line 4

def nodes
  @nodes
end

#parserObject (readonly)

Returns the value of attribute parser.



4
5
6
# File 'lib/guided_path/program.rb', line 4

def parser
  @parser
end

#starting_nodeObject

Returns the value of attribute starting_node.



5
6
7
# File 'lib/guided_path/program.rb', line 5

def starting_node
  @starting_node
end

Instance Method Details

#add_node(options) ⇒ Object



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
# 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_hashObject



49
50
51
52
53
54
55
56
57
58
# 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