module Rooftop::HookCalls::ClassMethods

Public Instance Methods

add_to_hook(hook, block) click to toggle source

Add something to the list of hook calls, for a particular hook. This is called in other mixins where something is being added to a hook (probably :after_find). For example Rooftop::FieldAliases and Rooftop::FieldCoercions

# File lib/rooftop/hook_calls.rb, line 16
def add_to_hook(hook, block)
  # get existing hook calls
  hook_calls = instance_variable_get(:"@hook_calls")
  # add new one for the appropriate hook
  if hook_calls[hook].is_a?(Array)
    hook_calls[hook] << block
  else
    hook_calls[hook] = [block]
  end
  instance_variable_set(:"@hook_calls",hook_calls)
end
setup_hooks!() click to toggle source

A method to call the hooks. This iterates over each of the types of hook (:after_find, :before_save etc, identified here: github.com/remiprev/her#callbacks) and sets up the actual hook. All this is worth it to control order.

# File lib/rooftop/hook_calls.rb, line 31
def setup_hooks!
  instance_variable_get(:"@hook_calls").each do |type, calls|
    calls.each do |call|
      self.send(type, call)
    end
  end
end