Module: Tins::Expose
- Included in:
- Object
- Defined in:
- lib/tins/expose.rb
Overview
This module should only be used in test or debugging contexts. Using it in production code can compromise encapsulation.
Modifying the exposed object’s state may affect the original object since it operates on a duplicated instance.
A module that provides a way to expose private and protected methods for testing or debugging purposes.
This module is particularly useful in test suites where you need to verify internal implementation details without making methods public, or for debugging complex object states during development.
Instance Method Summary collapse
-
#expose(method_name = nil, *args, &block) ⇒ Object
Expose any (private/protected) method or internal state of this object returning the result for specing purposes.
Instance Method Details
#expose(method_name = nil, *args, &block) ⇒ Object
Expose any (private/protected) method or internal state of this object returning the result for specing purposes.
This method provides three distinct usage patterns:
-
**Method Call**: When given a method name, directly calls that method and returns its result.
-
**Block Execution**: When given a block, evaluates the block in the context of the object, allowing access to private/protected methods.
-
**Full Exposure**: When called without arguments, returns a duplicate of the object with all private and protected methods exposed as public.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/tins/expose.rb', line 61 def expose(method_name = nil, *args, &block) if block instance_eval(&block) elsif method_name.nil? methods = private_methods(true) + protected_methods(true) o = dup o.singleton_class.class_eval do public(*methods) end o elsif method_name __send__(method_name, *args) end end |