Sha256: faba38a206414ed6ae99d9e04f21034ff8e8bfac441ad0276d922724b7b63fe2

Contents?: true

Size: 1.23 KB

Versions: 24

Compression:

Stored size: 1.23 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(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
      
      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

24 entries across 24 versions & 3 rubygems

Version Path
couch_potato-0.6.0 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.7 lib/couch_potato/view/properties_view_spec.rb
couch_potato-rails2-0.5.10 lib/couch_potato/view/properties_view_spec.rb
couch_potato-rails2-0.5.9 lib/couch_potato/view/properties_view_spec.rb
couch_potato-rails2-0.5.8 lib/couch_potato/view/properties_view_spec.rb
couch_potato-rails2-0.5.7 lib/couch_potato/view/properties_view_spec.rb
couch_potato-rails2-0.5.6 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.6 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.5 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.4 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.3 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.2 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.1 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.5.0 lib/couch_potato/view/properties_view_spec.rb
davber_couch_potato-0.4.0 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.4.0 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.3.2 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.3.1 lib/couch_potato/view/properties_view_spec.rb
davber_couch_potato-0.3.0 lib/couch_potato/view/properties_view_spec.rb
couch_potato-0.3.0 lib/couch_potato/view/properties_view_spec.rb