Module: PromoteSelf

Defined in:
lib/mega/promoteself.rb

Overview

:title: PromoteSelf

PromoteSelf converts a module’s class methods into instance methods such that the first parameter is passed self at the instance level. This promotes DRY programming when wishin to offer both an inheritable and a module callable procedure.

Usage

module MyModule
  extend PromoteSelf
  def self.jumble( obj, arg )
    obj + arg
  end
end

class String
  include MyModule
end

MyModule.jumble( "Try", "Me" )  #=> "TryMe"

"Try".jumble( "Me" )            #=> 'TryMe'

Authors(s)

  • Thomas Sawyer

Instance Method Summary collapse

Instance Method Details

#singleton_method_added(meth) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/mega/promoteself.rb', line 53

def singleton_method_added( meth )
  d = %{
    def #{meth}(*args)
      #{self.name}.#{meth}(self,*args)
    end
  }
  self.class_eval d
  singleton_method_added_promoteself( meth ) if defined?(singleton_method_added_promoteself)
end

#singleton_method_added_promoteselfObject



52
# File 'lib/mega/promoteself.rb', line 52

alias_method :singleton_method_added_promoteself, :singleton_method_added