Class: Tatami::Couch::View

Inherits:
Object
  • Object
show all
Defined in:
lib/tatami/couch.rb

Instance Method Summary collapse

Constructor Details

#initialize(couch, name) ⇒ View

Returns a new instance of View.



238
239
240
241
242
243
# 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

Instance Method Details

#create(map, reduce = nil) ⇒ View

Parameters:

  • map (String)

Returns:



284
285
286
287
288
289
# 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 = {}) ⇒ Map

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :format (Boolean)

    :string, :object, (:map)

  • :log (Boolean)

    puts url

  • :keys (Array)

    make a post request

  • :single_key (Array)

    calculates startkey and endkey

Returns:

  • (Map)


267
268
269
270
271
272
273
274
275
# 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 = {}) ⇒ Object



291
292
293
# File 'lib/tatami/couch.rb', line 291

def key_values opts={}
  rows(opts).map{|r| [ r[:key],r[:value] ]}
end

#keys(opts = {}) ⇒ Object



299
300
301
# File 'lib/tatami/couch.rb', line 299

def keys opts={}
  rows(opts).map{|d| d[:key]}
end

#options_to_param(opts) ⇒ String

Returns json.

Parameters:

  • []

    value

Returns:

  • (String)

    json



247
248
249
250
251
252
253
# 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 = {}) ⇒ Map

Returns rows as key => value.

Returns:

  • (Map)

    rows as key => value



309
310
311
312
313
# 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 = {}) ⇒ Object

Returns value of the first row.

Returns:

  • value of the first row



304
305
306
# File 'lib/tatami/couch.rb', line 304

def reduced opts={}
  data(opts).get :rows, 0, :value
end

#reduced_value(*values) ⇒ Object

special function for arrays as keys



316
317
318
# File 'lib/tatami/couch.rb', line 316

def reduced_value *values
  reduced :group_level => values.size, :startkey => values, :endkey => values.dup << {}
end

#rows(opts = {}) ⇒ Object



277
278
279
# File 'lib/tatami/couch.rb', line 277

def rows opts={}
  data(opts)[:rows]
end

#values(opts = {}) ⇒ Object



295
296
297
# File 'lib/tatami/couch.rb', line 295

def values opts={}
  rows(opts).map{|d| d[:value]}
end