Class: Rind::Element
- Inherits:
-
Object
- Object
- Rind::Element
- Includes:
- Equality, Manipulate, Traverse, Xpath
- Defined in:
- lib/rind/nodes.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#local_name ⇒ Object
(also: #name)
readonly
Returns the value of attribute local_name.
-
#namespace_name ⇒ Object
(also: #namespace)
readonly
Returns the value of attribute namespace_name.
Attributes included from Traverse
Instance Method Summary collapse
-
#[](key = nil) ⇒ Object
Get attributes or an attribute values.
-
#[]=(key, value) ⇒ Object
Set the value of an attribute.
-
#expanded_name ⇒ Object
Get the full name of the Element.
-
#initialize(options = {}) ⇒ Element
constructor
Options * attributes = hash or string * children = array of nodes === Examples Rind::Element.new( :attributes => { :id => “first”, :class => “second” }, :children => [Rind::Element.new(), Rind::Element.new()] ) Rind::Element.new( :attributes => ‘id=“first” class=“second”’, :children => “Hello World!” ).
-
#render! ⇒ Object
Renders the node in place.
- #to_s ⇒ Object
Methods included from Xpath
Methods included from Traverse
#ancestors, #descendants, #down, #next, #next_siblings, #prev, #prev_siblings, #siblings, #up
Methods included from Manipulate
#insert_after, #insert_before, #remove
Methods included from Equality
Constructor Details
#initialize(options = {}) ⇒ Element
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rind/nodes.rb', line 108 def initialize(={}) self.class.to_s =~ /^(?:([\w:]+)::)?(\w+)$/ @namespace_name, @local_name = $1, $2.downcase @namespace_name.downcase!.gsub!(/::/, ':') if not @namespace_name.nil? @namespace_name = [:namespace_name] if [:namespace_name] @attributes = Hash.new if [:attributes].is_a? Hash [:attributes].each do |k,v| @attributes[k.to_s] = v end elsif [:attributes].is_a? String # scan for attr=value|"value1 'value2'"|'"value1" value2' or attr by itself [:attributes].scan(/([\w-]+)(?:=(\w+|"[^"]+?"|'[^']+?'))?/) do |name, value| @attributes[name] = value.nil? ? '' : value.sub(/\A(["'])([^\1]+)\1\z/, '\2') end end @children = Children.new(self, *[:children]) end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
92 93 94 |
# File 'lib/rind/nodes.rb', line 92 def children @children end |
#local_name ⇒ Object (readonly) Also known as: name
Returns the value of attribute local_name.
92 93 94 |
# File 'lib/rind/nodes.rb', line 92 def local_name @local_name end |
#namespace_name ⇒ Object (readonly) Also known as: namespace
Returns the value of attribute namespace_name.
92 93 94 |
# File 'lib/rind/nodes.rb', line 92 def namespace_name @namespace_name end |
Instance Method Details
#[](key = nil) ⇒ Object
Get attributes or an attribute values.
Examples
e = Rind::Element.new(:attributes => {:id => "id_1", :class => "class_1"})
e[] => {"id"=>"id_1", "class"=>"class_1"}
e[:id] => "id_1"
e['class'] => "class_1"
136 137 138 |
# File 'lib/rind/nodes.rb', line 136 def [](key = nil) key.nil? ? @attributes : @attributes[key.to_s] end |
#[]=(key, value) ⇒ Object
145 146 147 |
# File 'lib/rind/nodes.rb', line 145 def []=(key, value) @attributes[key.to_s] = value end |
#expanded_name ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/rind/nodes.rb', line 155 def if @namespace_name == 'rind' @local_name else [@namespace_name, @local_name].join(':') end end |
#render! ⇒ Object
Renders the node in place. This will recursively call render!
on all child nodes.
165 166 167 168 |
# File 'lib/rind/nodes.rb', line 165 def render! @children.render! if not @children.empty? self end |
#to_s ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rind/nodes.rb', line 170 def to_s attrs = @attributes.collect{|k,v| "#{k}=\"#{v}\""}.join(' ') attrs = " #{attrs}" if not attrs.empty? if @children.empty? "<#{self.}#{attrs} />" else "<#{self.}#{attrs}>#{@children}</#{self.}>" end end |