Class: Element

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_html/elements/element.rb

Direct Known Subclasses

Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*elements, attributes: {}) ⇒ Element

Returns a new instance of Element.



4
5
6
7
# File 'lib/ruby_html/elements/element.rb', line 4

def initialize(*elements, attributes: {})
	@elements = elements
	@attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



2
3
4
# File 'lib/ruby_html/elements/element.rb', line 2

def attributes
  @attributes
end

#elementsObject (readonly)

Returns the value of attribute elements.



2
3
4
# File 'lib/ruby_html/elements/element.rb', line 2

def elements
  @elements
end

Instance Method Details

#add_attribute(attribute_hash) ⇒ Object



15
16
17
18
# File 'lib/ruby_html/elements/element.rb', line 15

def add_attribute(attribute_hash)
	@attributes.merge!(attribute_hash)
	## TODO: Account for adding elements that already exist.
end

#add_elements(*new_elements) ⇒ Object



9
10
11
12
13
# File 'lib/ruby_html/elements/element.rb', line 9

def add_elements(*new_elements)
	new_elements.each do |element|
		@elements.push(element)
	end
end

#closing_tagObject



46
47
48
# File 'lib/ruby_html/elements/element.rb', line 46

def closing_tag
	"</#{tag}>"
end

#opening_tagObject



38
39
40
41
42
43
44
# File 'lib/ruby_html/elements/element.rb', line 38

def opening_tag
	if rendered_attributes != ""
		"<#{tag} #{rendered_attributes}>"
	else
		"<#{tag}>"
	end
end

#renderObject



20
21
22
# File 'lib/ruby_html/elements/element.rb', line 20

def render
	 opening_tag + rendered_elements + closing_tag
end

#rendered_attributesObject



50
51
52
53
54
# File 'lib/ruby_html/elements/element.rb', line 50

def rendered_attributes
	attributes.map do |k, v|
		"#{k}='#{v}'"
	end.join(" ")
end

#rendered_elementsObject



24
25
26
27
28
29
30
31
32
# File 'lib/ruby_html/elements/element.rb', line 24

def rendered_elements
	elements.map do |element|
		if element.is_a? String
			element
		else
			element.render
		end
	end.join("")
end

#tagObject



34
35
36
# File 'lib/ruby_html/elements/element.rb', line 34

def tag
	self.class.name.downcase
end