From db7829e9966ff61cd1ce529bb0bd0ed91837af27 Mon Sep 17 00:00:00 2001 From: Peter Gumeson Date: Sun, 7 Jun 2009 18:46:30 -0700 Subject: [PATCH] Callbacks now take multiple callback methods --- README.md | 4 +- lib/couchrest/mixins/callbacks.rb | 14 ++++++- spec/couchrest/more/extended_doc_spec.rb | 47 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 62baa0c..a169b70 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,8 @@ CouchRest uses a mixin you can find in lib/mixins/callbacks which is extracted f Or the new shorter version: - before_save :before_method - after_save :after_method, :if => :condition + before_save :before_method, :another_method + after_save :after_method, :another_method, :if => :condition around_save {|r| stuff; yield; stuff } Check the mixin or the ExtendedDocument class to see how to implement your own callbacks. diff --git a/lib/couchrest/mixins/callbacks.rb b/lib/couchrest/mixins/callbacks.rb index 5703762..9aa02a8 100644 --- a/lib/couchrest/mixins/callbacks.rb +++ b/lib/couchrest/mixins/callbacks.rb @@ -533,13 +533,23 @@ module CouchRest # set_callback(:save, :before) becomes before_save [:before, :after, :around].each do |filter| self.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def self.#{filter}_#{symbol}(meth=nil, &blk) - set_callback(:#{symbol}, :#{filter}, meth||blk) + def self.#{filter}_#{symbol}(*symbols, &blk) + _alias_callbacks(symbols, blk) do |callback, options| + set_callback(:#{symbol}, :#{filter}, callback, options) + end end RUBY_EVAL end end end + + def _alias_callbacks(callbacks, block) + options = callbacks.last.is_a?(Hash) ? callbacks.pop : {} + callbacks.push(block) if block + callbacks.each do |callback| + yield callback, options + end + end end end end diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb index 09513fa..7ab5746 100644 --- a/spec/couchrest/more/extended_doc_spec.rb +++ b/spec/couchrest/more/extended_doc_spec.rb @@ -53,6 +53,32 @@ describe "ExtendedDocument" do after_update do |object| object.run_after_update = true end + + property :run_one + property :run_two + property :run_three + + before_save :run_one_method, :run_two_method do |object| + object.run_three = true + end + def run_one_method + self.run_one = true + end + def run_two_method + self.run_two = true + end + + attr_accessor :run_it + property :conditional_one + property :conditional_two + + before_save :conditional_one_method, :conditional_two_method, :if => proc { self.run_it } + def conditional_one_method + self.conditional_one = true + end + def conditional_two_method + self.conditional_two = true + end end class WithTemplateAndUniqueID < CouchRest::ExtendedDocument @@ -532,6 +558,27 @@ describe "ExtendedDocument" do @doc.save.should be_true @doc.run_after_save.should be_true end + it "should run the grouped callbacks before saving" do + @doc.run_one.should be_nil + @doc.run_two.should be_nil + @doc.run_three.should be_nil + @doc.save.should be_true + @doc.run_one.should be_true + @doc.run_two.should be_true + @doc.run_three.should be_true + end + it "should not run conditional callbacks" do + @doc.run_it = false + @doc.save.should be_true + @doc.conditional_one.should be_nil + @doc.conditional_two.should be_nil + end + it "should run conditional callbacks" do + @doc.run_it = true + @doc.save.should be_true + @doc.conditional_one.should be_true + @doc.conditional_two.should be_true + end end describe "create" do it "should run the before save filter when creating" do