1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
# encoding: utf-8
module Origin
# Allows for easy delegation of origin queryable instance methods to a
# specific method.
module Forwardable
# Tells origin with method on the class to delegate to when calling an
# original selectable or optional method on the class.
#
# @example Tell origin where to select from.
# class Band
# extend Origin::Forwardable
# select_with :criteria
#
# def self.criteria
# Query.new
# end
# end
#
# @param [ Symbol ] receiver The name of the receiver method.
#
# @return [ Array<Symbol> ] The names of the forwarded methods.
#
# @since 1.0.0
def select_with(receiver)
(Selectable.forwardables + Optional.forwardables).each do |name|
__forward__(name, receiver)
end
end
private
# Forwards the method name to the provided receiver method.
#
# @api private
#
# @example Define the forwarding.
# Model.__forward__(:exists, :criteria)
#
# @param [ Symbol ] name The name of the method.
# @param [ Symbol ] receiver The name of the receiver method.
#
# @since 1.0.0
def __forward__(name, receiver)
if self.class == Module
module_eval <<-SEL
def #{name}(*args, &block)
#{receiver}.__send__(:#{name}, *args, &block)
end
SEL
else
(class << self; self; end).class_eval <<-SEL
def #{name}(*args, &block)
#{receiver}.__send__(:#{name}, *args, &block)
end
SEL
end
end
end
end
|