#5 ✓resolved
Star Trader

[Pull] Failure for multiple child classes hooking the same method

Reported by Star Trader | January 8th, 2009 @ 10:07 PM

This is a duplicate of Datamapper bug #759

require 'extlib'

class Person
  include Extlib::Hook
  attr_accessor :name

  def valid?
    !name.nil?
  end

end

class Man < Person
  before :valid?, :val
  def val
    self.name ||= "Anonymous Guy"
  end
end

class Woman < Person
  before :valid?, :val
  def val
    self.name ||= "Anonymous Gal"
  end
end


puts "A Person with no name fails:"
puts Person.new.valid? #=> false
puts "A Man with no name Succeeds:"
m = Man.new
puts m.valid? #=> true
puts "A Woman with no name should Succeeds but does not:"
w = Woman.new
puts w.valid? #=> false (!?)

When two or more classes inherit from a root class and try to hook into the same method all after the first fail to to call the hooked method.

A work-around is to define the hook and method in the root class (e.g. Person).

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Lightweight fast Ruby core extensions library for DataMapper and Merb.

People watching this ticket

Tags

Pages