class Tatami::Couch::View

Public Class Methods

new(couch, name) click to toggle source
# File lib/tatami/couch.rb, line 238
def initialize couch, name
  @couch = couch
  name.prepend "#{@couch.db}/" unless name['/']
  @design, @view = name.split('/')
  @base = "/_design/#{@design}/_view/#{@view}"
end

Public Instance Methods

create(map, reduce=nil) click to toggle source

@param [String] map @p @return [View]

# File lib/tatami/couch.rb, line 284
def create map, reduce=nil
  v = {:map => map}
  v[:reduce] = reduce if reduce
  @couch.design_new :view, @view, v
  self
end
data(opts={}) click to toggle source

@option opts [Boolean] :format :string, :object, (:map) @option opts [Boolean] :log puts url @option opts [Array] :keys make a post request @option opts [Array] :single_key calculates startkey and endkey @return [Map]

# File lib/tatami/couch.rb, line 267
def data opts={}
  opts_add = {log: opts.delete(:log), format: opts.delete(:format) }
  list = opts[:list] ? "_list/#{opts.delete(:list)}" : "_view"
  if opts[:keys]
    @couch.post_db "/_design/#{@design}/#{list}/#{@view}", opts, opts_add
  else
    @couch.get_db "/_design/#{@design}/#{list}/#{@view}?#{options_to_param opts}", opts_add
  end
end
key_values(opts={}) click to toggle source
# File lib/tatami/couch.rb, line 291
def key_values opts={}
  rows(opts).map{|r| [ r[:key],r[:value] ]}
end
keys(opts={}) click to toggle source
# File lib/tatami/couch.rb, line 299
def keys opts={}
  rows(opts).map{|d| d[:key]}
end
options_to_param(opts) click to toggle source

@param [] value @return [String] json

# File lib/tatami/couch.rb, line 247
def options_to_param opts
  if single_key=opts.delete(:singlekey)
    opts[:startkey] =  single_key
    opts[:endkey]   = (single_key + [{}] )
  end
  opts.map{|option,value| "#{option}=#{ JSON.generate([value])[1..-2] }"}.join('&')
end
reduce(opts={}) click to toggle source

@return [Map] rows as key => value

# File lib/tatami/couch.rb, line 309
def reduce opts={}
  rows(opts).each_with_object(Map.new)do |row,o|
    o[row[:key]] = row[:value]
  end
end
reduced(opts={}) click to toggle source

@return value of the first row

# File lib/tatami/couch.rb, line 304
def reduced opts={}
  data(opts).get :rows, 0, :value
end
reduced_value(*values) click to toggle source

special function for arrays as keys

# File lib/tatami/couch.rb, line 316
def reduced_value *values
  reduced :group_level => values.size, :startkey => values, :endkey => values.dup << {}
end
rows(opts={}) click to toggle source
# File lib/tatami/couch.rb, line 277
def rows opts={}
  data(opts)[:rows]
end
values(opts={}) click to toggle source
# File lib/tatami/couch.rb, line 295
def values opts={}
  rows(opts).map{|d| d[:value]}
end