module AyeCommander::Hookable::ClassMethods

All hook functionality is defined at a class level, but runs at instance level

Constants

TYPES

Private Instance Methods

callable_hooks(kind, command) click to toggle source

Makes all the saved hooks callable in the command context

# File lib/aye_commander/hookable.rb, line 68
def callable_hooks(kind, command)
  hooks[kind].map do |hook|
    case hook
    when Symbol
      command.method(hook)
    when Proc
      ->(*args) { command.instance_exec(*args, &hook) }
    when Method
      hook
    end
  end
end
hooks() click to toggle source

Hash that saves the hooks

# File lib/aye_commander/hookable.rb, line 44
def hooks
  @hooks ||= Hash.new([])
end
prepare_hooks(kind, command) click to toggle source

Prepares the hooks so they can just be called. Before after and around hooks are similar in the sense that they just need to make all the received hooks callable and then they call themselves.

Arounds on the other hand… they basically wrap themselves in procs so that you can call the proc inside the proc that gives the proc. Quite a headache. Why would you need multiple around blocks in the first place?

# File lib/aye_commander/hookable.rb, line 57
def prepare_hooks(kind, command)
  hooks = callable_hooks(kind, command)
  return hooks unless kind == :around

  around_proc = hooks.reverse.reduce(command) do |callable, hook|
    -> { hook.call(callable) }
  end
  [around_proc]
end