Adding filter support to design docs

This commit is contained in:
Sam Lown 2011-04-27 13:30:08 +02:00
parent 8025c5354f
commit d62c2d1439
2 changed files with 25 additions and 0 deletions

View file

@ -65,6 +65,17 @@ module CouchRest
create_view_method(name) create_view_method(name)
end end
# 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
def create_view_method(name) def create_view_method(name)
model.class_eval <<-EOS, __FILE__, __LINE__ + 1 model.class_eval <<-EOS, __FILE__, __LINE__ + 1
def self.#{name}(opts = {}) def self.#{name}(opts = {})

View file

@ -87,6 +87,20 @@ describe "Design" do
end end
describe "#filter" do
before :each do
@object = @klass.new(DesignModel)
end
it "should add the provided function to the design doc" do
@object.filter(:important, "function(doc, req) { return doc.priority == 'high'; }")
DesignModel.design_doc['filters'].should_not be_empty
DesignModel.design_doc['filters']['important'].should_not be_blank
end
end
describe "#create_view_method" do describe "#create_view_method" do
before :each do before :each do
@object = @klass.new(DesignModel) @object = @klass.new(DesignModel)