Module: Formalism::Form::Fields::ClassMethods

Defined in:
lib/formalism/form/fields.rb

Overview

Module for class methods

Instance Method Summary collapse

Instance Method Details

#field(name, type = nil, **options) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/formalism/form/fields.rb', line 31

def field(name, type = nil, **options)
  Coercion.new(type, options[:of]).check unless type.nil?

  fields_and_nested_forms[name] = options.merge(type: type)

  define_field_methods(name)
end

#fields_and_nested_formsObject



27
28
29
# File 'lib/formalism/form/fields.rb', line 27

def fields_and_nested_forms
  @fields_and_nested_forms ||= {}
end

#included(something) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/formalism/form/fields.rb', line 15

def included(something)
  super

  fields_and_nested_forms.each do |name, options|
    if options.key?(:form)
      something.nested name, options[:form], **options
    else
      something.field name, options[:type], **options
    end
  end
end

#nested(name, form = nil, **options) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/formalism/form/fields.rb', line 39

def nested(name, form = nil, **options)
  unless form || options.key?(:initialize)
    raise ArgumentError, 'Neither form class nor initialize block is not present'
  end

  fields_and_nested_forms[name] = options.merge(form: form)

  define_nested_form_methods(name)
end