Class: Formalist::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/formalist/definition.rb

Constant Summary collapse

DuplicateElementError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, config, &block) ⇒ Definition



9
10
11
12
13
14
15
# File 'lib/formalist/definition.rb', line 9

def initialize(form, config, &block)
  @form = form
  @config = config
  @elements = []

  instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/formalist/definition.rb', line 25

def method_missing(name, *args, &block)
  if element_type?(name)
    add_element(name, *args, &block)
  elsif form.respond_to?(name, include_private = true)
    form.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/formalist/definition.rb', line 6

def config
  @config
end

#elementsObject (readonly)

Returns the value of attribute elements.



7
8
9
# File 'lib/formalist/definition.rb', line 7

def elements
  @elements
end

#formObject (readonly)

Returns the value of attribute form.



5
6
7
# File 'lib/formalist/definition.rb', line 5

def form
  @form
end

Instance Method Details

#with(**new_options, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/formalist/definition.rb', line 17

def with(**new_options, &block)
  new_config = new_options.each_with_object(config.dup) { |(key, value), config|
    config.send :"#{key}=", value
  }

  self.class.new(form, new_config, &block)
end