Top Level Namespace

Defined Under Namespace

Modules: Base64, Benchmark, Enumerable, Exception2MessageMapper, ExceptionForMatrix, FileUtils, Forwardable, JS, JSON, Kernel, Nashorn, Native, NodeJS, Observable, OpenURI, REPLUtils, Racc, RbConfig, SecureRandom, Shellwords, SingleForwardable, Singleton, URI, YAML Classes: Array, BasicObject, BigDecimal, Boolean, Buffer, Class, Console, Date, DateTime, Delegator, Dir, ENV, ERB, File, Float, Hash, IO, Integer, Logger, MatchData, Matrix, Method, Module, Mutex, NilClass, Numeric, Object, OpenStruct, OptionParser, PP, Pathname, PrettyPrint, Proc, Promise, PromiseV2, Range, Rational, Regexp, Set, SimpleDelegator, String, StringIO, StringScanner, Struct, Template, Thread, ThreadError, Time, Vector

Constant Summary collapse

Queue =
Thread::Queue
PromiseV1 =
Promise
OptParse =

An alias for OptionParser.

OptionParser
RUBY_EXE =

required for mspec it would appear

'bundle exec exe/opal'
ARGV =

Compatibility utilities for the API we provide in lib/opal/cli_runners/mini_racer

`scriptArgs`
ARGF =
Object.new
ParseError =
Racc::ParseError
OPAL_PLATFORM =
if nashorn
  'nashorn'
elsif node
  'nodejs'
elsif headless_chrome
  'headless-chrome'
elsif gjs
  'gjs'
elsif quickjs
  'quickjs'
elsif opal_miniracer
  'opal-miniracer'
else # possibly browser, which is the primary target
end

Instance Method Summary collapse

Instance Method Details

#DelegateClass(superclass, &block) ⇒ Object

The primary interface to this library. Use to setup delegation when defining your class.

class MyClass < DelegateClass(ClassToDelegateTo) # Step 1 def initialize super(obj_of_ClassToDelegateTo) # Step 2 end end

or:

MyClass = DelegateClass(ClassToDelegateTo) do # Step 1 def initialize super(obj_of_ClassToDelegateTo) # Step 2 end end

Here's a sample of use from Tempfile which is really a File object with a few special rules about storage location and when the File should be deleted. That makes for an almost textbook perfect example of how to use delegation.

class Tempfile < DelegateClass(File) # constant and class member data initialization...

def initialize(basename, tmpdir=Dir::tmpdir)
  # build up file path/name in var tmpname...

  @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)

  # ...

  super(@tmpfile)

  # below this point, all methods of File are supported...
end

# ...

end



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'opal/stdlib/delegate.rb', line 407

def DelegateClass(superclass, &block)
  klass = Class.new(Delegator)
  ignores = [*::Delegator.public_api, :to_s, :inspect, :=~, :!~, :===]
  protected_instance_methods = superclass.protected_instance_methods
  protected_instance_methods -= ignores
  public_instance_methods = superclass.public_instance_methods
  public_instance_methods -= ignores
  klass.module_eval do
    def __getobj__ # :nodoc:
      unless defined?(@delegate_dc_obj)
        return yield if block_given?
        __raise__ ::ArgumentError, 'not delegated'
      end
      @delegate_dc_obj
    end

    def __setobj__(obj) # :nodoc:
      __raise__ ::ArgumentError, 'cannot delegate to self' if equal?(obj)
      @delegate_dc_obj = obj
    end
    protected_instance_methods.each do |method|
      define_method(method, Delegator.delegating_block(method))
      protected method
    end
    public_instance_methods.each do |method|
      define_method(method, Delegator.delegating_block(method))
    end
  end
  klass.define_singleton_method :public_instance_methods do |all = true|
    super(all) | superclass.public_instance_methods
  end
  klass.define_singleton_method :protected_instance_methods do |all = true|
    super(all) | superclass.protected_instance_methods
  end
  klass.define_singleton_method :instance_methods do |all = true|
    super(all) | superclass.instance_methods
  end
  klass.define_singleton_method :public_instance_method do |name|
    super(name)
  rescue NameError
    raise unless self.public_instance_methods.include?(name)
    superclass.public_instance_method(name)
  end
  klass.define_singleton_method :instance_method do |name|
    super(name)
  rescue NameError
    raise unless instance_methods.include?(name)
    superclass.instance_method(name)
  end
  klass.module_eval(&block) if block
  klass
end

#ruby2_keywordsObject

This library provides three different ways to delegate method calls to an object. The easiest to use is SimpleDelegator. Pass an object to the constructor and all methods supported by the object will be delegated. This object can be changed later.

Going a step further, the top level DelegateClass method allows you to easily setup delegation through class inheritance. This is considerably more flexible and thus probably the most common use for this library.

Finally, if you need full control over the delegation scheme, you can inherit from the abstract class Delegator and customize as needed. (If you find yourself needing this control, have a look at Forwardable which is also in the standard library. It may suit your needs better.)

SimpleDelegator's implementation serves as a nice example of the use of Delegator:

require 'delegate'

class SimpleDelegator < Delegator def getobj @delegate_sd_obj # return object we are delegating to, required end

def __setobj__(obj)
  @delegate_sd_obj = obj # change delegation object,
                         # a feature we're providing
end

end

== Notes

Be advised, RDoc will not detect delegated methods.



45
# File 'opal/stdlib/delegate.rb', line 45

require 'ruby2_keywords'