Class: Peeek
- Inherits:
-
Object
- Object
- Peeek
- Defined in:
- lib/peeek.rb,
lib/peeek/cli.rb,
lib/peeek/call.rb,
lib/peeek/hook.rb,
lib/peeek/calls.rb,
lib/peeek/hooks.rb,
lib/peeek/version.rb,
lib/peeek/supervisor.rb,
lib/peeek/cli/options.rb,
lib/peeek/hook/linker.rb,
lib/peeek/hook/instance.rb,
lib/peeek/hook/singleton.rb,
lib/peeek/hook/specifier.rb
Defined Under Namespace
Modules: Readily Classes: CLI, Call, Calls, Hook, Hooks, Supervisor
Constant Summary collapse
- VERSION =
'1.0.3'
Instance Attribute Summary collapse
-
#calls ⇒ Peeek::Calls
readonly
Calls to the methods that the registered hooks captured.
-
#current ⇒ Peeek
readonly
The current Peeek object.
-
#global ⇒ Peeek
readonly
The global Peeek object.
-
#hooks ⇒ Peeek::Hooks
readonly
The registered hooks.
Class Method Summary collapse
-
.capture(hook_targets) { ... } ⇒ Peeek::Calls
Capture all calls to hook targets.
-
.current ⇒ Peeek
The current Peeek object.
-
.global ⇒ Peeek
The global Peeek object.
-
.local { ... } ⇒ Object
Run process to switch to a local Peeek object from the current Peeek object.
Instance Method Summary collapse
-
#circumvent { ... } ⇒ Object
Run process while circumvent the registered hooks and supervision.
-
#hook(object, *method_specs) {|call| ... } ⇒ Peeek::Hooks
Register a hook to methods of an object.
-
#initialize ⇒ Peeek
constructor
Initialize the Peeek object.
-
#release ⇒ Object
Release the registered hooks and supervision.
Constructor Details
#initialize ⇒ Peeek
Initialize the Peeek object.
64 65 66 67 68 |
# File 'lib/peeek.rb', line 64 def initialize @hooks = Hooks.new @instance_supervisor = Supervisor.create_for_instance @singleton_supervisor = Supervisor.create_for_singleton end |
Instance Attribute Details
#calls ⇒ Peeek::Calls (readonly)
Returns calls to the methods that the registered hooks captured.
77 78 79 |
# File 'lib/peeek.rb', line 77 def calls Calls.new(@hooks.map(&:calls).inject([], &:+)) end |
#current ⇒ Peeek (readonly)
Returns the current Peeek object.
21 22 23 |
# File 'lib/peeek.rb', line 21 def self.current @current ||= global end |
#global ⇒ Peeek (readonly)
Returns the global Peeek object.
12 13 14 |
# File 'lib/peeek.rb', line 12 def self.global @global ||= new end |
#hooks ⇒ Peeek::Hooks (readonly)
Returns the registered hooks.
72 73 74 |
# File 'lib/peeek.rb', line 72 def hooks @hooks end |
Class Method Details
.capture(hook_targets) { ... } ⇒ Peeek::Calls
Capture all calls to hook targets.
53 54 55 56 57 58 59 60 61 |
# File 'lib/peeek.rb', line 53 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 |
.current ⇒ Peeek
Returns the current Peeek object.
21 22 23 |
# File 'lib/peeek.rb', line 21 def self.current @current ||= global end |
.global ⇒ Peeek
Returns the global Peeek object.
12 13 14 |
# File 'lib/peeek.rb', line 12 def self.global @global ||= new end |
.local { ... } ⇒ Object
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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/peeek.rb', line 31 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 |
Instance Method Details
#circumvent { ... } ⇒ Object
Run process while circumvent the registered hooks and supervision.
120 121 122 123 124 125 126 127 128 |
# File 'lib/peeek.rb', line 120 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 |
#hook(object, *method_specs) {|call| ... } ⇒ Peeek::Hooks
Register a hook to methods of an object.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/peeek.rb', line 91 def hook(object, *method_specs, &process) 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 ⇒ Object
Release the registered hooks and supervision.
109 110 111 112 113 114 |
# File 'lib/peeek.rb', line 109 def release @hooks.clear @instance_supervisor.clear @singleton_supervisor.clear self end |