Skip to content

Including the Hooks module a second time (e.g. by including a module that also includes it) wipes out previously defined hooks #36

@mhgoldman

Description

@mhgoldman
module SomeModule
  extend ActiveSupport::Concern
  included do
    include Hooks
    define_hooks :myhook1
  end
end

class MyClass
  include Hooks
  define_hooks :myhook2

  include SomeModule

  def run_myhook1
    run_hook :myhook1
  end

  def run_myhook2
    run_hook :myhook2
  end  
end

MyClass._hooks # => {:myhook1=>[]}
MyClass.new.run_myhook1 # => Works fine
MyClass.new.run_myhook2 # => NoMethodError: undefined method `run' for nil:NilClass

This seems to be because the following code in lib/hooks.rb runs on every include:

# lib/hooks.rb
module Hooks
  def self.included(base)
    base.class_eval do
      extend Uber::InheritableAttr
      extend ClassMethods
      inheritable_attr :_hooks
      self._hooks= HookSet.new # <-- this wipes out existing hooks
    end
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions