Module: Hooks
- Included in:
- Battle::AI::Base, Battle::Logic::AbilityChangeHandler, Battle::Logic::BattleEndHandler, Battle::Logic::CatchHandler, Battle::Logic::DamageHandler, Battle::Logic::EndTurnHandler, Battle::Logic::ExpHandler, Battle::Logic::FTerrainChangeHandler, Battle::Logic::FleeHandler, Battle::Logic::ItemChangeHandler, Battle::Logic::StatChangeHandler, Battle::Logic::StatusChangeHandler, Battle::Logic::SwitchHandler, Battle::Logic::TransformHandler, Battle::Logic::WeatherChangeHandler, Battle::Move, Battle::Scene, Graphics, Graphics, PFM::Pokemon, PFM::PokemonBattler, Scene_Map, Spriteset_Map
- Defined in:
- scripts/00010 Hooks.rb
Overview
Module responsive of adding the Hook functionality to a class and its childs
How to use it:
# 1. Include the Hooks module to your class so exec_hooks become visible (and it's possible to register a hook)
include Hooks
# 2. Make a method that supports hooks
def my_method(params)
exec_hooks(NameOfTheClass, :name_of_prehook, binding) # Not mandatory
# do some stuff
exec_hooks(NameOfTheClass, :name_of_posthook, binding) # Not mandatory
return normal_return
rescue Hooks::ForceReturn => e
return e.data # What the hooks forced to return
end
# 3. To register a hook call the function from Hooks
Hooks.register(NameOfTheClass, :name_of_the_hook, 'Reason so you can delete it') do |hook_binding|
# Do something with self (object that called the hook) and hook_binding current binding
end
Defined Under Namespace
Modules: Reason Classes: ForceReturn
Class Method Summary collapse
-
.included(klass)
Function called when Hooks is included.
-
.register(klass, name, reason = nil, &block) {|hook_binding| ... }
Function that register a hook.
-
.remove(klass, name, reason = 'None')
Function that removes a hook with a reason.
-
.remove_without_name(klass, reason = 'None')
Function that removes a hook with a reason.
Instance Method Summary collapse
-
#exec_hooks(klass, name, method_binding)
Function that execute the hooks.
-
#force_return(object)
Function that force the return from the hook.
Class Method Details
.included(klass)
Function called when Hooks is included
106 107 108 |
# File 'scripts/00010 Hooks.rb', line 106 def included(klass) klass.instance_variable_set(:@__hooks, {}) end |
.register(klass, name, reason = nil, &block) {|hook_binding| ... }
Function that register a hook
76 77 78 79 80 81 82 83 84 |
# File 'scripts/00010 Hooks.rb', line 76 def register(klass, name, reason = nil, &block) log_error(format('A proc named %<name>s was defined without reason in %<class>s', name: name, class: klass)) unless reason hooks = (klass.instance_variable_get(:@__hooks)[name] ||= []) hooks << block block.extend(Reason) block.reason = reason || 'None' rescue NoMethodError raise 'Hooks was not included to that class!' end |
.remove(klass, name, reason = 'None')
Function that removes a hook with a reason
90 91 92 93 |
# File 'scripts/00010 Hooks.rb', line 90 def remove(klass, name, reason = 'None') hooks = klass.instance_variable_get(:@__hooks)&.[](name) hooks&.reject! { |hook| hook.reason == reason } end |
.remove_without_name(klass, reason = 'None')
Function that removes a hook with a reason
98 99 100 101 102 |
# File 'scripts/00010 Hooks.rb', line 98 def remove_without_name(klass, reason = 'None') klass.instance_variable_get(:@__hooks)&.each_value do |hooks| hooks&.reject! { |hook| hook.reason == reason } end end |
Instance Method Details
#exec_hooks(klass, name, method_binding)
Function that execute the hooks
52 53 54 55 56 57 58 59 |
# File 'scripts/00010 Hooks.rb', line 52 def exec_hooks(klass, name, method_binding) hooks = klass.instance_variable_get(:@__hooks)&.[](name) ForceReturn::CONST.hook_name = name hooks&.each do |hook| ForceReturn::CONST.reason = hook.reason instance_exec(method_binding, &hook) end end |
#force_return(object)
Function that force the return from the hook
63 64 65 66 |
# File 'scripts/00010 Hooks.rb', line 63 def force_return(object) ForceReturn::CONST.data = object raise ForceReturn::CONST end |