Class: HTML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_html/elements/element.rb

Direct Known Subclasses

Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*elements, attributes: {}) ⇒ Element



5
6
7
8
# File 'lib/ruby_html/elements/element.rb', line 5

def initialize(*elements, attributes: {})
  @elements = elements
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/ruby_html/elements/element.rb', line 3

def attributes
  @attributes
end

#elementsObject (readonly)

Returns the value of attribute elements.



3
4
5
# File 'lib/ruby_html/elements/element.rb', line 3

def elements
  @elements
end

Instance Method Details

#add_attribute(attribute_hash) ⇒ Object



16
17
18
19
# File 'lib/ruby_html/elements/element.rb', line 16

def add_attribute(attribute_hash)
  @attributes.merge!(attribute_hash)
  ## TODO: Account for adding elements that already exist.
end

#add_elements(*new_elements) ⇒ Object



10
11
12
13
14
# File 'lib/ruby_html/elements/element.rb', line 10

def add_elements(*new_elements)
  new_elements.each do |element|
    @elements.push(element)
  end
end

#closing_tagObject



47
48
49
# File 'lib/ruby_html/elements/element.rb', line 47

def closing_tag
  "</#{tag}>"
end

#opening_tagObject



39
40
41
42
43
44
45
# File 'lib/ruby_html/elements/element.rb', line 39

def opening_tag
  if rendered_attributes != ""
    "<#{tag} #{rendered_attributes}>"
  else
    "<#{tag}>"
  end
end

#renderObject



21
22
23
# File 'lib/ruby_html/elements/element.rb', line 21

def render
   opening_tag + rendered_elements + closing_tag
end

#rendered_attributesObject



51
52
53
54
55
# File 'lib/ruby_html/elements/element.rb', line 51

def rendered_attributes
  attributes.map do |k, v|
    "#{k}='#{v}'"
  end.join(" ")
end

#rendered_elementsObject



25
26
27
28
29
30
31
32
33
# File 'lib/ruby_html/elements/element.rb', line 25

def rendered_elements
  elements.map do |element|
    if element.is_a? String
      element
    else
      element.render
    end
  end.join("")
end

#tagObject



35
36
37
# File 'lib/ruby_html/elements/element.rb', line 35

def tag
  self.class.to_s.split('::').last.downcase
end