Class: Charming::Presentation::Components::Form::Builder
- Inherits:
-
Object
- Object
- Charming::Presentation::Components::Form::Builder
- 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
-
#fields ⇒ Object
readonly
The accumulated field list and the theme applied to each declared field.
-
#theme ⇒ Object
readonly
The accumulated field list and the theme applied to each declared field.
Instance Method Summary collapse
-
#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).
-
#confirm(name, **options) ⇒ Object
Appends a Confirm (boolean) field.
-
#initialize(theme: nil) ⇒ Builder
constructor
Initializes an empty builder.
-
#input(name, **options) ⇒ Object
Appends a single-line Input field.
-
#note(text, **options) ⇒ Object
Appends a static Note (non-focusable).
-
#select(name, **options) ⇒ Object
Appends a Select field with the given options array.
-
#textarea(name, **options) ⇒ Object
Appends a multi-line Textarea field.
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
#fields ⇒ Object (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 |
#theme ⇒ Object (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, **) fields << Confirm.new(name, **()) 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, **) fields << Input.new(name, **()) 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, **) fields << Note.new(text, **()) end |