Class: HexletCode::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/hexlet_code/form_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) {|_self| ... } ⇒ FormBuilder

Returns a new instance of FormBuilder.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
# File 'lib/hexlet_code/form_builder.rb', line 12

def initialize(data)
  @data = data
  @fields = []

  yield self if block_given?
end

Instance Method Details

#buildObject



40
41
42
# File 'lib/hexlet_code/form_builder.rb', line 40

def build
  @fields
end

#input(name, **options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/hexlet_code/form_builder.rb', line 23

def input(name, **options)
  input_type = options[:as] || :string
  check_errors(name)
  clean_attributes = { name:, value: @data[name] }.merge(options.except(:as))
  class_name = "HexletCode::FormTags::#{input_type.capitalize}Tag"
  tag = Object.const_get(class_name).new(clean_attributes)

  @fields.push(label(name), tag)
  tag
end

#label(name) ⇒ Object



19
20
21
# File 'lib/hexlet_code/form_builder.rb', line 19

def label(name)
  FormTags::LabelTag.new(name:)
end

#submit(value = 'Save') ⇒ Object



34
35
36
37
38
# File 'lib/hexlet_code/form_builder.rb', line 34

def submit(value = 'Save')
  tag = FormTags::SubmitTag.new(value:)
  @fields << tag
  tag
end