Class: Larynx::Field

Inherits:
Object
  • Object
show all
Includes:
CallbacksWithAsync
Defined in:
lib/larynx/field.rb

Constant Summary collapse

VALID_PROMPT_OPTIONS =
[:play, :speak, :phrase, :bargein, :repeats, :interdigit_timeout, :timeout]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CallbacksWithAsync

included

Constructor Details

#initialize(name, options, &block) ⇒ Field

Returns a new instance of Field.



12
13
14
15
16
17
18
19
# File 'lib/larynx/field.rb', line 12

def initialize(name, options, &block)
  @name = name
  @options = options.reverse_merge(:attempts => 3)
  @prompt_queue = []

  instance_eval(&block)
  raise(Larynx::NoPromptDefined, 'A field requires a prompt to be defined') if @prompt_queue.empty?
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/larynx/field.rb', line 9

def app
  @app
end

#attemptObject (readonly)

Returns the value of attribute attempt.



9
10
11
# File 'lib/larynx/field.rb', line 9

def attempt
  @attempt
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/larynx/field.rb', line 9

def name
  @name
end

Instance Method Details

#add_prompt(options) ⇒ Object



30
31
32
33
34
35
# File 'lib/larynx/field.rb', line 30

def add_prompt(options)
  options.assert_valid_keys(*VALID_PROMPT_OPTIONS)
  repeats = options.delete(:repeats) || 1
  options.merge!(@options.slice(:length, :min_length, :max_length, :interdigit_timeout, :timeout))
  @prompt_queue += ([options] * repeats)
end

#callObject



108
109
110
# File 'lib/larynx/field.rb', line 108

def call
  @app.call
end

#callback_complete(callback, result = true) ⇒ Object

hook called when callback is complete



59
60
61
62
63
64
65
66
67
68
# File 'lib/larynx/field.rb', line 59

def callback_complete(callback, result=true)
  case callback
  when :validate
    evaluate_validity(result)
  when :invalid
    invalid_input
  when :success, :failure
    finalize
  end
end

#command_from_options(options) ⇒ Object



96
97
98
# File 'lib/larynx/field.rb', line 96

def command_from_options(options)
  (Prompt::COMMAND_OPTIONS & options.keys).first
end

#current_promptObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/larynx/field.rb', line 37

def current_prompt
  options = (@prompt_queue[@attempt-1] || @prompt_queue.last).dup
  method  = command_from_options(options)
  message = options[method].is_a?(Symbol) ? @app.send(options[method]) : options[method]
  options[method] = message

  Prompt.new(call, options) {|input, result|
    set_instance_variables(input, result)
    evaluate_input
  }
end

#evaluate_inputObject



70
71
72
# File 'lib/larynx/field.rb', line 70

def evaluate_input
  @valid_length ? fire_callback(:validate) : fire_callback(:invalid)
end

#evaluate_validity(result) ⇒ Object



74
75
76
# File 'lib/larynx/field.rb', line 74

def evaluate_validity(result)
  result ? fire_callback(:success) : fire_callback(:invalid)
end

#execute_promptObject



49
50
51
52
# File 'lib/larynx/field.rb', line 49

def execute_prompt
  call.execute current_prompt.command
  send_next_command
end

#finalizeObject



112
113
114
115
# File 'lib/larynx/field.rb', line 112

def finalize
  call.remove_observer self
  send_next_command
end

#increment_attemptsObject



54
55
56
# File 'lib/larynx/field.rb', line 54

def increment_attempts
  @attempt += 1
end

#invalid_inputObject



78
79
80
81
82
83
84
85
# File 'lib/larynx/field.rb', line 78

def invalid_input
  if last_attempt?
    fire_callback(:failure)
  else
    increment_attempts
    execute_prompt
  end
end

#last_attempt?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/larynx/field.rb', line 117

def last_attempt?
  @attempt == @options[:attempts]
end

#prompt(options) ⇒ Object



21
22
23
# File 'lib/larynx/field.rb', line 21

def prompt(options)
  add_prompt(options)
end

#reprompt(options) ⇒ Object



25
26
27
28
# File 'lib/larynx/field.rb', line 25

def reprompt(options)
  raise 'A reprompt can only be used after a prompt' if @prompt_queue.empty?
  add_prompt(options)
end

#run(app) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/larynx/field.rb', line 100

def run(app)
  @app = app
  @attempt = 1
  call.add_observer self
  fire_callback(:setup)
  execute_prompt
end

#send_next_commandObject



87
88
89
# File 'lib/larynx/field.rb', line 87

def send_next_command
  call.send_next_command if call.state == :ready
end

#set_instance_variables(input, result) ⇒ Object



91
92
93
94
# File 'lib/larynx/field.rb', line 91

def set_instance_variables(input, result)
  @value, @valid_length = input, result
  @app.send("#{@name}=", input)
end