Created
April 13, 2013 11:12
-
-
Save nbfritz/5377983 to your computer and use it in GitHub Desktop.
Scary awesome.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Tapper | |
| attr_reader :eavesdroppers | |
| def initialize(source, *eavesdroppers) | |
| @source = source | |
| @eavesdroppers = eavesdroppers | |
| end | |
| def method_missing(method, *args) | |
| if @source.respond_to?(method) | |
| valid_eavesdroppers = @eavesdroppers.find_all {|t| t.respond_to?(method) } | |
| define_singleton_method(method) do |*args| | |
| source_return = @source.send(method, *args) | |
| begin | |
| valid_eavesdroppers.each {|t| t.send(method, *args) } | |
| rescue | |
| true # swallow errors from eavesdroppers | |
| end | |
| return source_return | |
| end | |
| send(method, *args) | |
| end | |
| end | |
| def eavesdropper | |
| @eavesdroppers.first | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment