Sha256: cd571e5e030e2d07e43f7dcc388300e41b5f2216a8030c6d832440455306e533

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module QED

  require 'qed/advice/events'
  require 'qed/advice/patterns'

  # = Advice
  #
  # This class tracks advice defined by demo scripts
  # and helpers. It is instantiated in Scope, so that
  # the advice methods will have access to the same
  # local binding and the demo scripts themselves.
  #
  class Advice

    attr :patterns

    attr :events

    def initialize
      @patterns = Patterns.new
      @events   = Events.new
    end

    def call(type, *args)
      case type
      when :when
        @patterns.call(*args)
      else
        @events.call(type, *args)
      end
    end
  end

  #
  module Advisable

    def __advice__
      @__advice__ ||= Advice.new
    end

    def When(pattern, &procedure)
      case pattern
      when Symbol
        __advice__.events.add(:"#{pattern}", &procedure)
      else
        __advice__.patterns.add(pattern, &procedure)
      end
    end

    def Before(type=:code, &procedure)
      __advice__.events.add(:"before_#{type}", &procedure)
    end

    def After(type=:code, &procedure)
      __advice__.events.add(:"after_#{type}", &procedure)
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qed-2.1.1 lib/qed/advice.rb
qed-2.0.0 lib/qed/advice.rb
qed-2.1.0 lib/qed/advice.rb