Class: Larynx::Field
- Inherits:
-
Object
- Object
- Larynx::Field
- 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
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#attempt ⇒ Object
readonly
Returns the value of attribute attempt.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_prompt(options) ⇒ Object
- #call ⇒ Object
-
#callback_complete(callback, result = true) ⇒ Object
hook called when callback is complete.
- #command_from_options(options) ⇒ Object
- #current_prompt ⇒ Object
- #evaluate_input ⇒ Object
- #evaluate_validity(result) ⇒ Object
- #execute_prompt ⇒ Object
- #finalize ⇒ Object
- #increment_attempts ⇒ Object
-
#initialize(name, options, &block) ⇒ Field
constructor
A new instance of Field.
- #invalid_input ⇒ Object
- #last_attempt? ⇒ Boolean
- #prompt(options) ⇒ Object
- #reprompt(options) ⇒ Object
- #run(app) ⇒ Object
- #send_next_command ⇒ Object
- #set_instance_variables(input, result) ⇒ Object
Methods included from CallbacksWithAsync
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, , &block) @name = name = .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
#app ⇒ Object (readonly)
Returns the value of attribute app.
9 10 11 |
# File 'lib/larynx/field.rb', line 9 def app @app end |
#attempt ⇒ Object (readonly)
Returns the value of attribute attempt.
9 10 11 |
# File 'lib/larynx/field.rb', line 9 def attempt @attempt end |
#name ⇒ Object (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() .assert_valid_keys(*VALID_PROMPT_OPTIONS) repeats = .delete(:repeats) || 1 .merge!(.slice(:length, :min_length, :max_length, :interdigit_timeout, :timeout)) @prompt_queue += ([] * repeats) end |
#call ⇒ Object
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 () (Prompt::COMMAND_OPTIONS & .keys).first end |
#current_prompt ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/larynx/field.rb', line 37 def current_prompt = (@prompt_queue[@attempt-1] || @prompt_queue.last).dup method = () = [method].is_a?(Symbol) ? @app.send([method]) : [method] [method] = Prompt.new(call, ) {|input, result| set_instance_variables(input, result) evaluate_input } end |
#evaluate_input ⇒ Object
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_prompt ⇒ Object
49 50 51 52 |
# File 'lib/larynx/field.rb', line 49 def execute_prompt call.execute current_prompt.command send_next_command end |
#finalize ⇒ Object
112 113 114 115 |
# File 'lib/larynx/field.rb', line 112 def finalize call.remove_observer self send_next_command end |
#increment_attempts ⇒ Object
54 55 56 |
# File 'lib/larynx/field.rb', line 54 def increment_attempts @attempt += 1 end |
#invalid_input ⇒ Object
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
117 118 119 |
# File 'lib/larynx/field.rb', line 117 def last_attempt? @attempt == [:attempts] end |
#prompt(options) ⇒ Object
21 22 23 |
# File 'lib/larynx/field.rb', line 21 def prompt() add_prompt() end |
#reprompt(options) ⇒ Object
25 26 27 28 |
# File 'lib/larynx/field.rb', line 25 def reprompt() raise 'A reprompt can only be used after a prompt' if @prompt_queue.empty? add_prompt() 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_command ⇒ Object
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 |