Added the XML::Attribute class.

This class will replace the use of raw Hash/String values for attributes in
upcoming commits.
This commit is contained in:
Yorick Peterse 2014-07-16 10:08:11 +02:00
parent d09ab26680
commit ce86785da6
3 changed files with 86 additions and 0 deletions

View File

@ -24,6 +24,7 @@ require_relative 'oga/xml/comment'
require_relative 'oga/xml/cdata' require_relative 'oga/xml/cdata'
require_relative 'oga/xml/xml_declaration' require_relative 'oga/xml/xml_declaration'
require_relative 'oga/xml/doctype' require_relative 'oga/xml/doctype'
require_relative 'oga/xml/attribute'
require_relative 'oga/xml/node_set' require_relative 'oga/xml/node_set'
require_relative 'oga/html/parser' require_relative 'oga/html/parser'

50
lib/oga/xml/attribute.rb Normal file
View File

@ -0,0 +1,50 @@
module Oga
module XML
##
# Class for storing information about a single XML attribute.
#
# @!attribute [rw] name
# The name of the attribute.
# @return [String]
#
# @!attribute [rw] namespace
# The namespace of the attribute.
# @return [String]
#
# @!attribute [rw] value
# The value of the attribute.
# @return [String]
#
class Attribute
attr_accessor :name, :namespace, :value
##
# @param [Hash] options
#
# @option options [String] :name
# @option options [String] :namespace
# @option options [String] :value
#
def initialize(options = {})
@name = options[:name]
@namespace = options[:namespace]
@value = options[:value]
end
##
# @return [String]
#
def to_s
return value.to_s
end
##
# @return [String]
#
def inspect
return "Attribute(name: #{name.inspect} " \
"namespace: #{namespace.inspect} value: #{value.inspect})"
end
end # Attribute
end # XML
end # Oga

View File

@ -0,0 +1,35 @@
require 'spec_helper'
describe Oga::XML::Attribute do
context '#initialize' do
example 'set the name' do
described_class.new(:name => 'a').name.should == 'a'
end
example 'set the namespace' do
described_class.new(:namespace => 'a').namespace.should == 'a'
end
example 'set the value' do
described_class.new(:value => 'a').value.should == 'a'
end
end
context '#to_s' do
example 'return an empty String when there is no value' do
described_class.new.to_s.should == ''
end
example 'return the value if it is present' do
described_class.new(:value => 'a').to_s.should == 'a'
end
end
context '#inspect' do
example 'return the inspect value' do
obj = described_class.new(:name => 'a', :namespace => 'b', :value => 'c')
obj.inspect.should == 'Attribute(name: "a" namespace: "b" value: "c")'
end
end
end