free_mutant/lib/mutant/delegator.rb

44 lines
872 B
Ruby
Raw Normal View History

2014-05-11 13:20:09 -04:00
module Mutant
2014-06-09 22:13:15 -04:00
# A simple delegator with opinions
2014-05-11 13:20:09 -04:00
module Delegator
2014-06-09 22:13:15 -04:00
# Class level mixins
2014-05-11 13:20:09 -04:00
module ClassMethods
private
2014-05-22 22:57:43 -04:00
2014-05-11 13:20:09 -04:00
# Create delegators to object
#
# @return [undefined]
def delegate(*names)
2014-05-22 23:47:50 -04:00
names.each(&method(:define_delegator))
2014-05-11 13:20:09 -04:00
end
# Create delegator to object
#
# @param [Symbol] name
#
# @return [undefined]
def define_delegator(name)
2014-07-07 11:03:31 -04:00
fail "method #{name} already defined" if instance_methods.include?(name)
2014-05-11 13:20:09 -04:00
define_method(name) do
object.public_send(name)
end
private name
end
end # ClassMethods
# Hook called when module is included
#
# @param [Class,Module] host
#
2014-06-28 16:52:47 -04:00
# @return [undefined]
2014-05-11 13:20:09 -04:00
def self.included(host)
super
2014-05-22 23:49:20 -04:00
host.extend(ClassMethods)
2014-05-11 13:20:09 -04:00
end
end # Delegator
end # Mutant