Sha256: 2e275b25210880a8e99523900b0da94d8e9bf061a63afaf868160e9e41c1f756

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Ray
  module DSL
    # This is another class kept by your greedy EventRunner. You don't need
    # to know it exist. When you say on :foo do something end a handler is
    # created, so that your EventRunner can see if you are interested in
    # a specific event.
    class Handler
      def initialize(type, args, block)
        @type, @args, @block = type, args, block

        if desc = Ray.description_for_event(@type)
          desc.each_with_index do |type, i|
            next if @args[i].is_a? Matcher
            next if @args[i].is_a? Regexp

            begin
              @args[i] = Ray.convert(@args[i], type)
            rescue TypeError
              return
            end
          end
        end
      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 :args

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

        @args.each_with_index do |elem, i|
          other = args[i]

          case elem
          when Ray::DSL::Matcher
            return false unless elem.match?(other)
          when Regexp
            if other.is_a? Regexp
              return false unless elem == other
            elsif other.is_a? String
              return false unless elem =~ other
            else
              return false
            end
          else
            return false unless elem == other
          end
        end

        return true
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ray-0.0.0.pre2 lib/ray/dsl/handler.rb