Project

delegates

0.01
No commit activity in last 3 years
No release in over 3 years
ActiveSupport's delegation syntax is much more convenient than Ruby's stdlib Forwardable. This gem just extracts it as an independent functionality (available on-demand without monkey-patching Module).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 3.8
~> 0.85.0
~> 1.38.0
>= 0.0.7
~> 0.9
>= 0
 Project Readme

Delegates

Gem Version build

This gem is just an extraction of the handy delegate :method1, :method2, method3, to: :receiver from ActiveSupport. It seems to be seriously superior to stdlib's Forwardable, and sometimes I want it in contexts when ActiveSupport and monkey-patching is undesireable.

Usage:

gem install delegates

(or add gem 'delegates' to your Gemfile).

Then:

class Employee < Struct.new(:name, :department, :address)
  # ...
  extend Delegates
  delegate :city, :street, to: :address
  # ...
end

employee = Employee.new(name, department, address)

employee.city # will call employee.address.city

to: can be anything evaluatable from inside the class: :<CONSTANT>, :@<instance_variable>, 'chain.of.calls' etc.; special names requiring self (like class method) handled gracefully with just delegate ..., to: :class. New methods are defined with eval-ing strings, so they are as fast as if manually written.

Supported options examples (all that ActiveSupport's Module#delegate supports):

delegate :city, to: :address, prefix: true # defined method would be address_city
delegate :city, to: :address, prefix: :adrs # defined method would be adrs_city

delegate :city, to: :address, private: true # defined method would be private

delegate :city, to: :address, allow_nil: true

The latter option will handle the employee.city call when employee.address is nil by returning nil; otherwise (by default) informative DelegationError is raised.

Credits

99.99% of credits should go to Rails users and contributors, who found and and handled miriads of realistic edge cases. I just copied the code (and groomed it a bit for closer to my own style).