Skip to content

Subclasses with no additional nodes can't be loaded from XML #4

Description

@dmolesUC

I have a case where I want to create objects of different types from the same XML, depending on the value of certain attributes. I want to declare all the attributes in a superclass and then extend that for each subclass. Unfortunately, something in the mapping initialization seems to be dependent on declaring nodes in the leaf class. If I try to load_from_xml() on a leaf class with no nodes of its own, I get

/Users/dmoles/.rvm/gems/ruby-2.2.1/gems/xml-mapping-0.10.0/lib/xml/mapping/base.rb:343:in
    `load_from_xml': undefined mapping: :_default (XML::MappingError)

Clearly there's something going on by side effect when you declare a node, but I haven't figured out what it is.

Example:

#!/usr/bin/env ruby

require 'xml/mapping'

xml_str = '<foo bar="baz"/>'
xml = REXML::Document.new(xml_str).root

# ------------------------------
# No inheritance -- works

class Foo
  include XML::Mapping

  text_node :bar, '@bar', default_value: nil
end

foo = Foo.load_from_xml(xml)
puts "foo.bar: #{foo.bar}"

# ------------------------------
# Inheritance + defines an extra node -- works

class Bar < Foo
  include XML::Mapping

  text_node :baz, '@baz', default_value: nil
end

bar = Bar.load_from_xml(xml)
puts "bar.bar: #{bar.bar}"

# ------------------------------
# Inheritance + no nodes of its own -- fails 

class Baz < Foo
  include XML::Mapping
end

baz = Baz.load_from_xml(xml) # <- XML::MappingError

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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