Class: HTML::Element
- Inherits:
-
Object
- Object
- HTML::Element
- Defined in:
- lib/ruby_html/elements/element.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Instance Method Summary collapse
- #add_attribute(attribute_hash) ⇒ Object
- #add_elements(*new_elements) ⇒ Object
- #closing_tag ⇒ Object
-
#initialize(*elements, attributes: {}) ⇒ Element
constructor
A new instance of Element.
- #opening_tag ⇒ Object
- #render ⇒ Object
- #rendered_attributes ⇒ Object
- #rendered_elements ⇒ Object
- #tag ⇒ Object
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/ruby_html/elements/element.rb', line 3 def attributes @attributes end |
#elements ⇒ Object (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_tag ⇒ Object
47 48 49 |
# File 'lib/ruby_html/elements/element.rb', line 47 def closing_tag "</#{tag}>" end |
#opening_tag ⇒ Object
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 |
#render ⇒ Object
21 22 23 |
# File 'lib/ruby_html/elements/element.rb', line 21 def render opening_tag + rendered_elements + closing_tag end |
#rendered_attributes ⇒ Object
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_elements ⇒ Object
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 |
#tag ⇒ Object
35 36 37 |
# File 'lib/ruby_html/elements/element.rb', line 35 def tag self.class.to_s.split('::').last.downcase end |