Skip to content

How to access wrap child elements #106

@fidalgo

Description

@fidalgo

I'm creating a XML serializer, that comes after a mapper and a validator, and they share the same methods, so a code method exists in both mapper and validator. To avoid manually creating every field, I'm using some metaprogramming to set the values on the XML serializer, when I use a wrap I cannot set the child elements.

I have the following the following class:

require 'happymapper'

class Country
  include HappyMapper

  element :code, String
  element :name, String
end

class Profile
  include HappyMapper

  element :country, Country
  element :name, String
end

class User
  include HappyMapper

  wrap 'PersonalInformation' do
    has_many :profiles, Profile
  end
  element :name, String
end

I need to access the elements of PersonalInformation so I've tried:

User.new.class.elements

=> [#<HappyMapper::Element:0x0000558f73aed438
  @method_name="PersonalInformation",
  @name="PersonalInformation",
  @options={:single=>true, :name=>"PersonalInformation"},
  @tag="PersonalInformation",
  @type=#<Class:0x0000558f73aee3d8>,
  @xml_type="element">,
 #<HappyMapper::Element:0x0000558f73aece20
  @method_name="name",
  @name="name",
  @options={:single=>true, :name=>"name"},
  @tag="name",
  @type=String,
  @xml_type="element">]

But since the PersonalInformation it's an element itself, I don't have a way to find it's child elements. Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions