Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to return XML node attributes as hash, not as object property #106

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/nori.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ def self.hash_key(name, options = {})

def initialize(options = {})
defaults = {
:strip_namespaces => false,
:delete_namespace_attributes => false,
:convert_tags_to => nil,
:convert_attributes_to => nil,
:empty_tag_value => nil,
:advanced_typecasting => true,
:convert_dashes_to_underscores => true,
:scrub_xml => true,
:parser => :nokogiri
:strip_namespaces => false,
:delete_namespace_attributes => false,
:convert_tags_to => nil,
:convert_attributes_to => nil,
:empty_tag_value => nil,
:advanced_typecasting => true,
:convert_dashes_to_underscores => true,
:scrub_xml => true,
:parser => :nokogiri,
:string_with_attributes_as_hash => false,
}

validate_options! defaults.keys, options.keys
Expand Down
8 changes: 6 additions & 2 deletions lib/nori/xml_utility_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ def to_hash
t = advanced_typecasting(t) if t.is_a?(String) && @options[:advanced_typecasting]

if t.is_a?(String)
t = StringWithAttributes.new(t)
t.attributes = attributes
if @options[:string_with_attributes_as_hash] && attributes.any?
t = { '#text' => t }.merge(attributes.transform_keys { |k| "@#{k}" })
else
t = StringWithAttributes.new(t)
t.attributes = attributes
end
end

return { name => t }
Expand Down
14 changes: 14 additions & 0 deletions spec/nori/nori_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@
expect("some-string").not_to respond_to(:attributes)
expect("some-string").not_to respond_to(:attributes=)
end

it "returns hash with :string_with_attributes_as_hash option" do
xml =<<-XML
<opt>
<test attr="value" attr1="value1">Text</test>
<user login="grep">Gary R Epstein</user>
<user>Simon T Tyson</user>
</opt>
XML
@data = parse(xml, { string_with_attributes_as_hash: true })
expect(@data['opt']['test']).to eq({ '#text' => 'Text', '@attr' => 'value', '@attr1' => 'value1' })
expect(@data['opt']['user'][0]).to eq({ '#text' => 'Gary R Epstein', '@login' => 'grep' })
expect(@data['opt']['user'][1]).to eq('Simon T Tyson')
end
end

it "should typecast an integer" do
Expand Down
Loading