Sha256: 3206da2f96b8c588b75efb1be7cbef33343401d0d194a6310d64170f778ffab1

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module Aggro
  # Public: Adds a DSL defining event handlers.
  module EventDSL
    extend ActiveSupport::Concern

    def handles_event?(event_name, namespace = nil)
      self.class.handles_event?(event_name, namespace) || \
        namespace?(namespace) && event_methods[namespace].include?(event_name)
    end

    private

    def event_methods
      @event_methods ||= {}
    end

    def namespace?(namespace)
      !event_methods[namespace].nil?
    end

    class_methods do
      def events(namespace = nil, &block)
        new_methods = BlockHelper.method_definitions(&block)

        event_methods[namespace] ||= Set.new
        new_methods.each { |method| event_methods[namespace] << method }

        class_eval(&block)

        class_eval do
          new_methods.each do |method|
            alias_method "#{namespace}_#{method}", method
            remove_method method
          end
        end
      end

      def handles_event?(event_name, namespace = nil)
        namespace?(namespace) && event_methods[namespace].include?(event_name)
      end

      private

      def event_methods
        @event_methods ||= {}
      end

      def namespace?(namespace)
        !event_methods[namespace].nil?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aggro-0.0.4 lib/aggro/event_dsl.rb
aggro-0.0.3 lib/aggro/event_dsl.rb
aggro-0.0.2 lib/aggro/event_dsl.rb