Skip to content

Instantly share code, notes, and snippets.

@paneq
Created July 28, 2012 13:21
Show Gist options
  • Save paneq/3193376 to your computer and use it in GitHub Desktop.
Save paneq/3193376 to your computer and use it in GitHub Desktop.

No method_added hook implementation leads to proper working with include'ed methods.

Use prepend from Ruby 2.0. Decorators should return Modules as results of calling new:

class Memoize
  def self.new(method)
    m = Module.new
    m.define_method(method) do
      # memoization logic goes here.
      # use `super` to call original method.
    end
    m
  end
end
class ExternalService
def request
# ...
end
prepend Retry.new(3, :request)
module Response
def response
# ...
end
end
include Response
prepend Memoize.new(:response)
end
@michaelfairley
Copy link

Included methods should work fine in my lib (assuming that the decoration was done inside the module itself). Have you run into problems doing that, or are you focused only on the includer itself doing the decoration?

(I really like the simplicity of this version.)

@paneq
Copy link
Author

paneq commented Jul 31, 2012

@michaelfairley - I was just focused on the case when the includer is doing the decoration. The simplicity of this version is possible only due to powerful nature of prepend which is a new brother of our beloved include. I can't wait to see what Ruby community is going to do with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment