Skip to content
Browse files

hook (callback) methods are called even when they are private

  • Loading branch information...
1 parent 0c7d14e commit d211dcb95d195de65bfb56bf6b85a258c67fb071 @cordawyn cordawyn committed Sep 17, 2011
Showing with 62 additions and 9 deletions.
  1. +8 −8 lib/spira/resource/instance_methods.rb
  2. +54 −1 spec/hooks.spec
View
16 lib/spira/resource/instance_methods.rb
@@ -139,7 +139,7 @@ def _destroy_attributes(attributes, opts = {})
# @object.destroy!(:completely)
# @return [true, false] Whether or not the destroy was successful
def destroy!(what = nil)
- before_destroy if self.respond_to?(:before_destroy)
+ before_destroy if self.respond_to?(:before_destroy, true)
result = case what
when nil
_destroy_attributes(attributes, :destroy_type => true) != nil
@@ -150,7 +150,7 @@ def destroy!(what = nil)
when :completely
destroy!(:subject) && destroy!(:object)
end
- after_destroy if self.respond_to?(:after_destroy) if result
+ after_destroy if self.respond_to?(:after_destroy, true) if result
result
end
@@ -159,18 +159,18 @@ def destroy!(what = nil)
#
# @return [self] self
def save!
- existed = (self.respond_to?(:before_create) || self.respond_to?(:after_create)) && !self.type.nil? && exists?
- before_create if self.respond_to?(:before_create) && !self.type.nil? && !existed
- before_save if self.respond_to?(:before_save)
+ existed = (self.respond_to?(:before_create, true) || self.respond_to?(:after_create, true)) && !self.type.nil? && exists?
+ before_create if self.respond_to?(:before_create, true) && !self.type.nil? && !existed
+ before_save if self.respond_to?(:before_save, true)
# we use the non-raising validate and check it to make a slightly different error message. worth it?...
case validate
when true
_update!
when false
raise(ValidationError, "Could not save #{self.inspect} due to validation errors: " + errors.each.join(';'))
end
- after_create if self.respond_to?(:after_create) && !self.type.nil? && !existed
- after_save if self.respond_to?(:after_save)
+ after_create if self.respond_to?(:after_create, true) && !self.type.nil? && !existed
+ after_save if self.respond_to?(:after_save, true)
self
end
@@ -192,7 +192,7 @@ def update(properties)
properties.each do |property, value|
attribute_set(property, value)
end
- after_update if self.respond_to?(:after_update)
+ after_update if self.respond_to?(:after_update, true)
self
end
View
55 spec/hooks.spec
@@ -204,8 +204,61 @@ describe 'Spira resources' do
# This one makes sure that after_destory got called at all
@repository.should_not have_predicate RDF::FOAF.other
end
+ end
+ context "when the hook methods are private" do
+ before :all do
+ class ::PrivateHookTest < ::HookTest
+ type FOAF.bc_test
- end
+ def counter
+ @counter ||= {}
+ end
+ private
+
+ def before_create
+ self.counter[__method__] = true
+ end
+
+ def before_save
+ self.counter[__method__] = true
+ end
+
+ def before_destroy
+ self.counter[__method__] = true
+ end
+
+ def after_create
+ self.counter[__method__] = true
+ end
+
+ def after_save
+ self.counter[__method__] = true
+ end
+
+ def after_update
+ self.counter[__method__] = true
+ end
+
+ def after_destroy
+ self.counter[__method__] = true
+ end
+ end
+ end
+
+ before :each do
+ @repository << RDF::Statement.new(@subject, RDF.type, RDF::FOAF.bc_test)
+ end
+
+ it "should call the hook methods" do
+ subject = RDF::URI.new('http://example.org/test1').as(::PrivateHookTest)
+
+ subject.save!
+ subject.update!(:name => "Jay")
+ subject.destroy!
+
+ subject.counter.keys.count.should eql(7)
+ end
+ end
end

0 comments on commit d211dcb

Please sign in to comment.
Something went wrong with that request. Please try again.