class SingleTag

Collection of HTML element tags Describes a basic, self-closing HTML tag.

Attributes

attributes[R]
element[RW]

Public Class Methods

new(element, attributes: nil) click to toggle source

Attributes are a hash. Keys are symbols, values are arrays. Will render as key=“value1 value2 value3”

# File lib/objective_elements/single_tag.rb, line 12
def initialize(element, attributes: nil)
  @element = element
  self.attributes = attributes
end

Public Instance Methods

add_parent(parent) click to toggle source

Returns parent, with self added as a child

# File lib/objective_elements/single_tag.rb, line 27
def add_parent(parent)
  parent.add_content(self)
end
attributes=(new) click to toggle source
# File lib/objective_elements/single_tag.rb, line 17
def attributes=(new)
  @attributes = HTMLAttributes.new(new)
end
method_missing(method, arg = nil) click to toggle source

Allows us to work with attributes as methods:

Calls superclass method
# File lib/objective_elements/single_tag.rb, line 41
def method_missing(method, arg = nil)
  if @attributes.respond_to?(method)
    @attributes.send(method, arg)
  else
    super
  end
end
reset_attributes() click to toggle source

Deletes all current attributes, overwrites them with supplied hash.

# File lib/objective_elements/single_tag.rb, line 22
def reset_attributes
  @attributes = HTMLAttributes.new
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/objective_elements/single_tag.rb, line 49
def respond_to_missing?(method, include_private = false)
  @attributes.respond_to?(method) || super
end
to_a() click to toggle source
# File lib/objective_elements/single_tag.rb, line 31
def to_a
  [opening_tag]
end
to_s() click to toggle source

Renders our HTML.

# File lib/objective_elements/single_tag.rb, line 36
def to_s
  opening_tag + "\n"
end

Private Instance Methods

opening_tag() click to toggle source
# File lib/objective_elements/single_tag.rb, line 55
def opening_tag
  output =  '<' + @element
  output << ' ' + @attributes.to_s unless @attributes.empty?
  output << '>'
end