Class: Rind::Element

Inherits:
Object
  • Object
show all
Includes:
Equality, Manipulate, Traverse, Xpath
Defined in:
lib/rind/nodes.rb

Instance Attribute Summary collapse

Attributes included from Traverse

#parent

Instance Method Summary collapse

Methods included from Xpath

#s, #sf

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

#==, #eql?

Constructor Details

#initialize(options = {}) ⇒ Element

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!"
)


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(options={})
	self.class.to_s =~ /^(?:([\w:]+)::)?(\w+)$/
	@namespace_name, @local_name = $1, $2.downcase
	@namespace_name.downcase!.gsub!(/::/, ':') if not @namespace_name.nil?

	@namespace_name = options[:namespace_name] if options[:namespace_name]

	@attributes = Hash.new
	if options[:attributes].is_a? Hash
		options[:attributes].each do |k,v|
			@attributes[k.to_s] = v
		end
	elsif options[:attributes].is_a? String
		# scan for attr=value|"value1 'value2'"|'"value1" value2' or attr by itself
		options[:attributes].scan(/([\w-]+)(?:=(\w+|"[^"]+?"|'[^']+?'))?/) do |name, value|
			@attributes[name] = value.nil? ? '' : value.sub(/\A(["'])([^\1]+)\1\z/, '\2')
		end
	end

	@children = Children.new(self, *options[:children])
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



92
93
94
# File 'lib/rind/nodes.rb', line 92

def children
  @children
end

#local_nameObject (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_nameObject (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

Set the value of an attribute.

Examples

e = Rind::Element.new(:attributes => {:id => "id_1", :class => "class_1"})
e['id']   => "id_2"
e[:class] => "class_2"


145
146
147
# File 'lib/rind/nodes.rb', line 145

def []=(key, value)
	@attributes[key.to_s] = value
end

#expanded_nameObject

Get the full name of the Element.

Examples

b = Rind::Html::Br.new()
b.expanded_name => 'br'
cn = Custom::Node.new()
cn.expanded_name => 'custom:node'


155
156
157
158
159
160
161
# File 'lib/rind/nodes.rb', line 155

def expanded_name
	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_sObject



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.expanded_name}#{attrs} />"
	else
		"<#{self.expanded_name}#{attrs}>#{@children}</#{self.expanded_name}>"
	end
end