Class: ElementFactory::Element
- Inherits:
-
Object
- Object
- ElementFactory::Element
- Defined in:
- lib/element_factory/element.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add_child(child) ⇒ Object (also: #<<)
- #add_children(children) ⇒ Object
- #children ⇒ Object
- #html_attributes ⇒ Object
-
#initialize(name, attributes_or_string = nil) ⇒ Element
constructor
A new instance of Element.
- #tag_end ⇒ Object
- #tag_middle ⇒ Object
- #tag_start ⇒ Object
- #to_html ⇒ Object (also: #to_s)
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
#attributes ⇒ Object
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/element_factory/element.rb', line 3 def attributes @attributes end |
#name ⇒ Object
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 |
#children ⇒ Object
32 33 34 |
# File 'lib/element_factory/element.rb', line 32 def children @children ||= [] end |
#html_attributes ⇒ Object
19 20 21 |
# File 'lib/element_factory/element.rb', line 19 def html_attributes HtmlAttributes.new(self.attributes.dup) end |
#tag_end ⇒ Object
41 42 43 |
# File 'lib/element_factory/element.rb', line 41 def tag_end %|</%s>| % name end |
#tag_middle ⇒ Object
45 46 47 |
# File 'lib/element_factory/element.rb', line 45 def tag_middle children.map(&:to_html).join end |
#tag_start ⇒ Object
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_html ⇒ Object 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 |