module OhDelegator::Delegatee

Public Instance Methods

oh_delegators(*attributes) click to toggle source
# File lib/oh_delegator/delegatee.rb, line 3
def oh_delegators(*attributes)
  attributes.each do |attribute|
    # Read the delegator class alongwith the delegatee.
    klass = "#{ name }::#{ attribute.to_s.classify }".constantize

    define_method attribute do
      instance_variable_name = "@#{ attribute }"
      instance_variable = instance_variable_get(instance_variable_name)

      return instance_variable if instance_variable

      instance_variable_set(instance_variable_name, klass.new(self))
    end
  end
end