From ce86785da684596d2f279f77aa385f89ac5d1862 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 16 Jul 2014 10:08:11 +0200 Subject: [PATCH] Added the XML::Attribute class. This class will replace the use of raw Hash/String values for attributes in upcoming commits. --- lib/oga.rb | 1 + lib/oga/xml/attribute.rb | 50 ++++++++++++++++++++++++++++++++++ spec/oga/xml/attribute_spec.rb | 35 ++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 lib/oga/xml/attribute.rb create mode 100644 spec/oga/xml/attribute_spec.rb diff --git a/lib/oga.rb b/lib/oga.rb index 9c34495..4bd80ec 100644 --- a/lib/oga.rb +++ b/lib/oga.rb @@ -24,6 +24,7 @@ require_relative 'oga/xml/comment' require_relative 'oga/xml/cdata' require_relative 'oga/xml/xml_declaration' require_relative 'oga/xml/doctype' +require_relative 'oga/xml/attribute' require_relative 'oga/xml/node_set' require_relative 'oga/html/parser' diff --git a/lib/oga/xml/attribute.rb b/lib/oga/xml/attribute.rb new file mode 100644 index 0000000..a734403 --- /dev/null +++ b/lib/oga/xml/attribute.rb @@ -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 diff --git a/spec/oga/xml/attribute_spec.rb b/spec/oga/xml/attribute_spec.rb new file mode 100644 index 0000000..7d063f0 --- /dev/null +++ b/spec/oga/xml/attribute_spec.rb @@ -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