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"



14
15
16
17
# File 'lib/objective_elements/single_tag.rb', line 14

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:



43
44
45
46
47
48
49
# File 'lib/objective_elements/single_tag.rb', line 43

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.



7
8
9
# File 'lib/objective_elements/single_tag.rb', line 7

def attributes
  @attributes
end

#elementObject

Returns the value of attribute element.



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

def element
  @element
end

Instance Method Details

#add_parent(parent) ⇒ Object

Returns parent, with self added as a child



29
30
31
# File 'lib/objective_elements/single_tag.rb', line 29

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

#reset_attributesObject

Deletes all current attributes, overwrites them with supplied hash.



24
25
26
# File 'lib/objective_elements/single_tag.rb', line 24

def reset_attributes
  @attributes = HTMLAttributes.new
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/objective_elements/single_tag.rb', line 51

def respond_to_missing?(method, include_private = false)
  @attributes.respond_to?(method) || super
end

#to_aObject



33
34
35
# File 'lib/objective_elements/single_tag.rb', line 33

def to_a
  [opening_tag]
end

#to_sObject

Renders our HTML.



38
39
40
# File 'lib/objective_elements/single_tag.rb', line 38

def to_s
  "#{opening_tag}\n"
end