Class: Handlers::BeforeSaveHooks

Inherits:
Base
  • Object
show all
Defined in:
lib/handlers/hooks.rb

Instance Method Summary collapse

Methods inherited from Base

handle, #initialize, #raise_error, #upper

Constructor Details

This class inherits a constructor from Handlers::Base

Instance Method Details

#handleObject



5
6
7
# File 'lib/handlers/hooks.rb', line 5

def handle
  handle_hooks('before_save')
end

#handle_block(block) ⇒ Object



21
22
23
# File 'lib/handlers/hooks.rb', line 21

def handle_block(block)
  @klass.instance_eval(&block)
end

#handle_hooks(hook_name) ⇒ Object



9
10
11
12
13
# File 'lib/handlers/hooks.rb', line 9

def handle_hooks(hook_name)
  (upper("@@#{hook_name}") || []).each do |method_or_block|
    handle_single(method_or_block)
  end
end

#handle_method(method) ⇒ Object



25
26
27
28
# File 'lib/handlers/hooks.rb', line 25

def handle_method(method)
  raise_error("could not find hook method named #{method}") unless @klass.respond_to?(method)
  @klass.send(method)
end

#handle_single(method_or_block) ⇒ Object



15
16
17
18
19
# File 'lib/handlers/hooks.rb', line 15

def handle_single(method_or_block)
  raise_error('a hook seems to be nil') if method_or_block.nil?

  method_or_block.respond_to?(:call) ? handle_block(method_or_block) : handle_method(method_or_block)
end