Sha256: a2bc8504fc9acdb1abc70cf5802aee1e77cf544a78dd227e8d19e50828acf3ac

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'roar/representer'
require 'roar/representer/feature/hypermedia'
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 Representer
          include Representable::XML

          extend ClassMethods
          include InstanceMethods # otherwise Representable overrides our #to_xml.
        end
      end
      
      module InstanceMethods
        def to_xml(*args)
          before_serialize(*args)
          super
        end
        
        # Generic entry-point for rendering.
        def serialize(*args)
          to_xml(*args)
        end
        
        def deserialize(*args)
          from_xml(*args)
        end
      end
      
      
      module ClassMethods
        include Representable::XML::ClassMethods
        
        def links_definition_options
          [:links, {:from => :link, :class => Feature::Hypermedia::Hyperlink, :collection => true, :extend => XML::HyperlinkRepresenter}]
        end
        
        # Generic entry-point for parsing.
        def deserialize(*args)
          from_xml(*args)
        end
      end
      
      module HyperlinkRepresenter
        include XML
        
        self.representation_wrap = :link
        
        Feature::Hypermedia::Hyperlink.params.each do |attr|
          property attr, :attribute => true
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roar-0.10.2 lib/roar/representer/xml.rb
roar-0.10.1 lib/roar/representer/xml.rb
roar-0.10.0 lib/roar/representer/xml.rb