Project

not_my_job

0.0
No commit activity in last 3 years
No release in over 3 years
Provides a delegate class method to easily expose contained object's methods as your own. If the delegate object is nil, there are several options: If no block is provided, it will raise a NoMethodError exception; if the optional code block is specified you may return an alternate value, or to take some arbitrary action.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 Project Readme

(That's) Not My Job

Also known as Empleado Público ;) provides a delegate class method to easily expose contained object's methods as your own.

Quick install

Add this line to your application's Gemfile:

gem 'not_my_job'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install not_my_job

Usage

  delegate(*methods, to: target [, with_prefix: true|false ]) { block } → obj

In order to access the delegate object's methods, NotMyJob creates instance methods for each one specified method.

If the delegate object is nil, there are several options: If no block is provided, it will raise a NoMethodError exception; if the optional code block is specified you may return an alternate value, or to take some arbitrary action.

Examples

Simple delegation:

class Cuisine
  attr_accessor :name, :chef
  
  def initialize(name, chef)
    @name = name
    @chef = chef
  end
end

class Restaurant
  extend NotMyJob
  
  delegate :name, to: :cuisine
  delegate :chef, to: :cuisine, with_prefix: false
  
  def initialize(cuisine)
    @cuisine = cuisine
  end
end

cuisine    = Cuisine.new("Italian", "Mario") 
restaurant = Restauran.new(cuisine)

restaurant.cuisine_name #=> "Italian"
restaurant.chef         #=> "Mario"

Multiple methods delegation:

class Restaurant
  extend NotMyJob
  delegate :name, :chef to: :cuisine
end

restaurant.cuisine_name #=> "Italian"
restaurant.cuisine_chef #=> "Mario"

With ActiveRecord associations:

class Restaurant < ActiveRecord::Base
  extend NotMyJob
  
  belongs_to :place
  delegate :name, to: :place
end
restaurant.place_name #=> "Argentina"

If the delegate object is nil and a block is specified:

class Restaurant
  extend NotMyJob
  
  delegate :name, to: :category do
    logger.debug "Category is nil"
    send_notification_email
    "No category has been assigned to this restaurant"
  end
end
restaurant.category_name #=> "No category has been assigned to this restaurant"