Class: LazyForm::Tag

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

Constant Summary collapse

BOOLEAN_ATTRIBUTES =
%i(autofocus checked disabled readonly required)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}, &block) ⇒ Tag

Returns a new instance of Tag.



29
30
31
32
33
# File 'lib/lazy_form.rb', line 29

def initialize(name, attributes = {}, &block)
  @name = name
  @attributes = attributes
  @block = block
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



25
26
27
# File 'lib/lazy_form.rb', line 25

def attributes
  @attributes
end

#blockObject (readonly)

Returns the value of attribute block.



25
26
27
# File 'lib/lazy_form.rb', line 25

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/lazy_form.rb', line 25

def name
  @name
end

Instance Method Details

#to_sObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/lazy_form.rb', line 35

def to_s
  attrs = build_attributes attributes.reject { |k, v| v.nil? }
  attrs.unshift name

  if block.nil?
    "<#{attrs.reject(&:empty?).join ' '}/>"
  else
    "<#{attrs.reject(&:empty?).join ' '}>#{block.call}</#{name}>"
  end
end