Sha256: 3c3c7de6a5ff7ecb32a9b94a5992efe2be5a34ee520c54b4d5a02e4ccbd2af12

Contents?: true

Size: 797 Bytes

Versions: 21

Compression:

Stored size: 797 Bytes

Contents

module Abstractivator
  module Event
    def self.included(base)
      base.extend(ClassMethods)
    end
    module ClassMethods
      def event(*names)
        names.each do |name|
          define_method("on_#{name}") do |&block|
            __event_hooks[name] << block
            proc { __event_hooks[name].delete(block) }
          end

          define_method("do_#{name}") do |*args, **kws, &block|
            __event_hooks[name].each do |hook|
              if hook.parameters.map(&:first).include?(:key)
                hook.call(*args, **kws, &block)
              else
                hook.call(*args, &block)
              end
            end
          end
        end
      end
    end

    def __event_hooks
      @__event_hooks ||= Hash.new { |h, k| h[k] = [] }
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
abstractivator-0.20.0 lib/abstractivator/event.rb
abstractivator-0.19.0 lib/abstractivator/event.rb
abstractivator-0.18.0 lib/abstractivator/event.rb
abstractivator-0.17.0 lib/abstractivator/event.rb
abstractivator-0.16.0 lib/abstractivator/event.rb
abstractivator-0.15.0 lib/abstractivator/event.rb
abstractivator-0.14.0 lib/abstractivator/event.rb
abstractivator-0.13.0 lib/abstractivator/event.rb
abstractivator-0.12.0 lib/abstractivator/event.rb
abstractivator-0.11.0 lib/abstractivator/event.rb
abstractivator-0.10.0 lib/abstractivator/event.rb
abstractivator-0.8.0 lib/abstractivator/event.rb
abstractivator-0.7.0 lib/abstractivator/event.rb
abstractivator-0.6.0 lib/abstractivator/event.rb
abstractivator-0.5.0 lib/abstractivator/event.rb
abstractivator-0.4.1 lib/abstractivator/event.rb
abstractivator-0.4.0 lib/abstractivator/event.rb
abstractivator-0.3.1 lib/abstractivator/event.rb
abstractivator-0.3.0 lib/abstractivator/event.rb
abstractivator-0.2.0 lib/abstractivator/event.rb