Class: Peeek::Supervisor
- Inherits:
-
Object
- Object
- Peeek::Supervisor
- Defined in:
- lib/peeek/supervisor.rb
Instance Attribute Summary collapse
-
#hooks ⇒ Peeek::Hooks
readonly
Hooks that is registered to the supervisor.
-
#original_callbacks ⇒ Hash<Object, Method>
readonly
Original callbacks of objects that is supervising.
Class Method Summary collapse
-
.create_for_instance ⇒ Peeek::Supervisor
Create a supervisor for instance methods.
-
.create_for_singleton ⇒ Peeek::Supervisor
Create a supervisor for singleton methods.
Instance Method Summary collapse
-
#add(*hooks) ⇒ Object
(also: #<<)
Add hooks to target that is supervised.
-
#circumvent { ... } ⇒ Object
Run process while circumvent supervision.
-
#clear ⇒ Object
Clear the hooks and the objects that is supervising.
-
#initialize(callback_name) ⇒ Supervisor
constructor
Initialize the supervisor.
Constructor Details
#initialize(callback_name) ⇒ Supervisor
Initialize the supervisor.
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
#hooks ⇒ Peeek::Hooks (readonly)
Returns hooks that is registered to the supervisor.
32 33 34 |
# File 'lib/peeek/supervisor.rb', line 32 def hooks @hooks end |
#original_callbacks ⇒ Hash<Object, Method> (readonly)
Returns 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_instance ⇒ Peeek::Supervisor
Create a supervisor for instance methods.
9 10 11 |
# File 'lib/peeek/supervisor.rb', line 9 def self.create_for_instance new(:method_added) end |
.create_for_singleton ⇒ Peeek::Supervisor
Create a supervisor for singleton methods.
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.
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.
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 |
#clear ⇒ Object
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 |