Class: CouchRest::Model::ClassProxy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/model/class_proxy.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(klass, database) ⇒ Proxy

Returns a new instance of Proxy.



34
35
36
37
# File 'lib/couchrest/model/class_proxy.rb', line 34

def initialize(klass, database)
  @klass = klass
  @database = database
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/couchrest/model/class_proxy.rb', line 47

def method_missing(m, *args, &block)
  if has_view?(m)
    query = args.shift || {}
    return view(m, query, *args, &block)
  elsif m.to_s =~ /^find_(by_.+)/
    view_name = $1
    if has_view?(view_name)
      return first_from_view(view_name, *args) 
    end
  end
  super
end

Instance Method Details

#all(opts = {}, &block) ⇒ Object

DocumentQueries



62
63
64
65
66
# File 'lib/couchrest/model/class_proxy.rb', line 62

def all(opts = {}, &block)
  docs = @klass.all({:database => @database}.merge(opts), &block)
  docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
  docs
end

#count(opts = {}, &block) ⇒ Object



68
69
70
# File 'lib/couchrest/model/class_proxy.rb', line 68

def count(opts = {}, &block)
  @klass.all({:database => @database, :raw => true, :limit => 0}.merge(opts), &block)['total_rows']
end

#design_docObject

DesignDoc



120
121
122
# File 'lib/couchrest/model/class_proxy.rb', line 120

def design_doc
  @klass.design_doc
end

#first(opts = {}) ⇒ Object



72
73
74
75
76
# File 'lib/couchrest/model/class_proxy.rb', line 72

def first(opts = {})
  doc = @klass.first({:database => @database}.merge(opts))
  doc.database = @database if doc && doc.respond_to?(:database)
  doc
end

#first_from_view(name, *args) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/couchrest/model/class_proxy.rb', line 110

def first_from_view(name, *args)
  # add to first hash available, or add to end
  (args.last.is_a?(Hash) ? args.last : (args << {}).last)[:database] = @database
  doc = @klass.first_from_view(name, *args)
  doc.database = @database if doc && doc.respond_to?(:database)
  doc
end

#get(id) ⇒ Object Also known as: find



84
85
86
87
88
# File 'lib/couchrest/model/class_proxy.rb', line 84

def get(id)
  doc = @klass.get(id, @database)
  doc.database = @database if doc && doc.respond_to?(:database)
  doc
end

#get!(id) ⇒ Object Also known as: find!



91
92
93
94
95
# File 'lib/couchrest/model/class_proxy.rb', line 91

def get!(id)
  doc = @klass.get!(id, @database)
  doc.database = @database if doc && doc.respond_to?(:database)
  doc
end

#has_view?(view) ⇒ Boolean

Views

Returns:

  • (Boolean)


100
101
102
# File 'lib/couchrest/model/class_proxy.rb', line 100

def has_view?(view)
  @klass.has_view?(view)
end

#last(opts = {}) ⇒ Object



78
79
80
81
82
# File 'lib/couchrest/model/class_proxy.rb', line 78

def last(opts = {})
  doc = @klass.last({:database => @database}.merge(opts))
  doc.database = @database if doc && doc.respond_to?(:database)
  doc
end

#new(*args) ⇒ Object

Base



41
42
43
44
45
# File 'lib/couchrest/model/class_proxy.rb', line 41

def new(*args)
  doc = @klass.new(*args)
  doc.database = @database
  doc
end

#refresh_design_docObject



124
125
126
# File 'lib/couchrest/model/class_proxy.rb', line 124

def refresh_design_doc
  @klass.refresh_design_doc(@database)
end

#save_design_docObject



128
129
130
# File 'lib/couchrest/model/class_proxy.rb', line 128

def save_design_doc
  @klass.save_design_doc(@database)
end

#view(name, query = {}, &block) ⇒ Object



104
105
106
107
108
# File 'lib/couchrest/model/class_proxy.rb', line 104

def view(name, query={}, &block)
  docs = @klass.view(name, {:database => @database}.merge(query), &block)
  docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
  docs
end