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.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePeeek

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

#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



78
79
80
# File 'lib/peeek.rb', line 78

def calls
  Calls.new(@hooks.map(&:calls).inject([], &:+))
end

#currentPeeek (readonly)

Returns the current Peeek object.

Returns:

  • (Peeek)

    the current Peeek object

See Also:



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

def self.current
  @current ||= global
end

#globalPeeek (readonly)

Returns the global Peeek object.

Returns:

  • (Peeek)

    the global Peeek object



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

def self.global
  @global ||= new
end

#hooksPeeek::Hooks (readonly)

Returns the registered hooks.

Returns:



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.

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)


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

.currentPeeek

Returns the current Peeek object.

Returns:

  • (Peeek)

    the current Peeek object

See Also:



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

def self.current
  @current ||= global
end

.globalPeeek

Returns the global Peeek object.

Returns:

  • (Peeek)

    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.

Yields:

  • any process that want to run to switch

Raises:

  • (ArgumentError)


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.

Yields:

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

Raises:

  • (ArgumentError)


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.

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:



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

#releaseObject

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