Sha256: 76e67519b323c6f1e0292b74a5d68f0968e0e2b32d71ae6d9ec888da244e8dbd

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

require 'active_support/core_ext/hash/conversions'

module Restfulie
  module Common
    module Converter
      module Xml
        module Base
          module ClassMethods
            mattr_reader :media_type_name
            @@media_type_name = 'application/xml'
        
            mattr_reader :headers
            @@headers = { 
              :post => { 'Content-Type' => media_type_name }
            }
        
            def marshal(entity, options = {})
              to_xml(entity, options)
            end
        
            def unmarshal(string)
              Hash.from_xml string
            end
        
            mattr_reader :recipes
            @@recipes = {}
        
            def describe_recipe(recipe_name, options={}, &block)
              raise 'Undefined recipe' unless block_given?
              raise 'Undefined recipe_name'   unless recipe_name
              @@recipes[recipe_name] = block
            end
        
            def to_xml(obj, options = {}, &block)
              return obj if obj.kind_of?(String)
              
              if block_given?
                recipe = block 
              elsif options[:recipe]
                recipe = @@recipes[options[:recipe]]
              elsif obj.kind_of?(Hash) && obj.size==1
                return obj.values.first.to_xml(:root => obj.keys.first)
              else
                return obj.to_xml
              end
              
              # Create representation and proxy
              builder = Builder.new(obj, options)
        
              # Check recipe arity size before calling it
              recipe.call(*[builder, obj, options][0,recipe.arity])
              builder.doc.to_xml
            end
            
            def helper
              Helpers
            end
          end
        end
      end
    end
  end
end 

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restfulie-1.0.0.beta1 lib/restfulie/common/converter/xml/base.rb
restfulie-0.1.0.beta1 lib/restfulie/common/converter/xml/base.rb