Socket
Book a DemoInstallSign in
Socket

method_decorator.rb

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.rb

0.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

MethodDecorator

Method decorator implementation for Ruby, similar to Python.

Installation

Add this line to your application's Gemfile:

gem 'method-decorator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install method-decorator

Usage and Examples

Decorators can be used to wrap around method calls. Decoators can have their own arguments, including blocks. When the method is called, each decorator will be called in order, receiving the caller instance, the return value of the previous decorator, and the method's arguments and block.

class Multiply < Decorator::Base
  def call(this, args, ret, &block)
    multiplier, = @decorator_args

    return multiplier * ret if ret

    multiplier * original(this, *args, &block)
  end
end

class Controller
  extend Decorator::DecoratorAware

  Multiply(2)
  Multiply(3)
  def index
    10
  end
end

puts Controller.new.index # 60

Decorators can have names other than the class:

class DivideDecorator < Decorator::Base
  decorator_name :divide

  def call(this, args, ret, &block)
    #
  end
end

They can also receive their own block:

class Route < Decorator::Base
  def call(this, args, ret, &block)
    @decorator_block.call
  end
end

class Controller
  extend Decorator::DecoratorAware

  Route(:get, '/') { p "hello" }
  def index
    10
  end
end

Controller.new.index  # Hello

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 07 Nov 2019

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.