Class: Element
- Inherits:
-
Object
- Object
- 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
Returns a new instance of Element.
4 5 6 7 |
# File 'lib/ruby_html/elements/element.rb', line 4 def initialize(*elements, attributes: {}) @elements = elements @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
2 3 4 |
# File 'lib/ruby_html/elements/element.rb', line 2 def attributes @attributes end |
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
2 3 4 |
# File 'lib/ruby_html/elements/element.rb', line 2 def elements @elements end |
Instance Method Details
#add_attribute(attribute_hash) ⇒ Object
15 16 17 18 |
# File 'lib/ruby_html/elements/element.rb', line 15 def add_attribute(attribute_hash) @attributes.merge!(attribute_hash) ## TODO: Account for adding elements that already exist. end |
#add_elements(*new_elements) ⇒ Object
9 10 11 12 13 |
# File 'lib/ruby_html/elements/element.rb', line 9 def add_elements(*new_elements) new_elements.each do |element| @elements.push(element) end end |
#closing_tag ⇒ Object
46 47 48 |
# File 'lib/ruby_html/elements/element.rb', line 46 def closing_tag "</#{tag}>" end |
#opening_tag ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/ruby_html/elements/element.rb', line 38 def opening_tag if rendered_attributes != "" "<#{tag} #{rendered_attributes}>" else "<#{tag}>" end end |
#render ⇒ Object
20 21 22 |
# File 'lib/ruby_html/elements/element.rb', line 20 def render opening_tag + rendered_elements + closing_tag end |
#rendered_attributes ⇒ Object
50 51 52 53 54 |
# File 'lib/ruby_html/elements/element.rb', line 50 def rendered_attributes attributes.map do |k, v| "#{k}='#{v}'" end.join(" ") end |
#rendered_elements ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/ruby_html/elements/element.rb', line 24 def rendered_elements elements.map do |element| if element.is_a? String element else element.render end end.join("") end |
#tag ⇒ Object
34 35 36 |
# File 'lib/ruby_html/elements/element.rb', line 34 def tag self.class.name.downcase end |