Sha256: 301c52aab666d94862a2c82d588b93edab4c560f18a5b0db1e66542b4bfeca7c

Contents?: true

Size: 814 Bytes

Versions: 2

Compression:

Stored size: 814 Bytes

Contents

module SimpleDelegate
  def self.included(base)
    base.extend(SimpleDelegate::ClassMethods)
  end

  module ClassMethods
    def delegate(*args)
      opts = args.pop

      args.each do |method|
        define_method(method) do |*method_args, &method_blk|
          if opts[:to_instance_variable]
            object = instance_variable_get("@#{opts.fetch(:to_instance_variable)}")
            raise "Nothing was returned by: #{opts.fetch(:to_instance_variable)}" unless object
          elsif opts[:to]
            object = __send__(opts.fetch(:to))
            raise "Nothing was returned by: #{opts.fetch(:to)}" unless object
          else
            raise "Don't know where to delegate to"
          end

          object.__send__(method, *method_args, &method_blk)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_delegate-0.0.3 lib/simple_delegate.rb
simple_delegate-0.0.2 lib/simple_delegate.rb