Socket
Book a DemoInstallSign in
Socket

method_decorator

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

method_decorator

4.0.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

= Method Decorator {Build Status}[https://travis-ci.org/r4z3c/method_decorator] {Code Climate}[https://codeclimate.com/github/r4z3c/method_decorator] {Code Coverage}[https://codeclimate.com/github/r4z3c/method_decorator/coverage] {Issue Count}[https://codeclimate.com/github/r4z3c/method_decorator]

Provides a way to dynamically override methods without losing original behavior.

== Example

For a given class:

class SomeClass

    def some_method(some_arg)
        puts some_arg
    end

end

MethodDecorator.decorate SomeClass, :some_method do
    puts "decorated_some_method_with: #{call_args}"
    call_original
end

This call:

SomeClass.new.some_method 'some arg'

Produces this output:

decorated_some_method_with: ["some arg"]
some arg

== Helper Methods

Inside the block given to MethodDecorator.decorate, you can call four helper methods:

call_args          # returns original arguments given to the call
call_block         # returns original block given to the call
call_original      # call the original method, with original args and block given to the call
call_original_with # call the original method, with desired args and block

== Module inclusion

You can call decorate method directly instead of calling MethodDecorator.decorate. Just include MethodDecorator module:

class SomeClass

    def some_method(some_arg)
        puts some_arg
    end

    include MethodDecorator

    decorate :some_method do
        puts "decorated_some_method_with: #{call_args}"
        call_original
    end

end

== Singleton Classes Support

It works too:

class SomeClass

    class << self

        def some_method(some_arg)
            puts some_arg
        end

    end

end

MethodDecorator.decorate SomeClass.singleton_class, :some_method do
    puts "decorated_some_method_with: #{call_args}"
    call_original
end

SomeClass.some_method 'some arg'

== More

To see more of MethodDecorator usages, please take a look at DummyClass[https://github.com/r4z3c/method_decorator/blob/master/spec/support/dummy_class.rb].

===

{Donate}[https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=AMPXM3PW6CTBE]

FAQs

Package last updated on 18 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.