File: fallback_context_proxy.rb

package info (click to toggle)
ruby-docile 1.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156 kB
  • sloc: ruby: 374; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 2,538 bytes parent folder | download | duplicates (3)
1
2
3
4
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'set'

module Docile
  # @api private
  #
  # A proxy object with a primary receiver as well as a secondary
  # fallback receiver.
  #
  # Will attempt to forward all method calls first to the primary receiver,
  # and then to the fallback receiver if the primary does not handle that
  # method.
  #
  # This is useful for implementing DSL evaluation in the context of an object.
  #
  # @see Docile.dsl_eval
  class FallbackContextProxy
    # The set of methods which will **not** be proxied, but instead answered
    # by this object directly.
    NON_PROXIED_METHODS = Set[:__send__, :object_id, :__id__, :==, :equal?,
                              :'!', :'!=', :instance_exec, :instance_variables,
                              :instance_variable_get, :instance_variable_set,
                              :remove_instance_variable]

    # The set of instance variables which are local to this object and hidden.
    # All other instance variables will be copied in and out of this object
    # from the scope in which this proxy was created.
    NON_PROXIED_INSTANCE_VARIABLES = Set[:@__receiver__, :@__fallback__]

    # Undefine all instance methods except those in {NON_PROXIED_METHODS}
    instance_methods.each do |method|
      undef_method(method) unless NON_PROXIED_METHODS.include?(method.to_sym)
    end

    # @param [Object] receiver  the primary proxy target to which all methods
    #                             initially will be forwarded
    # @param [Object] fallback  the fallback proxy target to which any methods
    #                             not handled by `receiver` will be forwarded
    def initialize(receiver, fallback)
      @__receiver__ = receiver
      @__fallback__ = fallback
    end

    # @return [Array<Symbol>]  Instance variable names, excluding
    #                            {NON_PROXIED_INSTANCE_VARIABLES}
    #
    # @note on Ruby 1.8.x, the instance variable names are actually of
    #   type `String`.
    def instance_variables
      # Ruby 1.8.x returns string names, convert to symbols for compatibility
      super.select { |v| !NON_PROXIED_INSTANCE_VARIABLES.include?(v.to_sym) }
    end

    # Proxy all methods, excluding {NON_PROXIED_METHODS}, first to `receiver`
    # and then to `fallback` if not found.
    def method_missing(method, *args, &block)
      if @__receiver__.respond_to?(method.to_sym)
        @__receiver__.__send__(method.to_sym, *args, &block)
      else
        @__fallback__.__send__(method.to_sym, *args, &block)
      end
    end
  end
end