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

When a collection is empty, don't render anything #5

Open
ronaldtse opened this issue May 22, 2024 · 1 comment
Open

When a collection is empty, don't render anything #5

ronaldtse opened this issue May 22, 2024 · 1 comment

Comments

@ronaldtse
Copy link

When a collection is empty, there is no need to render.

Instead, apply render_nil: true behavior when specified. Right now render_nil only works on elements not on collections.

@HassanAkbar
Copy link
Member

@ronaldtse I tried to run the below code and it does not render anything if the collection is empty.

require 'shale'
require 'shale/adapter/nokogiri'
Shale.xml_adapter = Shale::Adapter::Nokogiri

class Address < Shale::Mapper
  attribute :city, Shale::Type::String
  attribute :street, Shale::Type::String
  attribute :zip, Shale::Type::String, collection: true
  
  xml do
    root "address"

    map_element "city", to: :city
    map_element "street", to: :street
    map_element "zip", to: :zip
  end
end

address = Address.from_xml(<<~DATA)
  <address>
    <city>London</city>
    <street>Oxford Street</street>
  </address>
DATA

puts address.to_xml(pretty: true)

the above code outputs the following XML

<address>
  <city>London</city>
  <street>Oxford Street</street>
</address>

Do you mean something else here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants