Module: Hatenablog::AfterHook

Included in:
Entry
Defined in:
lib/hatenablog/entry.rb

Instance Method Summary collapse

Instance Method Details

#after_hook(hook, *methods) ⇒ Object

Register a hooking method for given methods. The hook method is executed after calling given methods.

Parameters:

  • hooking (Symbol)

    method name

  • hooked (Array)

    methods name array



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hatenablog/entry.rb', line 13

def after_hook(hook, *methods)
  methods.each do |method|
    origin_method = "#{method}_origin".to_sym
    if instance_methods.include? origin_method
      raise NameError, "#{origin_method} isn't a unique name"
    end

    alias_method origin_method, method

    define_method(method) do |*args, &block|
      # @type var block: ^(*untyped) -> untyped
      result = send(origin_method, *args, &block)
      send(hook)
      result
    end
  end
end