module Formulario class Field class FormField < Field attr_reader :form def self.[](type) Class.new(self) do |typed_class| typed_class.type = type end end def self.type @type end def value form.params end def exceptional? ! form.valid? end def fields form.fields end def [](value) fields[value] end def initialize(**raw_value) @form = self.class.type.new(**raw_value) super end private def self.build(raw_value) raise UndefinedFormFieldType.new unless @type new(raw_value) end def self.type=(new_type) @type = new_type end def method_missing(name, *args, &block) return fields[name] if fields.has_key?(name) super end UndefinedFormFieldType = Class.new(RuntimeError) end end end