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.1.1 lib/abstractivator/event.rb