Sha256: 9b7e1e0126d06e61e34746432d2cfcfd7f57804a3fb9f268500fd1dc5bd22470

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

#!/usr/bin/env ruby
# Example demonstrating "around" advice for method_missing. This is a technique for
# avoiding collisions when different toolkits want to override method_missing in the
# same classes, e.g., Object. Using around advice as shown allows a toolkit to add 
# custom behavior while invoking the "native" method_missing to handle unrecognized
# method calls.
# Note that it is essential to use around advice, not before or after advice, because
# neither can prevent the call to the "wrapped" method_missing, which is presumably
# not what you want.
# In this (contrived) example, an Echo class uses method_missing to simply echo
# the method name and arguments. An aspect is used to intercept any calls to a 
# fictitious "log" method and handle those in a different way.

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'aquarium'

module Aquarium
  class Echo
    def method_missing sym, *args
      p "Echoing: #{sym.to_s}: #{args.join(" ")}"
    end
    def respond_to? sym, include_private = false
      true
    end
  end
end

p "Before advising Echo:"
echo1 = Aquarium::Echo.new
echo1.say "hello", "world!"
echo1.log "something", "interesting..."
echo1.shout "theater", "in", "a", "crowded", "firehouse!"

Aquarium::Aspects::Aspect.new :around, :calls_to => :method_missing, :for_type => Aquarium::Echo, do |join_point, obj, sym, *args|
  if sym == :log 
    p "--- Sending to log: #{args.join(" ")}" 
  else
    join_point.proceed
  end
end

p "After advising Echo:"
echo2 = Aquarium::Echo.new
echo2.say "hello", "world!"
echo2.log "something", "interesting..."
echo2.shout "theater", "in", "a", "crowded", "firehouse!"

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aquarium-0.4.0 examples/method_missing_example.rb
aquarium-0.3.0 examples/method_missing_example.rb
aquarium-0.3.1 examples/method_missing_example.rb
aquarium-0.4.1 examples/method_missing_example.rb