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



113
114
115
# File 'lib/couchrest/model/class_proxy.rb', line 113

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



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

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

#has_view?(view) ⇒ Boolean

Views

Returns:

  • (Boolean)


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

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



117
118
119
# File 'lib/couchrest/model/class_proxy.rb', line 117

def refresh_design_doc
  @klass.refresh_design_doc(@database)
end

#save_design_docObject



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

def save_design_doc
  @klass.save_design_doc(@database)
end

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



97
98
99
100
101
# File 'lib/couchrest/model/class_proxy.rb', line 97

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