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
  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

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#elementObject

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_attributesObject

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

Returns:

  • (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_aObject



31
32
33
# File 'lib/objective_elements/single_tag.rb', line 31

def to_a
  [opening_tag]
end

#to_sObject

Renders our HTML.



36
37
38
# File 'lib/objective_elements/single_tag.rb', line 36

def to_s
  opening_tag + "\n"
end