class Peeek
Constants
- VERSION
Attributes
@attribute [r] hooks @return [Peeek::Hooks] the registered hooks
Public Class Methods
Capture all calls to hook targets.
@param [Hash{Module, Class, Object => String, Array<String>, Symbol, Array<Symbol>}] hook_targets
an object and method specifier(s) that be target of hook
@yield any process that want to run to capture @return [Peeek::Calls] calls that were captured in the block
# File lib/peeek.rb, line 54 def self.capture(hook_targets) raise ArgumentError, 'block not supplied' unless block_given? local do hook_targets.each { |object, method_specs| current.hook(object, *method_specs) } yield current.calls end end
@attribute [r] current @scope class @return [Peeek] the current Peeek
object
@see Peeek.local
# File lib/peeek.rb, line 22 def self.current @current ||= global end
@attribute [r] global @scope class @return [Peeek] the global Peeek
object
# File lib/peeek.rb, line 13 def self.global @global ||= new end
Run process to switch to a local Peeek
object from the current Peeek
object. The local Peeek
object doesn’t inherit the registered hooks and supervision from the current Peeek
object. The current Peeek
object reverts after ran the process.
@yield any process that want to run to switch
# File lib/peeek.rb, line 32 def self.local raise ArgumentError, 'block not supplied' unless block_given? old = current @current = new old.circumvent do begin yield ensure current.release @current = old end end end
Initialize the Peeek
object.
# File lib/peeek.rb, line 65 def initialize @hooks = Hooks.new @instance_supervisor = Supervisor.create_for_instance @singleton_supervisor = Supervisor.create_for_singleton end
Public Instance Methods
@attribute [r] calls @return [Peeek::Calls] calls to the methods that the registered hooks
captured
# File lib/peeek.rb, line 78 def calls Calls.new(@hooks.map(&:calls).inject([], &:+)) end
Run process while circumvent the registered hooks and supervision.
@yield any process that want to run while circumvent the registered hooks
and supervision
# File lib/peeek.rb, line 123 def circumvent(&process) raise ArgumentError, 'block not supplied' unless block_given? @singleton_supervisor.circumvent do @instance_supervisor.circumvent do @hooks.circumvent(&process) end end end
Register a hook to methods of an object.
@param [Module, Class, Object] object a target object that hook @param [Array<String>, Array<Symbol>] method_specs method specifiers of the
object. see also examples of {Peeek::Hook.create}
@yield [call] process a call to the methods. give optionally @yieldparam [Peeek::Call] call a call to the methods @return [Peeek::Hooks] registered hooks at calling
@see Peeek::Hook.create
# File lib/peeek.rb, line 92 def hook(object, *method_specs, &process) object = object.take if object.is_a?(Lazy) and object.defined? hooks = method_specs.map do |method_spec| Hook.create(object, method_spec, &process).tap do |hook| if hook.defined? hook.link elsif hook.instance? @instance_supervisor << hook elsif hook.singleton? @singleton_supervisor << hook end end end @hooks.push(*hooks) Hooks.new(hooks) end
Release the registered hooks and supervision.
# File lib/peeek.rb, line 112 def release @hooks.clear @instance_supervisor.clear @singleton_supervisor.clear self end