Sha256: d5fcca26094438c37e1345451cd7bbde4fad93cd3cd3f7ac106a154e01f9aa46

Contents?: true

Size: 1.04 KB

Versions: 31

Compression:

Stored size: 1.04 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 view_parameters
        {:include_docs => options[:include_docs] || false}.merge(super)
      end
      
      def process_results(results)
        if count?
          results['rows'].first.try(:[], 'value') || 0
        else  
          results['rows'].map do |row|
            if row['doc'].instance_of?(klass)
              row['doc']
            else
              klass.json_create row['doc'] || row['value'].merge(:_id => row['id'] || row['key'])
            end
          end
        end
      end
      
      private 
      
      def count?
        view_parameters[:reduce]
      end
      
    end
  end
end

Version data entries

31 entries across 31 versions & 3 rubygems

Version Path
couch_potato-0.7.1 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.7.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.7.0.pre.1 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.6.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.7 lib/couch_potato/view/custom_view_spec.rb
couch_potato-rails2-0.5.10 lib/couch_potato/view/custom_view_spec.rb
couch_potato-rails2-0.5.9 lib/couch_potato/view/custom_view_spec.rb
couch_potato-rails2-0.5.8 lib/couch_potato/view/custom_view_spec.rb
couch_potato-rails2-0.5.7 lib/couch_potato/view/custom_view_spec.rb
couch_potato-rails2-0.5.6 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.6 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.5 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.4 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.3 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.2 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.1 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.5.0 lib/couch_potato/view/custom_view_spec.rb
davber_couch_potato-0.4.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.4.0 lib/couch_potato/view/custom_view_spec.rb
couch_potato-0.3.2 lib/couch_potato/view/custom_view_spec.rb