Sha256: ecad1f61e3c155bd68759cb14d5a60a622963717217599434e5cdadf6ad4aa26

Contents?: true

Size: 1.83 KB

Versions: 40

Compression:

Stored size: 1.83 KB

Contents

module Forwardable
  def instance_delegate(hash)
    hash.each do |methods, accessor|
      methods = [methods] unless methods.respond_to? :each

      methods.each do |method|
        def_instance_delegator(accessor, method)
      end
    end
  end

  def def_instance_delegators(accessor, *methods)
    methods.each do |method|
      next if %w[__send__ __id__].include?(method)

      def_instance_delegator(accessor, method)
    end
  end

  def def_instance_delegator(accessor, method, ali = method)
    if accessor.to_s.start_with? '@'
      define_method ali do |*args, &block|
        instance_variable_get(accessor).__send__(method, *args, &block)
      end
    else
      define_method ali do |*args, &block|
        __send__(accessor).__send__(method, *args, &block)
      end
    end
  end

  alias delegate instance_delegate
  alias def_delegators def_instance_delegators
  alias def_delegator def_instance_delegator
end

module SingleForwardable
  def single_delegate(hash)
    hash.each do |methods, accessor|
      methods = [methods] unless methods.respond_to? :each

      methods.each do |method|
        def_single_delegator(accessor, method)
      end
    end
  end

  def def_single_delegators(accessor, *methods)
    methods.each do |method|
      next if %w[__send__ __id__].include? method

      def_single_delegator(accessor, method)
    end
  end

  def def_single_delegator(accessor, method, ali = method)
    if accessor.to_s.start_with? '@'
      define_singleton_method ali do |*args, &block|
        instance_variable_get(accessor).__send__(method, *args, &block)
      end
    else
      define_singleton_method ali do |*args, &block|
        __send__(accessor).__send__(method, *args, &block)
      end
    end
  end

  alias delegate single_delegate
  alias def_delegators def_single_delegators
  alias def_delegator def_single_delegator
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 stdlib/forwardable.rb
opal-1.8.2 stdlib/forwardable.rb
opal-1.8.1 stdlib/forwardable.rb
opal-1.8.0 stdlib/forwardable.rb
opal-1.8.0.beta1 stdlib/forwardable.rb
opal-1.7.4 stdlib/forwardable.rb
opal-1.8.0.alpha1 stdlib/forwardable.rb
opal-1.7.3 stdlib/forwardable.rb
opal-1.7.2 stdlib/forwardable.rb
opal-1.7.1 stdlib/forwardable.rb
opal-1.7.0 stdlib/forwardable.rb
opal-1.7.0.rc1 stdlib/forwardable.rb
opal-1.6.1 stdlib/forwardable.rb
opal-1.6.0 stdlib/forwardable.rb
opal-1.6.0.rc1 stdlib/forwardable.rb
opal-1.6.0.alpha1 stdlib/forwardable.rb
opal-1.5.1 stdlib/forwardable.rb
opal-1.5.0 stdlib/forwardable.rb
opal-1.5.0.rc1 stdlib/forwardable.rb
opal-1.4.1 stdlib/forwardable.rb