Class: Larynx::Form

Inherits:
Application show all
Defined in:
lib/larynx/form.rb

Instance Attribute Summary collapse

Attributes inherited from Application

#call

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Application

#log, run

Constructor Details

#initialize(*args, &block) ⇒ Form

Returns a new instance of Form.



21
22
23
24
25
# File 'lib/larynx/form.rb', line 21

def initialize(*args, &block)
  @fields = self.class.field_definitions.map {|field| Field.new(field[:name], field[:options], &field[:block]) }
  @field_index = -1
  super
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



8
9
10
# File 'lib/larynx/form.rb', line 8

def fields
  @fields
end

Class Method Details

.field(name, options = {}, &block) ⇒ Object



15
16
17
18
# File 'lib/larynx/form.rb', line 15

def field(name, options={}, &block)
  self.field_definitions <<  {:name => name, :options => options, :block => block}
  attr_accessor name
end

.setup(&block) ⇒ Object



11
12
13
# File 'lib/larynx/form.rb', line 11

def setup(&block)
  self.setup_block = block
end

Instance Method Details

#attemptObject



58
59
60
# File 'lib/larynx/form.rb', line 58

def attempt
  current_field.attempt
end

#current_fieldObject



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

def current_field
  @fields[@field_index]
end

#field_index(name) ⇒ Object



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

def field_index(name)
  field = @fields.find {|f| f.name == name }
  @fields.index(field)
end

#last_attempt?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/larynx/form.rb', line 62

def last_attempt?
  current_field.last_attempt?
end

#next_field(field_name = nil) ⇒ Object



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

def next_field(field_name=nil)
  if field_name
    @field_index = field_index(field_name)
  else
    @field_index += 1
  end
  if field = current_field
    field.run(self)
    field
  end
end

#restart_formObject



32
33
34
35
# File 'lib/larynx/form.rb', line 32

def restart_form
  @field_index = -1
  run
end

#runObject



27
28
29
30
# File 'lib/larynx/form.rb', line 27

def run
  instance_eval &self.class.setup_block if self.class.setup_block
  next_field
end