Class: JmeterPerf::Helpers::FallbackContextProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/jmeter_perf/helpers/fallback_content_proxy.rb

Constant Summary collapse

NON_PROXIED_METHODS =

List of methods that should not be proxied.

[
  :__id__,
  :__send__,
  :!,
  :"!=",
  :==,
  :equal?,
  :instance_eval,
  :instance_variable_get,
  :instance_variable_set,
  :instance_variables,
  :object_id,
  :remove_instance_variable
]
NON_PROXIED_INSTANCE_VARIABLES =

List of instance variables that should not be proxied.

[
  :@__fallback__,
  :@__receiver__
]

Instance Method Summary collapse

Constructor Details

#initialize(receiver, fallback) ⇒ FallbackContextProxy

Initializes a new FallbackContextProxy.

Parameters:

  • receiver (Object)

    the primary object to which method calls are delegated.

  • fallback (Object)

    the secondary object to which method calls are delegated if the primary does not respond.



39
40
41
42
# File 'lib/jmeter_perf/helpers/fallback_content_proxy.rb', line 39

def initialize(receiver, fallback)
  @__receiver__ = receiver
  @__fallback__ = fallback
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Handles method calls that are not explicitly defined in the proxy.

Parameters:

  • method (Symbol)

    the name of the method.

  • args (Array)

    the arguments to pass to the method.

Returns:

  • (Object)

    the result of the method call.



80
81
82
# File 'lib/jmeter_perf/helpers/fallback_content_proxy.rb', line 80

def method_missing(method, ...)
  __proxy_method__(method, ...)
end

Instance Method Details

#__proxy_method__(method) ⇒ Object

Proxies a method call to the primary receiver or fallback.

Parameters:

  • method (Symbol)

    the name of the method.

  • args (Array)

    the arguments to pass to the method.

Returns:

  • (Object)

    the result of the method call.

Raises:

  • (NoMethodError)

    if neither the primary receiver nor the fallback respond to the method.



90
91
92
93
94
# File 'lib/jmeter_perf/helpers/fallback_content_proxy.rb', line 90

def __proxy_method__(method, ...)
  @__receiver__.__send__(method.to_sym, ...)
rescue NoMethodError
  @__fallback__.__send__(method.to_sym, ...)
end

#idObject

Returns the ID of the primary receiver.

Returns:

  • (Object)

    the ID of the primary receiver.



47
48
49
# File 'lib/jmeter_perf/helpers/fallback_content_proxy.rb', line 47

def id
  @__receiver__.__send__(:id)
end

#instance_variablesArray<Symbol>

Returns the instance variables of the proxy, excluding non-proxied instance variables.

Returns:

  • (Array<Symbol>)

    the instance variables of the proxy.



62
63
64
# File 'lib/jmeter_perf/helpers/fallback_content_proxy.rb', line 62

def instance_variables
  super.map(&:to_sym) - NON_PROXIED_INSTANCE_VARIABLES
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Checks if the proxy responds to a given method.

Parameters:

  • name (Symbol)

    the name of the method.

  • include_private (Boolean)

    whether to include private methods.

Returns:

  • (Boolean)

    true if the proxy responds to the method, false otherwise.



71
72
73
# File 'lib/jmeter_perf/helpers/fallback_content_proxy.rb', line 71

def respond_to_missing?(name, include_private)
  __proxy_method__(:respond_to?, name, include_private)
end

#subObject

Proxies the ‘sub` method call to the primary receiver or fallback.

Parameters:

  • args (Array)

    the arguments to pass to the ‘sub` method.

Returns:

  • (Object)

    the result of the ‘sub` method call.



55
56
57
# File 'lib/jmeter_perf/helpers/fallback_content_proxy.rb', line 55

def sub(...)
  __proxy_method__(:sub, ...)
end