Class: SingleTag

Inherits:
Object
  • Object
show all
Defined in:
lib/objective_elements/single_tag.rb

Overview

Collection of HTML element tags Describes a basic, self-closing HTML tag.

Direct Known Subclasses

DoubleTag

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/objective_elements/single_tag.rb', line 4

def attributes
  @attributes
end

#elementObject

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
38
39
40
# File 'lib/objective_elements/single_tag.rb', line 29

def add_attributes(new)
  # Don't break everything if this is passed an empty value:
  return self unless 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



79
80
81
# File 'lib/objective_elements/single_tag.rb', line 79

def add_parent(parent)
  parent.add_content(self)
end

#delete_attributes(keys) ⇒ Object Also known as: delete_attribute



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/objective_elements/single_tag.rb', line 43

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_tagObject



83
84
85
86
87
# File 'lib/objective_elements/single_tag.rb', line 83

def opening_tag
  output =  '<' + @element
  output << ' ' + render_attributes unless @attributes.empty?
  output << '>'
end

#render_attributesObject

Turns attributes into a string we can insert.



70
71
72
73
74
75
76
# File 'lib/objective_elements/single_tag.rb', line 70

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



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/objective_elements/single_tag.rb', line 57

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_aObject



89
90
91
# File 'lib/objective_elements/single_tag.rb', line 89

def to_a
  [opening_tag]
end

#to_sObject

Renders our HTML.



94
95
96
# File 'lib/objective_elements/single_tag.rb', line 94

def to_s
  opening_tag + "\n"
end