Module: ProxyMethod::ClassMethods

Defined in:
lib/proxy_method.rb

Constant Summary collapse

DEFAULT_PROXY_MESSAGE =
'Disabled by proxy_method'
DEFAULT_PREFIX =
'unproxied_'

Instance Method Summary collapse

Instance Method Details

#proxiedObject



76
77
78
# File 'lib/proxy_method.rb', line 76

def proxied
  self.dup.reproxy!
end

#proxied_class_methodsObject



10
11
12
# File 'lib/proxy_method.rb', line 10

def proxied_class_methods
  @_proxied_class_methods ||= {}
end

#proxied_instance_methodsObject



6
7
8
# File 'lib/proxy_method.rb', line 6

def proxied_instance_methods
  @_proxied_instance_methods ||= {}
end

#proxy_class_method(original_method_names, options = {}, &proxy_block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/proxy_method.rb', line 19

def proxy_class_method(original_method_names, options = {}, &proxy_block)
  original_method_names = Array(original_method_names)

  error_message = options[:raise] || DEFAULT_PROXY_MESSAGE
  prefix = options[:prefix] || DEFAULT_PREFIX

  original_method_names.each do |original_method_name|
    self.proxied_class_methods.merge!(original_method_name => prefix)
    new_method_name = :"#{prefix}#{original_method_name}"

    self.singleton_class.send(:alias_method, new_method_name, original_method_name)
    define_singleton_method(original_method_name) do |*args, &block|
      if proxy_class_methods_enabled?
        if proxy_block
          proxy_block.call(self.unproxied, original_method_name, *args, &block)
        else
          raise error_message
        end
      else
        send(new_method_name, *args, &block)
      end
    end
  end
end

#proxy_class_methods_enabled?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/proxy_method.rb', line 14

def proxy_class_methods_enabled?
  return @_proxy_class_methods_enabled if defined?(@_proxy_class_methods_enabled)
  @_proxy_class_methods_enabled = true
end

#proxy_instance_method(original_method_names, options = {}, &proxy_block) ⇒ Object Also known as: proxy_method



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/proxy_method.rb', line 44

def proxy_instance_method(original_method_names, options = {}, &proxy_block)
  original_method_names = Array(original_method_names)

  error_message = options[:raise] || DEFAULT_PROXY_MESSAGE
  prefix = options[:prefix] || DEFAULT_PREFIX

  original_method_names.each do |original_method_name|
    self.proxied_instance_methods.merge!(original_method_name => prefix)
    new_method_name = :"#{prefix}#{original_method_name}"

    alias_method new_method_name, original_method_name

    define_method(original_method_name) do |*args, &block|
      if proxy_instance_methods_enabled?
        if proxy_block
          proxy_block.call(self.unproxied, original_method_name, *args, &block)
        else
          raise error_message
        end
      else
        send(new_method_name, *args, &block)
      end
    end
  end
end

#reproxy!Object



85
86
87
88
# File 'lib/proxy_method.rb', line 85

def reproxy!
  @_proxy_class_methods_enabled = true
  self
end

#unproxiedObject



72
73
74
# File 'lib/proxy_method.rb', line 72

def unproxied
  self.dup.unproxy!
end

#unproxy!Object



80
81
82
83
# File 'lib/proxy_method.rb', line 80

def unproxy!
  @_proxy_class_methods_enabled = false
  self
end