Class: SingleTag
- Inherits:
-
Object
- Object
- SingleTag
- Defined in:
- lib/objective_elements/single_tag.rb
Overview
Collection of HTML element tags Describes a basic, self-closing HTML tag.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#element ⇒ Object
Returns the value of attribute element.
Instance Method Summary collapse
-
#add_parent(parent) ⇒ Object
Returns parent, with self added as a child.
-
#initialize(element, attributes: nil) ⇒ SingleTag
constructor
Attributes are a hash.
-
#method_missing(method, arg = nil) ⇒ Object
Allows us to work with attributes as methods:.
-
#reset_attributes ⇒ Object
Deletes all current attributes, overwrites them with supplied hash.
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
- #to_a ⇒ Object
-
#to_s ⇒ Object
Renders our HTML.
Constructor Details
#initialize(element, attributes: nil) ⇒ SingleTag
Attributes are a hash. Keys are symbols, values are arrays. Will render as key=“value1 value2 value3”
12 13 14 15 |
# File 'lib/objective_elements/single_tag.rb', line 12 def initialize(element, attributes: nil) @element = element self.attributes = attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, arg = nil) ⇒ Object
Allows us to work with attributes as methods:
41 42 43 44 45 46 47 |
# 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 |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/objective_elements/single_tag.rb', line 5 def attributes @attributes end |
#element ⇒ Object
Returns the value of attribute element.
4 5 6 |
# File 'lib/objective_elements/single_tag.rb', line 4 def element @element end |
Instance Method Details
#add_parent(parent) ⇒ Object
Returns parent, with self added as a child
27 28 29 |
# File 'lib/objective_elements/single_tag.rb', line 27 def add_parent(parent) parent.add_content(self) end |
#reset_attributes ⇒ Object
Deletes all current attributes, overwrites them with supplied hash.
22 23 24 |
# File 'lib/objective_elements/single_tag.rb', line 22 def reset_attributes @attributes = HTMLAttributes.new end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
49 50 51 |
# 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 ⇒ Object
31 32 33 |
# File 'lib/objective_elements/single_tag.rb', line 31 def to_a [opening_tag] end |
#to_s ⇒ Object
Renders our HTML.
36 37 38 |
# File 'lib/objective_elements/single_tag.rb', line 36 def to_s opening_tag + "\n" end |