Module: Sequel::Plugins::DeprecatedHookClassMethods::ClassMethods
- Defined in:
- lib/sequel/model/deprecated_hooks.rb
Instance Method Summary collapse
-
#add_hook_type(*hooks) ⇒ Object
This adds a new hook type.
-
#has_hooks?(hook) ⇒ Boolean
Returns true if there are any hook blocks for the given hook.
-
#hook_blocks(hook) ⇒ Object
Yield every block related to the given hook.
- #inherited(subclass) ⇒ Object
Instance Method Details
#add_hook_type(*hooks) ⇒ Object
This adds a new hook type. It will define both a class method that you can use to add hooks, as well as an instance method that you can use to call all hooks of that type. The class method can be called with a symbol or a block or both. If a block is given and and symbol is not, it adds the hook block to the hook type. If a block and symbol are both given, it replaces the hook block associated with that symbol for a given hook type, or adds it if there is no hook block with that symbol for that hook type. If no block is given, it assumes the symbol specifies an instance method to call and adds it to the hook type.
If any hook block returns false, the instance method will return false immediately without running the rest of the hooks of that type.
It is recommended that you always provide a symbol to this method, for descriptive purposes. It’s only necessary to do so when you are using a system that reloads code.
Example of usage:
class MyModel
define_hook :before_move_to
before_move_to(:check_move_allowed){|o| o.allow_move?}
def move_to(there)
return if before_move_to == false
# move MyModel object to there
end
end
40 41 42 43 44 45 46 47 |
# File 'lib/sequel/model/deprecated_hooks.rb', line 40 def add_hook_type(*hooks) Deprecation.deprecate('Sequel::Model.add_hook_type', 'Use Model.plugin :hook_class_methods') hooks.each do |hook| @hooks[hook] = [] instance_eval("def #{hook}(method = nil, &block); add_hook(:#{hook}, method, &block) end", __FILE__, __LINE__) class_eval("def #{hook}; run_hooks(:#{hook}); end", __FILE__, __LINE__) end end |
#has_hooks?(hook) ⇒ Boolean
Returns true if there are any hook blocks for the given hook.
50 51 52 |
# File 'lib/sequel/model/deprecated_hooks.rb', line 50 def has_hooks?(hook) !@hooks[hook].empty? end |
#hook_blocks(hook) ⇒ Object
Yield every block related to the given hook.
55 56 57 |
# File 'lib/sequel/model/deprecated_hooks.rb', line 55 def hook_blocks(hook) @hooks[hook].each{|k,v| yield v} end |
#inherited(subclass) ⇒ Object
59 60 61 62 63 |
# File 'lib/sequel/model/deprecated_hooks.rb', line 59 def inherited(subclass) super hooks = subclass.instance_variable_set(:@hooks, {}) instance_variable_get(:@hooks).each{|k,v| hooks[k] = v.dup} end |