Sha256: d60f746b1a31fb25c4ec1e1178ba6f80d791c860cc02c5d22f5d16cbe27f5761

Contents?: true

Size: 934 Bytes

Versions: 4

Compression:

Stored size: 934 Bytes

Contents

module Ray
  module DSL
    # Used internally to call blocks registered with Ray::DSL::EventListener#on.
    class Handler
      def initialize(type, group, args, block)
        @type, @group, @args, @block = type, group, args, block
      end

      def match?(event)
        return false unless event.type == @type
        return match_args?(event.args) unless @args.empty?
        true
      end

      alias :=== :match?

      def call(event)
        if @block.arity == 0
          @block.call
        else
          @block.call(*event.args)
        end
      end

      attr_reader :type
      attr_reader :group
      attr_reader :args

      private
      def match_args?(args)
        return false if @args.size > args.size

        @args.each_with_index do |elem, i|
          other = args[i]
          return false unless (elem === args[i]) || (elem == args[i])
        end

        return true
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ray-0.2.1 lib/ray/dsl/handler.rb
ray-0.2.0 lib/ray/dsl/handler.rb
ray-0.1.1 lib/ray/dsl/handler.rb
ray-0.1.0 lib/ray/dsl/handler.rb