Add aliases for accessing attributes via [] and []=
This also fixes accessing attributes via symbol name and tests to ensure that such does not break in the future.
This commit is contained in:
parent
b8fd8670df
commit
d40baf0c72
|
@ -91,6 +91,8 @@ module Oga
|
||||||
found ? found.value : nil
|
found ? found.value : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias_method :[], :get
|
||||||
|
|
||||||
# Adds a new attribute to the element.
|
# Adds a new attribute to the element.
|
||||||
#
|
#
|
||||||
# @param [Oga::XML::Attribute] attribute
|
# @param [Oga::XML::Attribute] attribute
|
||||||
|
@ -113,11 +115,7 @@ module Oga
|
||||||
if found
|
if found
|
||||||
found.value = value
|
found.value = value
|
||||||
else
|
else
|
||||||
if name.include?(':')
|
name, ns = split_name(name)
|
||||||
ns, name = name.split(':')
|
|
||||||
else
|
|
||||||
ns = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
attr = Attribute.new(
|
attr = Attribute.new(
|
||||||
:name => name,
|
:name => name,
|
||||||
|
@ -129,6 +127,8 @@ module Oga
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias_method :[]=, :set
|
||||||
|
|
||||||
# Removes an attribute from the element.
|
# Removes an attribute from the element.
|
||||||
#
|
#
|
||||||
# @param [String] name The name (optionally including namespace prefix)
|
# @param [String] name The name (optionally including namespace prefix)
|
||||||
|
|
|
@ -129,6 +129,13 @@ describe Oga::XML::Element do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#[]' do
|
||||||
|
it 'is an alias to get' do
|
||||||
|
described_class.instance_method(:[]).should ==
|
||||||
|
described_class.instance_method(:get)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#add_attribute' do
|
describe '#add_attribute' do
|
||||||
before do
|
before do
|
||||||
@element = described_class.new
|
@element = described_class.new
|
||||||
|
@ -161,6 +168,14 @@ describe Oga::XML::Element do
|
||||||
@element.get('class').should == 'foo'
|
@element.get('class').should == 'foo'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'supports the use of Symbols for attribute names' do
|
||||||
|
@element.set(:foo, 'foo')
|
||||||
|
@element.get('foo').should == 'foo'
|
||||||
|
|
||||||
|
@element.set('bar', 'bar')
|
||||||
|
@element.get(:bar).should == 'bar'
|
||||||
|
end
|
||||||
|
|
||||||
it 'adds a new attribute with a namespace' do
|
it 'adds a new attribute with a namespace' do
|
||||||
@element.set('x:bar', 'foo')
|
@element.set('x:bar', 'foo')
|
||||||
|
|
||||||
|
@ -186,6 +201,13 @@ describe Oga::XML::Element do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#[]=' do
|
||||||
|
it 'is an alias to set' do
|
||||||
|
described_class.instance_method(:[]=).should ==
|
||||||
|
described_class.instance_method(:set)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#unset' do
|
describe '#unset' do
|
||||||
before do
|
before do
|
||||||
@element = described_class.new
|
@element = described_class.new
|
||||||
|
|
Loading…
Reference in New Issue