Sha256: 72f8ec7a0beb2f8008b2c9a9684cb669510d46954c18da07923443e30ba88225

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module Couchbase
  class View
    java_import com.couchbase.client.java.view.ViewQuery
    java_import com.couchbase.client.java.document.json.JsonArray
    java_import com.couchbase.client.java.document.RawJsonDocument

    def initialize(design_doc, view, bucket)
      @format = :json
      @bucket = bucket.bucket
      @view_query = ViewQuery.from(design_doc, view.to_s)
    end

    def key(key)
      @view_query.key(convert_key(key))
      self
    end

    def keys(keys)
      @view_query.keys(JsonArray.from(keys.to_java))
      self
    end

    def start_key(key)
      @view_query.start_key(convert_key(key))
      self
    end

    def end_key(key)
      @view_query.end_key(convert_key(key))
      self
    end

    def limit(num)
      @view_query.limit(num)
      self
    end

    def skip(num)
      @view_query.skip(num)
      self
    end

    def group(group = true)
      @view_query.group(group)
      self
    end

    def group_level(level)
      @view_query.group_level(level)
      self
    end

    def reduce(reduce = true)
      @view_query.reduce(reduce)
      self
    end

    def fresh
      @view_query.stale(com.couchbase.client.java.view.Stale::FALSE)
      self
    end

    def fetch
      results = @bucket.query(@view_query)
      {}.tap do |response|
        results.each do |view_row|
          if view_row.id.nil?
            # Reduced view
            return view_row.value
          else
            doc = view_row.document(RawJsonDocument.java_class)
            response[view_row.id] = doc.nil? ? nil : Document.new(doc)
          end
        end
      end
    end

    private

    def convert_key(key)
      case key
      when Array
        JsonArray.from(key.to_java)
      else
        key
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
couchbase-jruby-client-1.0.4-java lib/couchbase/view.rb