Module: Impersonator::Api
- Included in:
- Impersonator
- Defined in:
- lib/impersonator/api.rb
Overview
Public API exposed by the global Impersonator
module.
Instance Method Summary collapse
- #configuration ⇒ Configuration
-
#configure {|config| ... } ⇒ Object
Configures how Impersonator works by yielding a configuration object you can use to tweak settings.
-
#current_recording ⇒ Recording?
The current recording, if any, or
nil
otherwise. -
#impersonate(*methods) ⇒ Proxy
Receives a list of methods to impersonate and a block that will be used, at record time, to instantiate the object to impersonate.
-
#impersonate_methods(actual_object, *methods) ⇒ Proxy
Impersonates a list of methods of a given object.
-
#recording(label, disabled: false) ⇒ Object
Wraps the execution of the yielded code withing a new recording titled with the passed label.
-
#reset ⇒ Object
Reset configuration and other global state.
Instance Method Details
#configuration ⇒ Configuration
43 44 45 |
# File 'lib/impersonator/api.rb', line 43 def configuration @configuration ||= Configuration.new end |
#configure {|config| ... } ⇒ Object
Configures how Impersonator works by yielding a configuration object you can use to tweak settings.
Impersonator.configure do |config|
config.recordings_path = 'my/own/recording/path'
end
38 39 40 |
# File 'lib/impersonator/api.rb', line 38 def configure yield configuration end |
#current_recording ⇒ Recording?
The current recording, if any, or nil
otherwise.
24 25 26 |
# File 'lib/impersonator/api.rb', line 24 def current_recording @current_recording end |
#impersonate(*methods) ⇒ Proxy
Receives a list of methods to impersonate and a block that will be used, at record time, to instantiate the object to impersonate. At replay time, it will generate a double that will replay the methods.
impersonator = Impersonator.impersonate(:add, :subtract) { Calculator.new } impersonator.add(3, 4)
Notice that the actual object won't be instantiated in record mode. For that reason, the impersonated object will only respond to the list of impersonated methods.
If you need to invoke other (not impersonated) methods see #impersonate_method instead.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/impersonator/api.rb', line 69 def impersonate(*methods) unless block_given? raise ArgumentError, 'Provide a block to instantiate the object to impersonate in record mode' end object_to_impersonate = if current_recording&.record_mode? yield else Double.new(*methods) end impersonate_methods(object_to_impersonate, *methods) end |
#impersonate_methods(actual_object, *methods) ⇒ Proxy
Impersonates a list of methods of a given object
The returned object will impersonate the list of methods and will delegate the rest of method calls to the actual object.
90 91 92 93 94 95 96 97 98 |
# File 'lib/impersonator/api.rb', line 90 def impersonate_methods(actual_object, *methods) unless @current_recording raise Impersonator::Errors::ConfigurationError, 'You must start a recording to impersonate'\ ' objects. Use Impersonator.recording {}' end ::Impersonator::Proxy.new(actual_object, recording: current_recording, impersonated_methods: methods) end |
#recording(label, disabled: false) ⇒ Object
Wraps the execution of the yielded code withing a new recording titled with the passed label.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/impersonator/api.rb', line 10 def recording(label, disabled: false) @current_recording = ::Impersonator::Recording.new label, disabled: disabled, recordings_path: configuration.recordings_path @current_recording.start yield @current_recording.finish ensure @current_recording = nil end |
#reset ⇒ Object
Reset configuration and other global state.
It is meant to be used internally by tests.
50 51 52 53 |
# File 'lib/impersonator/api.rb', line 50 def reset @current_recording = nil @configuration = nil end |