Proper namespace support for attributes.

This separates namespace handling into namespace names and namespace objects.
The namespace objects are retrieved from the element an attribute belongs to.
Once retrieved the namespace is cached, due to the overhead of retrieving
namespaces in large documents.
This commit is contained in:
Yorick Peterse committed 2014-08-11 00:40:17 +02:00
1 parent fe8f77cf45
commit 04cbbdcf9e
4 files changed
+63 -36

No files matched your search

+23 -14
View File
@@ -7,35 +7,44 @@ module Oga
# The name of the attribute.
# @return [String]
#
# @!attribute [rw] namespace
# The namespace of the attribute.
# @return [Oga::XML::Namespace]
# @!attribute [rw] namespace_name
# @return [String]
#
# @!attribute [rw] value
# The value of the attribute.
# @return [String]
#
# @!attribute [r] element
# The element this attribute belongs to.
# @return [Oga::XML::Element]
#
class Attribute
attr_accessor :name, :namespace, :value
attr_accessor :name, :namespace_name, :element, :value
##
# @param [Hash] options
#
# @option options [String] :name
# @option options [Oga::XML::Namespace] :namespace
# @option options [String] :namespace_name
# @option options [String] :value
# @option options [Oga::XML::Element] :element
#
def initialize(options = {})
if options[:namespace] and !options[:namespace].is_a?(Namespace)
raise(
TypeError,
':namespace must be an instance of Oga::XML::Namespace'
)
end
@name = options[:name]
@value = options[:value]
@element = options[:element]
@name = options[:name]
@namespace = options[:namespace]
@value = options[:value]
@namespace_name = options[:namespace_name]
end
##
# Returns the {Oga::XML::Namespace} instance for the current namespace
# name.
#
# @return [Oga::XML::Namespace]
#
def namespace
return @namespace ||= element.available_namespaces[namespace_name]
end
##
+10 -2
View File
@@ -4,16 +4,24 @@ module Oga
# The Namespace class contains information about XML namespaces such as the
# name and URI.
#
# @!attribute [r] name
# @return [String]
#
# @!attribute [r] uri
# @return [String]
#
class Namespace
attr_accessor :name
attr_accessor :name, :uri
##
# @param [Hash] options
#
# @option options [String] :name
# @option options [String] :uri
#
def initialize(options = {})
@name = options[:name]
@uri = options[:uri]
end
##
@@ -27,7 +35,7 @@ module Oga
# @return [String]
#
def inspect
return "Namespace(name: #{name.inspect})"
return "Namespace(name: #{name.inspect} uri: #{uri.inspect})"
end
end # Namespace
end # XML
+1 -3
View File
@@ -150,9 +150,7 @@ rule
# foo:bar
| T_ATTR_NS T_ATTR
{
ns = Namespace.new(:name => val[0])
Attribute.new(:namespace => ns, :name => val[1])
Attribute.new(:namespace_name => val[0], :name => val[1])
}
;