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
readonly
Returns the value of attribute attributes.
-
#element ⇒ Object
Returns the value of attribute element.
Instance Method Summary collapse
-
#add_attributes(new) ⇒ Object
(also: #add_attribute)
This is the only way we add new attributes.
-
#add_parent(parent) ⇒ Object
Returns parent, with self added as a child.
- #delete_attributes(keys) ⇒ Object (also: #delete_attribute)
-
#initialize(element, attributes: nil) ⇒ SingleTag
constructor
Attributes are a hash.
- #opening_tag ⇒ Object
-
#render_attributes ⇒ Object
Turns attributes into a string we can insert.
-
#reset_attributes(new = nil) ⇒ Object
Deletes all current attributes, overwrites them with supplied hash.
- #rewrite_attribute(new) ⇒ Object
- #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 reset_attributes(attributes) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/objective_elements/single_tag.rb', line 4 def attributes @attributes end |
#element ⇒ Object
Returns the value of attribute element.
5 6 7 |
# File 'lib/objective_elements/single_tag.rb', line 5 def element @element end |
Instance Method Details
#add_attributes(new) ⇒ Object Also known as: add_attribute
This is the only way we add new attributes. Flexible about what you give it– accepts both strings and symbols for the keys, and both strings and arrays for the values.
29 30 31 32 33 34 35 36 37 |
# File 'lib/objective_elements/single_tag.rb', line 29 def add_attributes(new) if new.is_a? String add_string_attributes(new) else add_hash_attributes(new) end self end |
#add_parent(parent) ⇒ Object
Returns parent, with self added as a child
76 77 78 |
# File 'lib/objective_elements/single_tag.rb', line 76 def add_parent(parent) parent.add_content(self) end |
#delete_attributes(keys) ⇒ Object Also known as: delete_attribute
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/objective_elements/single_tag.rb', line 40 def delete_attributes(keys) # accepts an array or a single element to_delete = if keys.is_a? Array keys.map(&:to_sym) else [keys.to_sym] end to_delete.each { |k| @attributes.delete k } self end |
#opening_tag ⇒ Object
80 81 82 83 84 |
# File 'lib/objective_elements/single_tag.rb', line 80 def opening_tag output = '<' + @element output << ' ' + render_attributes unless @attributes.empty? output << '>' end |
#render_attributes ⇒ Object
Turns attributes into a string we can insert.
67 68 69 70 71 72 73 |
# File 'lib/objective_elements/single_tag.rb', line 67 def render_attributes attribute_string = '' @attributes.each_pair do |k, v| attribute_string << "#{k}=\"#{v.join ' '}\" " end attribute_string.strip end |
#reset_attributes(new = nil) ⇒ Object
Deletes all current attributes, overwrites them with supplied hash.
18 19 20 21 22 23 |
# File 'lib/objective_elements/single_tag.rb', line 18 def reset_attributes(new = nil) @attributes = {} add_attributes(new) if new self end |
#rewrite_attribute(new) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/objective_elements/single_tag.rb', line 54 def rewrite_attribute(new) formatted_new = if new.is_a? String hashify_attributes(new) else new.transform_keys(&:to_sym) end delete_attributes formatted_new.keys add_hash_attributes formatted_new end |
#to_a ⇒ Object
86 87 88 |
# File 'lib/objective_elements/single_tag.rb', line 86 def to_a [opening_tag] end |
#to_s ⇒ Object
Renders our HTML.
91 92 93 |
# File 'lib/objective_elements/single_tag.rb', line 91 def to_s opening_tag + "\n" end |