Class: Ippon::Form::Processor

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, key) ⇒ Processor

Returns a new instance of Processor.



176
177
178
179
180
# File 'lib/ippon/form.rb', line 176

def initialize(data, key)
  @data = data
  @key = key
  @is_valid = true
end

Class Attribute Details

.schema_builderObject



159
160
161
# File 'lib/ippon/form.rb', line 159

def self.schema_builder
  @schema_builder ||= Ippon::Validate::Builder
end

Class Method Details

.form(name, &blk) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/ippon/form.rb', line 165

def self.form(name, &blk)
  if method_defined?(name)
    raise ArgumentError, "method already exists: ##{name}"
  end

  form_builder = FormBuilder.new(schema_builder: schema_builder)
  define_method(name) { |field| form_builder.process(field, self) }
  form_builder.instance_eval(&blk) if blk
  form_builder
end

Instance Method Details

#fill(field, from: nil, schema: nil) ⇒ Object



194
195
196
197
198
199
# File 'lib/ippon/form.rb', line 194

def fill(field, from: nil, schema: nil)
  key = from ? @key[from] : @key
  field.fill_from_data(@data, key)
  validate_input(field, schema) if schema
  field
end

#result_to_error(result) ⇒ Object



217
218
219
# File 'lib/ippon/form.rb', line 217

def result_to_error(result)
  result.error_messages
end

#valid?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/ippon/form.rb', line 182

def valid?
  @is_valid
end

#validate(field, schema, value) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/ippon/form.rb', line 201

def validate(field, schema, value)
  result = schema.validate(value)
  field.output = result.value
  field.error = result_to_error(result)
  @is_valid = false if result.error?
  field
end

#validate_input(field, schema) ⇒ Object



209
210
211
# File 'lib/ippon/form.rb', line 209

def validate_input(field, schema)
  validate(field, schema, field.input)
end

#validate_output(field, schema) ⇒ Object



213
214
215
# File 'lib/ippon/form.rb', line 213

def validate_output(field, schema)
  validate(field, schema, field.output)
end

#with_nested(name) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/ippon/form.rb', line 186

def with_nested(name)
  old_key = @key
  @key = @key[name]
  yield
ensure
  @key = old_key
end