2009-03-27 14:42:49 +01:00
|
|
|
module CouchRest
|
|
|
|
module Mixins
|
|
|
|
module ClassProxy
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
# Return a proxy object which represents a model class on a
|
|
|
|
# chosen database instance. This allows you to DRY operations
|
|
|
|
# where a database is chosen dynamically.
|
|
|
|
#
|
|
|
|
# ==== Example:
|
|
|
|
#
|
|
|
|
# db = CouchRest::Database.new(...)
|
|
|
|
# articles = Article.on(db)
|
|
|
|
#
|
|
|
|
# articles.all { ... }
|
|
|
|
# articles.by_title { ... }
|
|
|
|
#
|
|
|
|
# u = articles.get("someid")
|
|
|
|
#
|
|
|
|
# u = articles.new(:title => "I like plankton")
|
|
|
|
# u.save # saved on the correct database
|
|
|
|
|
|
|
|
def on(database)
|
|
|
|
Proxy.new(self, database)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Proxy #:nodoc:
|
|
|
|
def initialize(klass, database)
|
|
|
|
@klass = klass
|
|
|
|
@database = database
|
|
|
|
end
|
|
|
|
|
|
|
|
# ExtendedDocument
|
|
|
|
|
|
|
|
def new(*args)
|
|
|
|
doc = @klass.new(*args)
|
|
|
|
doc.database = @database
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(m, *args, &block)
|
|
|
|
if has_view?(m)
|
|
|
|
query = args.shift || {}
|
|
|
|
view(m, query, *args, &block)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Mixins::DocumentQueries
|
|
|
|
|
|
|
|
def all(opts = {}, &block)
|
2009-09-09 09:08:59 +02:00
|
|
|
docs = @klass.all({:database => @database}.merge(opts), &block)
|
|
|
|
docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
|
|
|
|
docs
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
|
2009-07-21 14:14:58 +02:00
|
|
|
def count(opts = {}, &block)
|
2009-07-21 14:36:09 +02:00
|
|
|
@klass.all({:database => @database, :raw => true, :limit => 0}.merge(opts), &block)['total_rows']
|
2009-07-21 14:14:58 +02:00
|
|
|
end
|
|
|
|
|
2009-03-27 14:42:49 +01:00
|
|
|
def first(opts = {})
|
2009-09-09 09:08:59 +02:00
|
|
|
doc = @klass.first({:database => @database}.merge(opts))
|
|
|
|
doc.database = @database if doc && doc.respond_to?(:database)
|
|
|
|
doc
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get(id)
|
2009-09-09 09:08:59 +02:00
|
|
|
doc = @klass.get(id, @database)
|
|
|
|
doc.database = @database if doc && doc.respond_to?(:database)
|
|
|
|
doc
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Mixins::Views
|
|
|
|
|
|
|
|
def has_view?(view)
|
|
|
|
@klass.has_view?(view)
|
|
|
|
end
|
|
|
|
|
|
|
|
def view(name, query={}, &block)
|
2009-09-09 09:08:59 +02:00
|
|
|
docs = @klass.view(name, {:database => @database}.merge(query), &block)
|
|
|
|
docs.each { |doc| doc.database = @database if doc.respond_to?(:database) } if docs
|
|
|
|
docs
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
|
2010-04-07 23:00:51 +02:00
|
|
|
|
2009-03-27 14:42:49 +01:00
|
|
|
# Mixins::DesignDoc
|
|
|
|
|
|
|
|
def design_doc
|
|
|
|
@klass.design_doc
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_design_doc
|
2010-04-06 19:51:17 +02:00
|
|
|
@klass.refresh_design_doc(@database)
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def save_design_doc
|
2010-04-06 19:51:17 +02:00
|
|
|
@klass.save_design_doc(@database)
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
2010-04-07 23:00:51 +02:00
|
|
|
|
|
|
|
# DEPRICATED
|
|
|
|
def all_design_doc_versions
|
|
|
|
@klass.all_design_doc_versions(@database)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stored_design_doc
|
|
|
|
@klass.stored_design_doc(@database)
|
|
|
|
end
|
|
|
|
alias :model_design_doc :stored_design_doc
|
|
|
|
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|