extlib/hook breaks if hooked method is redefined

require "rubygems"
require "dm-core"
require "spec"

DataMapper.setup(:default, "sqlite3::memory:")

class Person

  include DataMapper::Resource

  property :id, Serial

  before :save, :shower
  after  :save, :shave

  def shower
    @showered = true
  end

  def shave
    @shaved = true
  end

  def showered?
    @showered
  end

  def shaved?
    @shaved
  end

end

describe "Hooking Resource#save" do

  before(:all) do
    DataMapper.auto_migrate!
  end

  describe "when the hooked method DOES CALL super" do

    before(:each) do
      class Person
        def save(context = nil)
          super(context)
        end
      end
    end

    it_should_behave_like "hooks"

  end

  describe "when the hooked method DOES NOT CALL super" do

    before(:each) do
      class Person
        def save(context = nil)
          true
        end
      end
    end

    it_should_behave_like "hooks"

  end

  describe "hooks", :shared => true do

    it "should preserve established before and after hooks" do
      p = Person.new
      p.should_not be_showered
      p.should_not be_shaved
      p.save
      p.should be_showered
      p.should be_shaved
    end

  end

end

# mungo:Desktop snusnu$ spec -c -f -s freak.rb
#
# Hooking Resource#save when the hooked method DOES CALL super
# - should preserve established before hooks
#
# Hooking Resource#save when the hooked method DOES NOT CALL super
# - should preserve established before hooks (FAILED - 1)
#
# 1)
# 'Hooking Resource#save when the hooked method DOES NOT CALL super should preserve established before hooks' FAILED
# expected showered? to return true, got nil
# ./freak.rb:76:
#
# Finished in 0.007412 seconds
#
# 2 examples, 1 failure


Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>