Class: Couch::View

Inherits:
Document show all
Defined in:
lib/couch/view.rb

Direct Known Subclasses

DocView

Constant Summary collapse

DUMMY_NAME =
"foo"

Constants included from Log

Log::QUIET

Instance Attribute Summary

Attributes inherited from Document

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

async, #changed?, #database, database, #destroy, #dup, #id, #id=, #initialize, #inspect, instantiate, #new_record?, #reload, #rev, #save, #to_hash, #to_json, #type, #update

Methods included from DocExtensions

condition_keys, #count

Constructor Details

This class inherits a constructor from Couch::Document

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Couch::Document

Class Method Details

.create(name, opts) ⇒ Object

-- create a view --------------------------------------------------



10
11
12
13
14
15
16
17
18
19
# File 'lib/couch/view.rb', line 10

def self.create(name, opts)
  map = opts[:map] || raise("Missing map function")
  reduce = opts[:reduce]

  super :id => "_design/#{name}",
    :views => { DUMMY_NAME => { 
      :map => map,
      :reduce => reduce
    } }
end

.find(name) ⇒ Object



4
5
6
# File 'lib/couch/view.rb', line 4

def self.find(name)
  super("_design/#{name}")
end

Instance Method Details

#all(opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/couch/view.rb', line 53

def all(opts = {})
  opts = opts.to_a.inject({}) do |h, (k,v)|
    h.update k => Yajl::Encoder.encode(v)
  end 
  
  r = Couch.database.get CGI.url_for("#{id}/_view/#{DUMMY_NAME}", opts)
  r[:rows].
    define_instance_method(:offset, r[:offset]).
    define_instance_method(:total_rows, r[:total_rows])
end

#mapObject



21
22
23
# File 'lib/couch/view.rb', line 21

def map
  view[:map]
end

#map=(map) ⇒ Object



25
26
27
28
29
30
# File 'lib/couch/view.rb', line 25

def map=(map)
  update :views => { DUMMY_NAME => { 
    :map => map,
    :reduce => reduce
  } }
end

#reduceObject



32
33
34
# File 'lib/couch/view.rb', line 32

def reduce
  view[:reduce]
end

#reduce=(reduce) ⇒ Object



36
37
38
39
40
41
# File 'lib/couch/view.rb', line 36

def reduce=(reduce)
  update :views => { DUMMY_NAME => { 
    :map => map,
    :reduce => reduce
  } }
end

#total_rows(opts = nil) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/couch/view.rb', line 64

def total_rows(opts = nil)
  if opts.nil?
    r = Couch.database.get CGI.url_for("#{id}/_view/#{DUMMY_NAME}")
    r[:total_rows]
  else
    all(opts).length
  end
end