Class: Methodist::Observer
Overview
Methodist::Observer
Base class for Methodist observers
Defined Under Namespace
Classes: ExecuteBlockWasNotDefined
Class Attribute Summary collapse
-
.execution_block ⇒ Object
readonly
Returns the value of attribute execution_block.
Class Method Summary collapse
-
.execute(&block) ⇒ Object
Method for passing execution block for execution after observed method.
-
.observe(klass, method_name, skip_if: nil, instance_method: true, &main_block) ⇒ Object
Subscribe to the instance method of the klass to observe.
-
.observe_class_method(klass, method_name, skip_if: nil, &main_block) ⇒ Object
Subscribe to the class method of the klass to observe.
- .observed_methods ⇒ Object
-
.stop_observe(klass, method_name, instance_method: true) ⇒ Object
Stop observation instance method of klass for observe.
-
.trigger!(klass, method_name, result, *args) ⇒ Object
The executable block is passed to #execute Parameters klass and method_name are passed to block call.
Methods inherited from Pattern
Class Attribute Details
.execution_block ⇒ Object (readonly)
Returns the value of attribute execution_block.
8 9 10 |
# File 'lib/methodist/observer.rb', line 8 def execution_block @execution_block end |
Class Method Details
.execute(&block) ⇒ Object
Method for passing execution block for execution after observed method
110 111 112 |
# File 'lib/methodist/observer.rb', line 110 def execute(&block) @execution_block = block end |
.observe(klass, method_name, skip_if: nil, instance_method: true, &main_block) ⇒ Object
Subscribe to the instance method of the klass to observe
Parameters
-
klass
[Class] - The owner of the method for observation -
method_name
[String/Symbol] - Observation method name
Options
-
skip_if
[Proc] - Skip triggered execution if condition is true (default nil) -
instance_method
[Boolean] - True if observer klass instance method, false if observer klass method (default true)
Yield
main_block - execution block. If this block was passed, ‘#execute` block will be ignored
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/methodist/observer.rb', line 26 def observe(klass, method_name, skip_if: nil, instance_method: true, &main_block) method_names = generate_method_names(method_name) # Make dump original method. if instance_method alias_instance_method(klass, method_names[:dump], method_names[:name]) else alias_class_method(klass, method_names[:dump], method_names[:name]) end observe_method!(klass, method_names, skip_if: skip_if, instance_method: instance_method, &main_block) end |
.observe_class_method(klass, method_name, skip_if: nil, &main_block) ⇒ Object
Subscribe to the class method of the klass to observe
Parameters
-
klass
[Class] - The owner of the method for observation -
method_name
[String/Symbol] - Observation method name
Options
-
skip_if
[Proc] - Skip triggered execution if condition is true
Yield
main_block - execution block. If this block was passed, ‘#execute` block will be ignored
54 55 56 |
# File 'lib/methodist/observer.rb', line 54 def observe_class_method(klass, method_name, skip_if: nil, &main_block) observe(klass, method_name, skip_if: skip_if, instance_method: false, &main_block) end |
.observed_methods ⇒ Object
114 115 116 |
# File 'lib/methodist/observer.rb', line 114 def observed_methods @observed_method ||= [] end |
.stop_observe(klass, method_name, instance_method: true) ⇒ Object
Stop observation instance method of klass for observe
Parameters
-
klass
[Class] - Klass owner of the observed method -
method_name
[String/Symbol] - Name of the observed method
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/methodist/observer.rb', line 65 def stop_observe(klass, method_name, instance_method: true) method_names = generate_method_names(method_name) if instance_method unless method_defined?(klass, method_names[:observed]) && method_defined?(klass, method_names[:dump]) return false end klass.send(:alias_method, method_names[:name], method_names[:dump]) # restore dumped instance method klass.send(:remove_method, method_names[:observed]) # remove observed instance method klass.send(:remove_method, method_names[:dump]) # remove dump instance method else unless klass_method_defined?(klass, method_names[:observed]) && klass_method_defined?(klass, method_names[:dump]) return false end klass.singleton_class.send(:alias_method, method_names[:name], method_names[:dump]) # restore dumped class method klass.singleton_class.send(:remove_method, method_names[:observed]) # remove observed class method klass.singleton_class.send(:remove_method, method_names[:dump]) # remove dump class method end remove_from_observed(klass, method_names[:name], instance_method: instance_method) true end |
.trigger!(klass, method_name, result, *args) ⇒ Object
The executable block is passed to #execute Parameters klass and method_name are passed to block call
Parameters
-
klass
[Class] - Klass owner of the observed method -
method
[Class] - Name of the observed method
Raise
ExecuteBlockWasNotDefined
- when no block was passed to the execute method in the observer class
101 102 103 104 105 |
# File 'lib/methodist/observer.rb', line 101 def trigger!(klass, method_name, result, *args) block = execution_block rescue nil raise ExecuteBlockWasNotDefined, "You must define execute block in your #{self.name}" unless block block.call(klass, method_name, result, *args) end |