Class: ElementFactory::Element

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes_or_string = nil) ⇒ Element

Returns a new instance of Element.



5
6
7
8
9
10
11
12
# File 'lib/element_factory/element.rb', line 5

def initialize(name, attributes_or_string=nil)
  @name = name.to_s
  @attributes = coerce_attributes(attributes_or_string)

  if attributes[:text].is_a?(String)
    add_child Elements::TextElement.new(attributes[:text])
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/element_factory/element.rb', line 3

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/element_factory/element.rb', line 3

def name
  @name
end

Instance Method Details

#add_child(child) ⇒ Object Also known as: <<



23
24
25
# File 'lib/element_factory/element.rb', line 23

def add_child(child)
  children << child
end

#add_children(children) ⇒ Object



28
29
30
# File 'lib/element_factory/element.rb', line 28

def add_children(children)
  children.each {|child| add_child(child) }
end

#childrenObject



32
33
34
# File 'lib/element_factory/element.rb', line 32

def children
  @children ||= []
end

#html_attributesObject



19
20
21
# File 'lib/element_factory/element.rb', line 19

def html_attributes
  HtmlAttributes.new(self.attributes.dup)
end

#tag_endObject



41
42
43
# File 'lib/element_factory/element.rb', line 41

def tag_end
  %|</%s>| % name
end

#tag_middleObject



45
46
47
# File 'lib/element_factory/element.rb', line 45

def tag_middle
  children.map(&:to_html).join
end

#tag_startObject



36
37
38
39
# File 'lib/element_factory/element.rb', line 36

def tag_start
  tag_attributes = attributes.any?? " #{html_attributes}" : ""
  %|<%s%s>| % [name, tag_attributes]
end

#to_htmlObject Also known as: to_s



14
15
16
# File 'lib/element_factory/element.rb', line 14

def to_html
  tag_start + tag_middle + tag_end
end