Skip to content

Instantly share code, notes, and snippets.

@jahan-paisley
Created March 13, 2013 11:26
Show Gist options
  • Save jahan-paisley/5151239 to your computer and use it in GitHub Desktop.
Save jahan-paisley/5151239 to your computer and use it in GitHub Desktop.
require 'jcouchbase'
require 'json'
include Java
import com.couchbase.client.protocol.views.Query
class Couchbase
attr_accessor :client
alias :old_getter :[]
def [](key)
to_hash(old_getter(key))
end
def get_view(design, view, key = nil, include_docs = true)
query = Query.new
query.setIncludeDocs(include_docs)
query.setKey(key) unless key.nil?
view = client.getView(design, view)
client.asyncQuery(view, query).get.iterator.map do |row|
{
:id => row.getId,
:document => to_hash(row.getDocument),
:view_key => row.getKey,
:view_value => row.getValue,
}
end
end
private
def to_hash(value)
Hash[JSON.parse(value).map { |key, value| [key.to_sym, value] }]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment