Sha256: f253e7c586b8deeec9a251ba988137d6043a6df8c486b463fc686d1066967b0e

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

require 'roar/representer/base'
require 'representable/xml'

module Roar
  # Includes #from_xml and #to_xml into your represented object.
  # In addition to that, some more options are available when declaring properties.
  module Representer
    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
      
      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
        # 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
      
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roar-0.8.2 lib/roar/representer/xml.rb
roar-0.8.1 lib/roar/representer/xml.rb