2011-02-05 22:22:07 +01:00
|
|
|
|
|
|
|
#### NOTE Work in progress! Not yet used!
|
|
|
|
|
|
|
|
module CouchRest
|
|
|
|
module Model
|
|
|
|
|
|
|
|
# A design block in CouchRest Model groups together the functionality of CouchDB's
|
|
|
|
# design documents in a simple block definition.
|
|
|
|
#
|
|
|
|
# class Person < CouchRest::Model::Base
|
|
|
|
# property :name
|
|
|
|
# timestamps!
|
|
|
|
#
|
|
|
|
# design do
|
|
|
|
# view :by_name
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2011-02-06 04:32:23 +01:00
|
|
|
module Designs
|
2011-02-05 22:22:07 +01:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
2011-02-27 19:06:37 +01:00
|
|
|
# Add views and other design document features
|
|
|
|
# to the current model.
|
2011-02-05 22:22:07 +01:00
|
|
|
def design(*args, &block)
|
2011-02-06 02:52:09 +01:00
|
|
|
mapper = DesignMapper.new(self)
|
2011-02-06 17:17:14 +01:00
|
|
|
mapper.create_view_method(:all)
|
2011-02-05 22:22:07 +01:00
|
|
|
|
2011-03-13 13:39:26 +01:00
|
|
|
mapper.instance_eval(&block) if block_given?
|
2011-02-05 22:22:07 +01:00
|
|
|
end
|
|
|
|
|
2011-02-27 19:06:37 +01:00
|
|
|
# Override the default page pagination value:
|
|
|
|
#
|
|
|
|
# class Person < CouchRest::Model::Base
|
|
|
|
# paginates_per 10
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
def paginates_per(val)
|
|
|
|
@_default_per_page = val
|
|
|
|
end
|
|
|
|
|
|
|
|
# The models number of documents to return
|
|
|
|
# by default when performing pagination.
|
|
|
|
# Returns 25 unless explicitly overridden via <tt>paginates_per</tt>
|
|
|
|
def default_per_page
|
|
|
|
@_default_per_page || 25
|
|
|
|
end
|
|
|
|
|
2011-02-05 22:22:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2011-02-06 02:52:09 +01:00
|
|
|
class DesignMapper
|
2011-02-05 22:22:07 +01:00
|
|
|
|
2011-02-06 02:52:09 +01:00
|
|
|
attr_accessor :model
|
2011-02-05 22:22:07 +01:00
|
|
|
|
2011-02-06 02:52:09 +01:00
|
|
|
def initialize(model)
|
|
|
|
self.model = model
|
|
|
|
end
|
2011-02-05 22:22:07 +01:00
|
|
|
|
2011-11-16 23:30:46 +01:00
|
|
|
# Generate a method that will provide a new View instance when
|
|
|
|
# requested. This will also define the view in CouchDB unless
|
|
|
|
# auto_update_design_doc is disabled.
|
2011-02-06 02:52:09 +01:00
|
|
|
def view(name, opts = {})
|
2011-11-16 23:30:46 +01:00
|
|
|
View.create(model, name, opts) if model.auto_update_design_doc
|
2011-02-06 17:17:14 +01:00
|
|
|
create_view_method(name)
|
|
|
|
end
|
|
|
|
|
2011-04-27 13:30:08 +02:00
|
|
|
# Really simple design function that allows a filter
|
|
|
|
# to be added. Filters are simple functions used when listening
|
|
|
|
# to the _changes feed.
|
|
|
|
#
|
|
|
|
# No methods are created here, the design is simply updated.
|
|
|
|
# See the CouchDB API for more information on how to use this.
|
|
|
|
def filter(name, function)
|
|
|
|
filters = (self.model.design_doc['filters'] ||= {})
|
|
|
|
filters[name.to_s] = function
|
|
|
|
end
|
|
|
|
|
2011-02-06 17:17:14 +01:00
|
|
|
def create_view_method(name)
|
2011-02-06 02:52:09 +01:00
|
|
|
model.class_eval <<-EOS, __FILE__, __LINE__ + 1
|
|
|
|
def self.#{name}(opts = {})
|
2011-02-06 04:32:23 +01:00
|
|
|
CouchRest::Model::Designs::View.new(self, opts, '#{name}')
|
2011-02-06 02:52:09 +01:00
|
|
|
end
|
|
|
|
EOS
|
2011-02-05 22:22:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|