Sha256: 7a7b8079ebf87fb775b2b67155f6f258722fb4cd99832f27f633fa33f7f6e7c4

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

class Module
  unless respond_to? :delegate
    def delegate(*methods)
      options = methods.pop
      unless options.is_a?(Hash) && to = options[:to]
        raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
      end

      if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
        raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
      end

      prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" || ''

      file, line = caller.first.split(':', 2)
      line = line.to_i

      methods.each do |method|
        on_nil =
          if options[:allow_nil]
            'return'
          else
            %(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
          end
        # def customer_name(*args, &block)
        module_eval %{
          def #{prefix}#{method}(*args, &block)
            #{to}.__send__(#{method.inspect}, *args, &block)
          rescue NoMethodError
            if #{to}.nil?
              #{on_nil}
            else
              raise
            end
          end
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sugar-high-0.7.1 lib/sugar-high/delegate.rb
sugar-high-0.7.0 lib/sugar-high/delegate.rb