Class: Larynx::Fields::Field
- Inherits:
-
Object
- Object
- Larynx::Fields::Field
- Includes:
- CallbacksWithAsync
- Defined in:
- lib/larynx/fields.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_prompt(options) ⇒ Object
- #call ⇒ Object
-
#callback_complete(callback, result) ⇒ 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
- #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.
55 56 57 58 59 60 61 62 |
# File 'lib/larynx/fields.rb', line 55 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.
52 53 54 |
# File 'lib/larynx/fields.rb', line 52 def app @app end |
#name ⇒ Object (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() .assert_valid_keys(:play, :speak, :phrase, :bargein, :repeats, :interdigit_timeout, :timeout) repeats = .delete(:repeats) || 1 .merge!(.slice(:length, :min_length, :max_length, :interdigit_timeout, :timeout)) @prompt_queue += ([] * repeats) end |
#call ⇒ Object
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 () ([:play, :speak, :phrase] & .keys).first end |
#current_prompt ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/larynx/fields.rb', line 80 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
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_prompt ⇒ Object
92 93 94 95 |
# File 'lib/larynx/fields.rb', line 92 def execute_prompt call.execute current_prompt.command send_next_command end |
#finalize ⇒ Object
156 157 158 159 |
# File 'lib/larynx/fields.rb', line 156 def finalize call.remove_observer self send_next_command end |
#increment_attempts ⇒ Object
97 98 99 |
# File 'lib/larynx/fields.rb', line 97 def increment_attempts @attempt += 1 end |
#invalid_input ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/larynx/fields.rb', line 122 def invalid_input if @attempt < [: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() add_prompt() end |
#reprompt(options) ⇒ Object
68 69 70 71 |
# File 'lib/larynx/fields.rb', line 68 def reprompt() raise 'A reprompt can only be used after a prompt' if @prompt_queue.empty? add_prompt() 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_command ⇒ Object
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 |