Module: VirtFS::DelegateModule

Included in:
VirtFS
Defined in:
lib/virtfs/delegate_module.rb

Instance Method Summary collapse

Instance Method Details

#delegate_module(superclass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/virtfs/delegate_module.rb', line 5

def delegate_module(superclass)
  mod = Module.new
  methods = superclass.instance_methods
  methods -= ::Delegator.public_api
  methods -= [:to_s, :inspect, :=~, :!~, :===]

  mod.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 self.equal?(obj)
      @delegate_dc_obj = obj
    end

    methods.each do |method|
      define_method(method, Delegator.delegating_block(method))
    end
  end

  mod.define_singleton_method :public_instance_methods do |all = true|
    super(all) - superclass.protected_instance_methods
  end

  mod.define_singleton_method :protected_instance_methods do |all = true|
    super(all) | superclass.protected_instance_methods
  end
  mod
end