Class: Peeek::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/peeek/supervisor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback_name) ⇒ Supervisor

Initialize the supervisor.

Parameters:

  • callback_name (Symbol)

    name of the method that is called when methods was added to object of a hook



24
25
26
27
28
# File 'lib/peeek/supervisor.rb', line 24

def initialize(callback_name)
  @callback_name = callback_name
  @hooks = Hooks.new
  @original_callbacks = {}
end

Instance Attribute Details

#hooksPeeek::Hooks (readonly)

Returns hooks that is registered to the supervisor.

Returns:

  • (Peeek::Hooks)

    hooks that is registered to the supervisor



32
33
34
# File 'lib/peeek/supervisor.rb', line 32

def hooks
  @hooks
end

#original_callbacksHash<Object, Method> (readonly)

Returns original callbacks of objects that is supervising.

Returns:

  • (Hash<Object, Method>)

    original callbacks of objects that is supervising



37
38
39
# File 'lib/peeek/supervisor.rb', line 37

def original_callbacks
  @original_callbacks
end

Class Method Details

.create_for_instancePeeek::Supervisor

Create a supervisor for instance methods.

Returns:



9
10
11
# File 'lib/peeek/supervisor.rb', line 9

def self.create_for_instance
  new(:method_added)
end

.create_for_singletonPeeek::Supervisor

Create a supervisor for singleton methods.

Returns:



16
17
18
# File 'lib/peeek/supervisor.rb', line 16

def self.create_for_singleton
  new(:singleton_method_added)
end

Instance Method Details

#add(*hooks) ⇒ Object Also known as: <<

Add hooks to target that is supervised.

Parameters:

  • hooks (Array<Peeek::Hook>)

    hooks that is supervised



42
43
44
45
46
47
48
49
50
# File 'lib/peeek/supervisor.rb', line 42

def add(*hooks)
  @hooks.push(*hooks)

  hooks.map(&:object).uniq.each do |object|
    @original_callbacks[object] = proceed(object) unless proceeded?(object)
  end

  self
end

#circumvent { ... } ⇒ Object

Run process while circumvent supervision.

Yields:

  • any process that wants to run while circumvent supervision



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/peeek/supervisor.rb', line 63

def circumvent(&process)
  current_callbacks = @original_callbacks.keys.map do |object|
    [object, object.method(@callback_name)]
  end

  define_callbacks(@original_callbacks)

  begin
    @hooks.circumvent(&process)
  ensure
    define_callbacks(Hash[current_callbacks])
  end
end

#clearObject

Clear the hooks and the objects that is supervising.



54
55
56
57
58
# File 'lib/peeek/supervisor.rb', line 54

def clear
  @hooks.clear
  define_callbacks(@original_callbacks).clear
  self
end