Sha256: 453be51ee3245f2c0fc6eac578791ecca925ff5116a17619e32035bdacef6f5a

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

require 'restful/serializers/base'

#
#  AR params hash.
#
module Restful
  module Serializers
    class HashSerializer < Base
      
      serializer_name :hash
      
      def serialize(resource, options = {})
        case resource
        when Restful::ApiModel::Collection then serialize_collection(resource)
        when Restful::ApiModel::Resource   then serialize_tuples(resource.values, resource.full_url)
        when Restful::ApiModel::Map        then serialize_map(resource)
        else
          serialize_tuples(resource.values, resource.full_url)
        end
      end

      private      
        
        def serialize_collection(collection)
          values = collection.value.map { |r| serialize(r) }
        
          if entries = collection.total_entries
            values = { :total_entries => entries, collection.name => values }
          end
        
          values
        end
        
        def serialize_map(map)
          map.values.inject({}) do |memo, attribute|
            memo[attribute.name] = serialize_value(attribute.value)
            memo
          end
        end
        
        def serialize_tuples(tuples, url)
          tuples.inject({ "restful_url" => url }) do |params, value|
            params[value.name.to_s.tr("-", "_").to_sym] = serialize_value(value)
            params
          end
        end
        
        def serialize_value(value)
          case value.type
            when :collection then serialize_collection(value)
            when :link       then value.value
            when :resource   then serialize(value)
            else                  formatted_value(value)
          end
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
purzelrakete-restful-0.2.14 lib/restful/serializers/hash_serializer.rb
purzelrakete-restful-0.2.15 lib/restful/serializers/hash_serializer.rb