Sha256: 2f98201ec506220703a8594224e3a6354f69fe937c8f8244e3f2a65d4b68d968

Contents?: true

Size: 1.24 KB

Versions: 29

Compression:

Stored size: 1.24 KB

Contents

module CouchPotato
  module View
    # A view to return model instances with only some properties poulated by searching its properties, e.g. for very large documents where you are only interested in some of their data
    #
    # example:
    #   view :my_view, :key => :name, :properties => [:name, :author], :type => :properties
    class PropertiesViewSpec < ModelViewSpec
      def map_function
        map_body do
          "emit(#{formatted_key}, #{properties_for_map(properties)});"
        end
      end

      def reduce_function
        <<-JS
          function(key, values, rereduce) {
            if(rereduce) {
              return sum(values);
            } else {
              return values.length;
            }
          }
        JS
      end

      def process_results(results)
        results['rows'].map do |row|
          klass.json_create row['value'].merge(:_id => row['id'])
        end
      end

      def view_parameters
        {:include_docs => false}.merge(super)
      end

      def language
        :javascript
      end

      private

      def properties
        options[:properties]
      end

      def properties_for_map(properties)
        '{' + properties.map{|p| "#{p}: doc.#{p}"}.join(', ') + '}'
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

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