Class: Ippon::Form::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ippon/form.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_builder:) ⇒ FormBuilder

Returns a new instance of FormBuilder.



107
108
109
110
111
# File 'lib/ippon/form.rb', line 107

def initialize(schema_builder:)
  @schema_builder = schema_builder
  @pre_steps = []
  @post_steps = []
end

Instance Method Details

#nested(name, &blk) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
# File 'lib/ippon/form.rb', line 123

def nested(name, &blk)
  raise ArgumentError, "block required" if !blk

  @pre_steps << proc { |field, form_processor|
    form_processor.with_nested(name) do
      form_processor.instance_exec(field.hash(name), &blk)
    end
  }
end

#output(&blk) ⇒ Object

Raises:

  • (ArgumentError)


133
134
135
136
137
138
139
140
141
# File 'lib/ippon/form.rb', line 133

def output(&blk)
  raise ArgumentError, "block required" if !blk

  schema = blk.call(@schema_builder)
  @post_steps << proc { |field, form_processor|
    form_processor.validate_output(field, schema)
  }
  self
end

#process(field, form_processor) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ippon/form.rb', line 143

def process(field, form_processor)
  @pre_steps.each do |step|
    step.call(field, form_processor)
  end

  return if !form_processor.valid?

  field.fill_output_from_children

  @post_steps.each do |step|
    step.call(field, form_processor)
  end
end