Sha256: 3989c57c877b5566ef49c7e376faf28f3e8fd8364c69c770b0694b40260491c1

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

module CouchPotato
  module View

    class ViewQuery
      def initialize(couchrest_database, design_document_name, view_name, map_function, reduce_function = nil)
        @database = couchrest_database
        @design_document_name = design_document_name
        @view_name = view_name
        @map_function = map_function
        @reduce_function = reduce_function
      end
      
      def query_view!(parameters = {})
        begin
          query_view parameters
        rescue RestClient::ResourceNotFound => e
          create_view
          retry
        end
      end
      
      private
      
      def create_view
        design_doc = @database.get "_design/#{@design_document_name}" rescue nil
        design_doc ||= {'views' => {}, "_id" => "_design/#{@design_document_name}"}
        design_doc['views'][@view_name.to_s] = {
          'map' => @map_function,
          'reduce' => @reduce_function
        }
        @database.save_doc(design_doc)
      end
      
      def query_view(parameters)
        @database.view view_url, parameters
      end
      
      def view_url
        "#{@design_document_name}/#{@view_name}"
      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/view_query.rb
langalex-couch_potato-0.2.1 lib/couch_potato/view/view_query.rb
langalex-couch_potato-0.2.2 lib/couch_potato/view/view_query.rb
speedmax-couch_potato-0.2.0 lib/couch_potato/view/view_query.rb
speedmax-couch_potato-0.2.2 lib/couch_potato/view/view_query.rb