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.4'
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.
65 66 67 68 69 |
# File 'lib/peeek.rb', line 65 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.
78 79 80 |
# File 'lib/peeek.rb', line 78 def calls Calls.new(@hooks.map(&:calls).inject([], &:+)) end |
#current ⇒ Peeek (readonly)
Returns the current Peeek object.
22 23 24 |
# File 'lib/peeek.rb', line 22 def self.current @current ||= global end |
#global ⇒ Peeek (readonly)
Returns the global Peeek object.
13 14 15 |
# File 'lib/peeek.rb', line 13 def self.global @global ||= new end |
#hooks ⇒ Peeek::Hooks (readonly)
Returns the registered hooks.
73 74 75 |
# File 'lib/peeek.rb', line 73 def hooks @hooks end |
Class Method Details
.capture(hook_targets) { ... } ⇒ Peeek::Calls
Capture all calls to hook targets.
54 55 56 57 58 59 60 61 62 |
# 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 |
.current ⇒ Peeek
Returns the current Peeek object.
22 23 24 |
# File 'lib/peeek.rb', line 22 def self.current @current ||= global end |
.global ⇒ Peeek
Returns the global Peeek object.
13 14 15 |
# File 'lib/peeek.rb', line 13 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 |
Instance Method Details
#circumvent { ... } ⇒ Object
Run process while circumvent the registered hooks and supervision.
123 124 125 126 127 128 129 130 131 |
# 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 |
#hook(object, *method_specs) {|call| ... } ⇒ Peeek::Hooks
Register a hook to methods of an object.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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 ⇒ Object
Release the registered hooks and supervision.
112 113 114 115 116 117 |
# File 'lib/peeek.rb', line 112 def release @hooks.clear @instance_supervisor.clear @singleton_supervisor.clear self end |