Sha256: 71f836176f01dc69c2c432eaaf4da9f2ef60c7cf46783cb3e6807beaa9cdf641

Contents?: true

Size: 1.3 KB

Versions: 21

Compression:

Stored size: 1.3 KB

Contents

module CouchPotato
  module View
    # a view for custom map/reduce functions that still returns model instances
    #
    # example:
    #   view :my_custom_view, :map => "function(doc) { emit(doc._id, null); }", :include_docs => true, :type => :custom, :reduce => nil
    class CustomViewSpec < BaseViewSpec
      def map_function
        options[:map]
      end

      def reduce_function
        options[:reduce]
      end

      def lib
        options[:lib]
      end

      def view_parameters
        {:include_docs => options[:include_docs] || false}.merge(super)
      end

      def process_results(results)
        processed = if count?
                      results['rows'].first.try(:[], 'value') || 0
                    else
                      results['rows'].map do |row|
                        if row['doc'].kind_of?(klass)
                          row['doc']
                        else
                          result = row['doc'] || (row['value'].merge(:_id => row['id'] || row['key']) unless view_parameters[:include_docs])
                          klass.json_create result if result
                        end
                      end.compact
                    end
        super processed
      end

      private

      def count?
        view_parameters[:reduce]
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
couch_potato-1.17.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.16.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.15.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.14.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.13.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.12.1 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.12.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.11.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.10.1 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.10.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.9.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.7.1 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.7.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.6.5 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.6.4 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.6.3 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.4.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.3.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.2.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-1.1.4 lib/couch_potato/view/custom_view_spec.rb