Sha256: 3c2cf3ef4316aeb09b95bf36ba680b68a285b9198006265e3f45c7c54bd0ac1f

Contents?: true

Size: 836 Bytes

Versions: 5

Compression:

Stored size: 836 Bytes

Contents

module CouchPotato
  module View
    # a view to return model instances by searching its properties
    # example: view :my_view, :key => :name
    class ModelViewSpec < BaseViewSpec
      
      def view_parameters
        {:include_docs => true}.merge(super)
      end
      
      def map_function
        "function(doc) {
            emit(#{formatted_key(key)}, null);
         }"
      end
      
      def process_results(results)
        results['rows'].map do |row|
          klass.json_create row['doc']
        end
      end
      
      private
      
      def key
        options[:key]
      end
      
      def formatted_key(key)
        if key.is_a? Array
          '[' + key.map{|attribute| formatted_key(attribute)}.join(', ') + ']'
        else
          "doc['#{key}']"
        end
      end
      
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
langalex-couch_potato-0.2.0 lib/couch_potato/view/model_view_spec.rb
langalex-couch_potato-0.2.1 lib/couch_potato/view/model_view_spec.rb
langalex-couch_potato-0.2.2 lib/couch_potato/view/model_view_spec.rb
speedmax-couch_potato-0.2.0 lib/couch_potato/view/model_view_spec.rb
speedmax-couch_potato-0.2.2 lib/couch_potato/view/model_view_spec.rb