lib/roar/representer/xml.rb in roar-0.8.0 vs lib/roar/representer/xml.rb in roar-0.8.1

- old
+ new

@@ -1,43 +1,67 @@ -require 'roar/representer' +require 'roar/representer/base' require 'representable/xml' - module Roar - # Basic work-flow - # in: * representer parses representation - # * recognized elements are stored as representer attributes - # out: * attributes in representer are assigned - either as hash in #to_xml, by calling #serialize(represented), - # by calling representer's accessors (eg in client?) or whatever else - # * representation is compiled from representer only - # TODO: make XML a module to include in Hyperlink < Base. + # Includes #from_xml and #to_xml into your represented object. + # In addition to that, some more options are available when declaring properties. module Representer - class XML < Base - include Representable::XML - - def serialize - to_xml.serialize + module XML + def self.included(base) + base.class_eval do + include Base + include Representable::XML + + extend ClassMethods + include InstanceMethods # otherwise Representable overrides our #to_xml. + end end - def self.deserialize(xml) - from_xml(xml) + module InstanceMethods + def from_xml(document, options={}) + if block = deserialize_block_for_options(options) + return super(document, &block) + end + + super + end + + def to_xml(*args) + before_serialize(*args) + super.serialize + end + + # Generic entry-point for rendering. + def serialize(*args) + to_xml(*args) + end end + module ClassMethods + include Representable::XML::ClassMethods + + def links_definition_options + {:from => :link, :as => [Hyperlink]} + end + + # Generic entry-point for parsing. + def deserialize(*args) + from_xml(*args) + end + end + + # Encapsulates a hypermedia <link ...>. - class Hyperlink < self + class Hyperlink + # TODO: make XML a module to include in Hyperlink < Base. + include XML + self.representation_name = :link property :rel, :from => "@rel" property :href, :from => "@href" end - - def self.links_definition_options - {:tag => :link, :as => [Hyperlink]} - end - - require 'roar/representer/feature/hypermedia' - include Feature::Hypermedia end end end