Class Index [+]

Quicksearch

Sequel::Plugins::HookClassMethods::ClassMethods

Public Instance Methods

add_hook_type(*hooks) click to toggle source

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 before 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
    # File lib/sequel/plugins/hook_class_methods.rb, line 72
72:         def add_hook_type(*hooks)
73:           Model::HOOKS.concat(hooks)
74:           hooks.each do |hook|
75:             @hooks[hook] = []
76:             instance_eval("def #{hook}(method = nil, &block); add_hook(:#{hook}, method, &block) end", __FILE__, __LINE__)
77:             class_eval("def #{hook}; model.hook_blocks(:#{hook}){|b| return false if instance_eval(&b) == false}; end", __FILE__, __LINE__)
78:           end
79:         end
has_hooks?(hook) click to toggle source

Returns true if there are any hook blocks for the given hook.

    # File lib/sequel/plugins/hook_class_methods.rb, line 82
82:         def has_hooks?(hook)
83:           !@hooks[hook].empty?
84:         end
hook_blocks(hook) click to toggle source

Yield every block related to the given hook.

    # File lib/sequel/plugins/hook_class_methods.rb, line 87
87:         def hook_blocks(hook)
88:           @hooks[hook].each{|k,v| yield v}
89:         end
inherited(subclass) click to toggle source

Make a copy of the current class’s hooks for the subclass.

    # File lib/sequel/plugins/hook_class_methods.rb, line 92
92:         def inherited(subclass)
93:           hooks = subclass.instance_variable_set(:@hooks, {}) 
94:           instance_variable_get(:@hooks).each{|k,v| hooks[k] = v.dup}
95:           super
96:         end

Private Instance Methods

add_hook(hook, tag, &block) click to toggle source

Add a hook block to the list of hook methods. If a non-nil tag is given and it already is in the list of hooks, replace it with the new block.

     # File lib/sequel/plugins/hook_class_methods.rb, line 103
103:         def add_hook(hook, tag, &block)
104:           unless block
105:             (raise Error, 'No hook method specified') unless tag
106:             block = proc {send tag}
107:           end
108:           h = @hooks[hook]
109:           if tag && (old = h.find{|x| x[0] == tag})
110:             old[1] = block
111:           else
112:             if hook.to_s =~ /^before/
113:               h.unshift([tag,block])
114:             else
115:               h << [tag, block]
116:             end
117:           end
118:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.