2009-03-27 14:42:49 +01:00
|
|
|
module CouchRest
|
2010-06-20 22:01:11 +02:00
|
|
|
module Model
|
2009-03-27 14:42:49 +01:00
|
|
|
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
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# Base
|
2009-03-27 14:42:49 +01:00
|
|
|
|
|
|
|
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 || {}
|
2010-06-18 19:00:20 +02:00
|
|
|
return view(m, query, *args, &block)
|
|
|
|
elsif m.to_s =~ /^find_(by_.+)/
|
|
|
|
view_name = $1
|
|
|
|
if has_view?(view_name)
|
2010-06-18 21:11:02 +02:00
|
|
|
return first_from_view(view_name, *args)
|
2010-06-18 19:00:20 +02:00
|
|
|
end
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
2010-06-18 19:00:20 +02:00
|
|
|
super
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# DocumentQueries
|
2009-03-27 14:42:49 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-12-31 21:59:57 +01:00
|
|
|
def last(opts = {})
|
|
|
|
doc = @klass.last({:database => @database}.merge(opts))
|
|
|
|
doc.database = @database if doc && doc.respond_to?(:database)
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2009-03-27 14:42:49 +01:00
|
|
|
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
|
2010-06-18 19:00:20 +02:00
|
|
|
alias :find :get
|
2011-05-23 19:02:08 +02:00
|
|
|
|
|
|
|
def get!(id)
|
|
|
|
doc = @klass.get!(id, @database)
|
|
|
|
doc.database = @database if doc && doc.respond_to?(:database)
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
alias :find! :get!
|
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# Views
|
2009-03-27 14:42:49 +01:00
|
|
|
|
|
|
|
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-06-18 20:07:34 +02:00
|
|
|
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)
|
2010-06-18 19:00:20 +02:00
|
|
|
doc.database = @database if doc && doc.respond_to?(:database)
|
|
|
|
doc
|
|
|
|
end
|
2010-04-07 23:00:51 +02:00
|
|
|
|
2010-06-20 22:01:11 +02:00
|
|
|
# DesignDoc
|
2009-03-27 14:42:49 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2009-03-27 14:42:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|