Class: AroundTheWorld::MethodWrapper
- Inherits:
-
Object
- Object
- AroundTheWorld::MethodWrapper
- Includes:
- ProxyCreation
- Defined in:
- lib/around_the_world/method_wrapper.rb,
lib/around_the_world/method_wrapper/proxy_creation.rb
Defined Under Namespace
Modules: ProxyCreation
Instance Attribute Summary collapse
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
-
.wrap(**args, &block) ⇒ Object
Passes arguments directly to #new - see #initialize for full docs.
Instance Method Summary collapse
-
#initialize(method_name:, target:, prevent_double_wrapping_for: nil, &block) ⇒ MethodWrapper
constructor
A new instance of MethodWrapper.
-
#wrap ⇒ Object
Defines the wrapped method inside a proxy module and prepends the proxy module to the target module if necessary.
Constructor Details
#initialize(method_name:, target:, prevent_double_wrapping_for: nil, &block) ⇒ MethodWrapper
Returns a new instance of MethodWrapper.
28 29 30 31 32 33 34 35 |
# File 'lib/around_the_world/method_wrapper.rb', line 28 def initialize(method_name:, target:, prevent_double_wrapping_for: nil, &block) raise TypeError, "target must be a module or a class" unless target.is_a?(Module) @method_name = method_name.to_sym @target = target @prevent_double_wrapping_for = prevent_double_wrapping_for || nil @block = block end |
Instance Attribute Details
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
18 19 20 |
# File 'lib/around_the_world/method_wrapper.rb', line 18 def method_name @method_name end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
18 19 20 |
# File 'lib/around_the_world/method_wrapper.rb', line 18 def target @target end |
Class Method Details
.wrap(**args, &block) ⇒ Object
Passes arguments directly to #new - see #initialize for full docs
13 14 15 |
# File 'lib/around_the_world/method_wrapper.rb', line 13 def wrap(**args, &block) new(**args, &block).wrap end |
Instance Method Details
#wrap ⇒ Object
Defines the wrapped method inside a proxy module and prepends the proxy module to the target module if necessary.
38 39 40 41 42 43 44 |
# File 'lib/around_the_world/method_wrapper.rb', line 38 def wrap ensure_method_defined! prevent_double_wrapping! if prevent_double_wrapping? define_proxy_method target.prepend proxy_module unless target.ancestors.include?(proxy_module) end |