Class: Larynx::Fields::Field

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

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.



55
56
57
58
59
60
61
62
# File 'lib/larynx/fields.rb', line 55

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.



52
53
54
# File 'lib/larynx/fields.rb', line 52

def app
  @app
end

#nameObject (readonly)

Returns the value of attribute name.



52
53
54
# File 'lib/larynx/fields.rb', line 52

def name
  @name
end

Instance Method Details

#add_prompt(options) ⇒ Object



73
74
75
76
77
78
# File 'lib/larynx/fields.rb', line 73

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

#callObject



152
153
154
# File 'lib/larynx/fields.rb', line 152

def call
  @app.call
end

#callback_complete(callback, result) ⇒ Object

hook called when callback is complete



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/larynx/fields.rb', line 102

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

#command_from_options(options) ⇒ Object



140
141
142
# File 'lib/larynx/fields.rb', line 140

def command_from_options(options)
  ([:play, :speak, :phrase] & options.keys).first
end

#current_promptObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/larynx/fields.rb', line 80

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



114
115
116
# File 'lib/larynx/fields.rb', line 114

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

#evaluate_validity(result) ⇒ Object



118
119
120
# File 'lib/larynx/fields.rb', line 118

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

#execute_promptObject



92
93
94
95
# File 'lib/larynx/fields.rb', line 92

def execute_prompt
  call.execute current_prompt.command
  send_next_command
end

#finalizeObject



156
157
158
159
# File 'lib/larynx/fields.rb', line 156

def finalize
  call.remove_observer self
  send_next_command
end

#increment_attemptsObject



97
98
99
# File 'lib/larynx/fields.rb', line 97

def increment_attempts
  @attempt += 1
end

#invalid_inputObject



122
123
124
125
126
127
128
129
# File 'lib/larynx/fields.rb', line 122

def invalid_input
  if @attempt < @options[:attempts]
    increment_attempts
    execute_prompt
  else
    fire_callback(:failure)
  end
end

#prompt(options) ⇒ Object



64
65
66
# File 'lib/larynx/fields.rb', line 64

def prompt(options)
  add_prompt(options)
end

#reprompt(options) ⇒ Object



68
69
70
71
# File 'lib/larynx/fields.rb', line 68

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

#run(app) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/larynx/fields.rb', line 144

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

#send_next_commandObject



131
132
133
# File 'lib/larynx/fields.rb', line 131

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

#set_instance_variables(input, result) ⇒ Object



135
136
137
138
# File 'lib/larynx/fields.rb', line 135

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