Sha256: f96777098819b62f9c99e7e5e12b33a47cfd7df11d4db9a3d261174f0c26242d

Contents?: true

Size: 1.02 KB

Versions: 12

Compression:

Stored size: 1.02 KB

Contents

class RestModel
  module Response
    extend ActiveSupport::Concern

    def resource(options = {})
      root = options.fetch(:root, true)

      {}.tap do |resource|
        resource_keys(options).inject(resource) do |buffer, key|
          buffer.merge!(key.to_resource(self))
        end

        if root and self.class.relations.any? and !options[:summarize]
          resource.merge!({link: link})
        end
      end
    end

    def link
      self.class.relations.map {|key| key.to_relation(self)}
    end

    def resource_keys(options)
      summarize?(options) ? self.class.summarized_keys + [Href.new]
                          : self.class.keys
    end

    def summarize?(options)
      options[:summarize] and self.class.summarized_keys.any?
    end

    module ClassMethods
      def normalize(model, options = {})
        model.kind_of?(Enumerable) ?
          {entries: model.map {|m| m.resource(options.merge(summarize: true))}}
         : model.resource(options)
      end

      alias :resources :normalize
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rest_model-0.1.11 lib/rest_model/response.rb
rest_model-0.1.10 lib/rest_model/response.rb
rest_model-0.1.9 lib/rest_model/response.rb
rest_model-0.1.8 lib/rest_model/response.rb
rest_model-0.1.7 lib/rest_model/response.rb
rest_model-0.1.6 lib/rest_model/response.rb
rest_model-0.1.5 lib/rest_model/response.rb
rest_model-0.1.4 lib/rest_model/response.rb
rest_model-0.1.3 lib/rest_model/response.rb
rest_model-0.1.2 lib/rest_model/response.rb
rest_model-0.1.1 lib/rest_model/response.rb
rest_model-0.1.0 lib/rest_model/response.rb