Hacker News new | ask | show | jobs
by halostatue 1482 days ago
I haven’t looked at the Shale source code, but I suspect that it would not be hard to add `mapped_class` support the way you’ve described it, so that the business objects are not themselves mapper instances. At a guess, the `from_xml` probably does something like (vastly over simplified):

    def from_xml(xml_string)
      new.tap { |o|
        parse_xml(xml_string) do |key, value|
          o.__send__(:"#{key}=", value)
        end
      }
    end
It would then be possible to change this to:

    def from_xml(xml_string)
      (mapped_class || self).new.tap { |o|
        parse_xml(xml_string) do |key, value|
          o.__send__(:"#{key}=", value)
        end
      }
    end
      
This would make it easier to solve a larger problem of needing to serialize the same business object in different ways for different consumers with different levels of detail. It would also permit the construction of mappers for temporary objects that contain the details for more complex serializations that have indirect connections.