Class: VoiceForm::FormField

Inherits:
Object
  • Object
show all
Defined in:
lib/voice_form/form_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, component, &block) ⇒ FormField

Returns a new instance of FormField.



9
10
11
12
13
14
15
16
17
# File 'lib/voice_form/form_field.rb', line 9

def initialize(name, options, component, &block)
  @name, @options, @component = name, options, component
  @options.reverse_merge!(:attempts => 5, :call => 'call')
  @callbacks = {}
  @prompt_queue = []

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/voice_form/form_field.rb', line 5

def name
  @name
end

Instance Method Details

#confirm(options = {}, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/voice_form/form_field.rb', line 52

def confirm(options={}, &block)
  options.reverse_merge!(
    self.class.default_prompt_options.merge(
      :attempts => 3,
      :accept   => 1,
      :reject   => 2
    )
  )
  @confirmation_options = options.merge(:message => block)
end

#failure(&block) ⇒ Object



48
49
50
# File 'lib/voice_form/form_field.rb', line 48

def failure(&block)
  @callbacks[:failure] = block
end

#invalid(&block) ⇒ Object



36
37
38
# File 'lib/voice_form/form_field.rb', line 36

def invalid(&block)
  @callbacks[:invalid] = block
end

#prompt(options) ⇒ Object



19
20
21
# File 'lib/voice_form/form_field.rb', line 19

def prompt(options)
  add_prompt(options.reverse_merge(self.class.default_prompt_options))
end

#reprompt(options) ⇒ Object



23
24
25
26
# File 'lib/voice_form/form_field.rb', line 23

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

#run(component = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/voice_form/form_field.rb', line 63

def run(component=nil)
  @component = component if component

  run_callback(:setup)

  result = 1.upto(@options[:attempts]) do |attempt|
    @value = get_input(prompt_for_attempt(attempt))

    unless valid_length?
      run_callback(:timeout)
      next
    end

    if input_valid?
      if value_confirmed?
        break 0
      else
        next
      end
    else
      run_callback(:invalid)
    end
  end
  if result == 0
    run_callback(:success)
  else
    run_callback(:failure)
  end
  set_component_value @value
end

#setup(&block) ⇒ Object



28
29
30
# File 'lib/voice_form/form_field.rb', line 28

def setup(&block)
  @callbacks[:setup] = block
end

#success(&block) ⇒ Object



44
45
46
# File 'lib/voice_form/form_field.rb', line 44

def success(&block)
  @callbacks[:success] = block
end

#timeout(&block) ⇒ Object



40
41
42
# File 'lib/voice_form/form_field.rb', line 40

def timeout(&block)
  @callbacks[:timeout] = block
end

#validate(&block) ⇒ Object



32
33
34
# File 'lib/voice_form/form_field.rb', line 32

def validate(&block)
  @callbacks[:validate] = block
end