Happymapper (fork) - output from multiple classes

My question is related to creating documentation-based output in https://github.com/dam5s/happymapper , which is fork happymapper using nokogiri.

I used 2 examples in the documentation with the documentation. This is my example.

xml_doc = <<EOF <address location='home'> <street>Milchstrasse</street> <street>Another Street</street> <housenumber>23</housenumber> <postcode>26131</postcode> <city>Oldenburg</city> <country code="de">Germany</country> </address> EOF class Address include HappyMapper tag 'address' element :housenumber, Integer, :tag => "housenumber" end class Country include HappyMapper tag 'country' attribute :code, String content :name, String end outputs = Country.parse(xml_doc) outputs.each do |output| puts output.code puts output.name puts output.housenumber end 

Expected Result

 de Germany 23 

My conclusion

 sayth@sayth-E6410 ~/race (master●)$ ruby read_race.rb [ruby-2.4.0p0] de Germany read_race.rb:49:in `block in <main>': undefined method `housenumber' for #<Country:0x0055e55facf798 @code="de", @name="Germany"> (NoMethodError) from read_race.rb:46:in `each' from read_race.rb:46:in `<main>' 
+5
source share
1 answer

This is more or less direct copy / paste from documents. Hope this gives you what you want.

The most important parts call Address.parse instead of Country.parse and refer to Country fields as output.country.code instead of output.code . Then it works exactly as advertised in the Happymapper file.

 #!/usr/bin/env ruby require 'happymapper' ADDRESS_XML_DATA = <<XML <root> <address location='home'> <street>Milchstrasse</street> <street>Another Street</street> <housenumber>23</housenumber> <postcode>26131</postcode> <city>Oldenburg</city> <country code="de">Germany</country> </address> </root> XML class Country include HappyMapper tag 'country' attribute :code, String content :name, String end class Address include HappyMapper tag 'address' has_many :streets, String, :tag => 'street' def streets @streets.join('\n') end element :postcode , String , :tag => 'postcode' element :housenumber, String , :tag => 'housenumber' element :city , String , :tag => 'city' element :country , Country, :tag => 'country' end outputs = Address.parse(ADDRESS_XML_DATA) outputs.each do |output| puts output.country.code puts output.country.name puts output.housenumber end 
+3
source

Source: https://habr.com/ru/post/1263865/


All Articles