Class: Colloquy::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/colloquy/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier = :index, options = {}, &block) ⇒ Node

Returns a new instance of Node.



5
6
7
8
9
10
11
# File 'lib/colloquy/node.rb', line 5

def initialize(identifier = :index, options = {}, &block)
  @identifier = identifier.to_sym
  @flow = options[:flow] || nil
  @options = options.delete(:flow)
  
  instance_eval &block if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/colloquy/node.rb', line 39

def method_missing(method, *arguments, &block)
  if @flow and @flow.respond_to? method
    @flow.send(method, *arguments, &block) 
  else
    raise NoMethodError, "#{method} is missing in the flow"
  end
end

Instance Attribute Details

#flow=(value) ⇒ Object (writeonly)

Sets the attribute flow

Parameters:

  • value

    the value to set the attribute flow to.



3
4
5
# File 'lib/colloquy/node.rb', line 3

def flow=(value)
  @flow = value
end

#identifierObject (readonly)

Returns the value of attribute identifier.



2
3
4
# File 'lib/colloquy/node.rb', line 2

def identifier
  @identifier
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/colloquy/node.rb', line 2

def options
  @options
end

Instance Method Details



25
26
27
# File 'lib/colloquy/node.rb', line 25

def menu
  @menu ||= Colloquy::Menu.new(:flow => @flow)
end

Returns:

  • (Boolean)


21
22
23
# File 'lib/colloquy/node.rb', line 21

def menu?
  @menu && !@menu.empty?
end

#process(&block) ⇒ Object



55
56
57
# File 'lib/colloquy/node.rb', line 55

def process(&block)
  @process_block = block 
end

#process!(input = nil) ⇒ Object



59
60
61
# File 'lib/colloquy/node.rb', line 59

def process!(input = nil)
  @process_block.call(input) if @process_block
end

#prompt(message) ⇒ Object



13
14
15
# File 'lib/colloquy/node.rb', line 13

def prompt(message)
  @prompt = Colloquy::Prompt.new(:message => message, :flow => @flow)
end

#prompt?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/colloquy/node.rb', line 17

def prompt?
  @prompt && !@prompt.blank?
end

#renderObject



29
30
31
32
33
34
35
36
37
# File 'lib/colloquy/node.rb', line 29

def render
  if @prompt
    @prompt.render
  elsif !menu.empty?
    menu.render
  else
    ""
  end.strip
end

#request(&block) ⇒ Object



47
48
49
# File 'lib/colloquy/node.rb', line 47

def request(&block)
  @request_block = block 
end

#request!(input = nil) ⇒ Object



51
52
53
# File 'lib/colloquy/node.rb', line 51

def request!(input = nil)
  @request_block.call(input) if @request_block
end

#reset!Object



63
64
65
66
# File 'lib/colloquy/node.rb', line 63

def reset!
  @prompt = nil
  menu.reset!
end