Sha256: 03b71aa70e5591ccf257ff6f398956f9cbb9b38de7d3ec6e551fe8b6426b561c

Contents?: true

Size: 1.3 KB

Versions: 22

Compression:

Stored size: 1.3 KB

Contents

require 'restful/serializers/base'
require 'builder'

#
#  AR params hash.
#
module Restful
  module Serializers
    class ParamsSerializer < Base
      
      serializer_name :params
      
      def serialize(resource, options = {})
        params = {}
        resource.values.each do |value|
          if value.type == :collection # serialize the stuffs
            resources = value.value
            next if resources.empty?
            name = resources.first.name.pluralize
            
            array = []
            resources.each do |resource|
              array << serialize(resource)
            end              
            
            params["#{paramify_keys(value.name)}_attributes".to_sym] = array
          elsif value.type == :link
            params[paramify_keys(value.name).to_sym] = Restful::Rails.tools.dereference(value.value)
          elsif value.type == :resource
            params["#{paramify_keys(value.name)}_attributes".to_sym] = serialize(value)
          else # plain ole
            params[paramify_keys(value.name).to_sym] = value.value # no need to format dates etc - just pass objects through. 
          end
        end
        
        params
      end
      
      private
      
      def paramify_keys(original_key)
        original_key.to_s.tr("-", "_")
      end
    end    
  end
end

Version data entries

22 entries across 22 versions & 4 rubygems

Version Path
benjaminkrause-restful-0.2.8 lib/restful/serializers/params_serializer.rb
moviepilot-restful-0.2.18 lib/restful/serializers/params_serializer.rb
moviepilot-restful-0.2.19 lib/restful/serializers/params_serializer.rb
moviepilot-restful-0.2.20 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.10 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.11 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.12 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.13 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.14 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.15 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.16 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.17 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.18 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.2 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.3 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.4 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.5 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.6 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.7 lib/restful/serializers/params_serializer.rb
purzelrakete-restful-0.2.8 lib/restful/serializers/params_serializer.rb