Class: Base::ContainerElement
Overview
A container element, like Element but can have children.
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Attributes inherited from Element
#attributes, #end_element, #start_element, #string_name
Instance Method Summary collapse
-
#append(item) ⇒ Object
Append an element to this element.
-
#get_children ⇒ Object
Returns this elements children.
-
#has_children? ⇒ Boolean
Does this element have any child elements?.
-
#initialize(string_name, allowed_attrs = [], allowed_children = [], attrs = {}) ⇒ ContainerElement
constructor
Create a new ContainerElement, takes the string name e.g “div” of an element, an optional array of allowed atrributes, an optional array of allowed children, and an optional hash of attributes as parameters.
-
#is_child_allowed?(child_type) ⇒ Boolean
Checks to see if a certan child element is allowed for this element takes the string_name of a element as parameter.
-
#to_html ⇒ Object
Returns the string representation of this element.
Methods inherited from Element
#attribute_set?, #get_attribute, #is_attribute_allowed?, #set_attribute
Constructor Details
#initialize(string_name, allowed_attrs = [], allowed_children = [], attrs = {}) ⇒ ContainerElement
Create a new ContainerElement, takes the string name e.g “div” of an element, an optional array of allowed atrributes, an optional array of allowed children, and an optional hash of attributes as parameters.
81 82 83 84 85 86 |
# File 'lib/reparcs/base.rb', line 81 def initialize(string_name, allowed_attrs=[], allowed_children=[], attrs={}) super(string_name, allowed_attrs, attrs) @children = [] @allowed_child_elements = allowed_children @allowed_child_elements << "string" end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
135 136 137 |
# File 'lib/reparcs/base.rb', line 135 def children @children end |
Instance Method Details
#append(item) ⇒ Object
Append an element to this element
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/reparcs/base.rb', line 100 def append(item) if item.class == String item = StringElement.new(item) end if is_child_allowed?(item.string_name) @children << item else raise "'#{@string_name}' element's can not contain a '#{item.string_name}' child elements." end end |
#get_children ⇒ Object
Returns this elements children
132 133 134 |
# File 'lib/reparcs/base.rb', line 132 def get_children return @children end |
#has_children? ⇒ Boolean
Does this element have any child elements?
124 125 126 127 128 129 130 |
# File 'lib/reparcs/base.rb', line 124 def has_children? if @children.length > 0 return true else return false end end |
#is_child_allowed?(child_type) ⇒ Boolean
Checks to see if a certan child element is allowed for this element takes the string_name of a element as parameter
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/reparcs/base.rb', line 89 def is_child_allowed?(child_type) allowed = false @allowed_child_elements.each do |e| if e == child_type.to_s allowed = true break end end return allowed end |
#to_html ⇒ Object
Returns the string representation of this element
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/reparcs/base.rb', line 111 def to_html html = "#{@start_element}" @attributes.each do |key, attri| html << " #{key}=\"#{attri}\"" end html << ">" @children.each do |child| html << child.to_html end html << @end_element return html end |