Class: Peeek

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePeeek

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

#callsPeeek::Calls (readonly)

Returns calls to the methods that the registered hooks captured.

Returns:

  • (Peeek::Calls)

    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

#currentPeeek (readonly)

Returns the current Peeek object.

Returns:

  • (Peeek)

    the current Peeek object

See Also:



21
22
23
# File 'lib/peeek.rb', line 21

def self.current
  @current ||= global
end

#globalPeeek (readonly)

Returns the global Peeek object.

Returns:

  • (Peeek)

    the global Peeek object



12
13
14
# File 'lib/peeek.rb', line 12

def self.global
  @global ||= new
end

#hooksPeeek::Hooks (readonly)

Returns the registered hooks.

Returns:



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.

Parameters:

  • hook_targets (Hash{Module, Class, Object => String, Array<String>, Symbol, Array<Symbol>})

    an object and method specifier(s) that be target of hook

Yields:

  • any process that want to run to capture

Returns:

Raises:

  • (ArgumentError)


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

.currentPeeek

Returns the current Peeek object.

Returns:

  • (Peeek)

    the current Peeek object

See Also:



21
22
23
# File 'lib/peeek.rb', line 21

def self.current
  @current ||= global
end

.globalPeeek

Returns the global Peeek object.

Returns:

  • (Peeek)

    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.

Yields:

  • any process that want to run to switch

Raises:

  • (ArgumentError)


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.

Yields:

  • any process that want to run while circumvent the registered hooks and supervision

Raises:

  • (ArgumentError)


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.

Parameters:

  • object (Module, Class, Object)

    a target object that hook

  • method_specs (Array<String>, Array<Symbol>)

    method specifiers of the object. see also examples of Peeek::Hook.create

Yields:

  • (call)

    process a call to the methods. give optionally

Yield Parameters:

Returns:

See Also:



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

#releaseObject

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