Class: Charming::Presentation::Components::Form::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/presentation/components/form/builder.rb

Overview

Builder collects form field declarations inside a ‘form(:name) { … }` block and assembles them into a Form component when `build` is called. Each declaration method appends a Field subclass instance to the builder’s fields list.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme: nil) ⇒ Builder

Initializes an empty builder. theme is forwarded to every declared field unless the field declaration explicitly overrides it.



16
17
18
19
# File 'lib/charming/presentation/components/form/builder.rb', line 16

def initialize(theme: nil)
  @theme = theme
  @fields = []
end

Instance Attribute Details

#fieldsObject (readonly)

The accumulated field list and the theme applied to each declared field.



12
13
14
# File 'lib/charming/presentation/components/form/builder.rb', line 12

def fields
  @fields
end

#themeObject (readonly)

The accumulated field list and the theme applied to each declared field.



12
13
14
# File 'lib/charming/presentation/components/form/builder.rb', line 12

def theme
  @theme
end

Instance Method Details

#build(state:, theme: nil) ⇒ Object

Assembles the collected fields into a Form component, bound to state and using the theme argument (falling back to the builder’s theme).



48
49
50
# File 'lib/charming/presentation/components/form/builder.rb', line 48

def build(state:, theme: nil)
  Components::Form.new(fields: fields, state: state, theme: theme || self.theme)
end

#confirm(name, **options) ⇒ Object

Appends a Confirm (boolean) field.



37
38
39
# File 'lib/charming/presentation/components/form/builder.rb', line 37

def confirm(name, **options)
  fields << Confirm.new(name, **field_options(options))
end

#input(name, **options) ⇒ Object

Appends a single-line Input field. options are passed through to Input.



22
23
24
# File 'lib/charming/presentation/components/form/builder.rb', line 22

def input(name, **options)
  fields << Input.new(name, **field_options(options))
end

#note(text, **options) ⇒ Object

Appends a static Note (non-focusable).



42
43
44
# File 'lib/charming/presentation/components/form/builder.rb', line 42

def note(text, **options)
  fields << Note.new(text, **field_options(options))
end

#select(name, **options) ⇒ Object

Appends a Select field with the given options array.



32
33
34
# File 'lib/charming/presentation/components/form/builder.rb', line 32

def select(name, **options)
  fields << Select.new(name, **field_options(options))
end

#textarea(name, **options) ⇒ Object

Appends a multi-line Textarea field.



27
28
29
# File 'lib/charming/presentation/components/form/builder.rb', line 27

def textarea(name, **options)
  fields << Textarea.new(name, **field_options(options))
end