Class: Startback::Support::Hooks

Inherits:
Module
  • Object
show all
Defined in:
lib/startback/support/hooks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suffix) ⇒ Hooks

Returns a new instance of Hooks.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/startback/support/hooks.rb', line 5

def initialize(suffix)
  @suffix = suffix
  define_method :"before_#{suffix}" do
    self.class.__befores.each do |bl|
      instance_exec(&bl)
    end
  end
  define_method :"after_#{suffix}" do
    self.class.__afters.each do |bl|
      instance_exec(&bl)
    end
  end
end

Instance Attribute Details

#suffixObject (readonly)

Returns the value of attribute suffix.



18
19
20
# File 'lib/startback/support/hooks.rb', line 18

def suffix
  @suffix
end

Instance Method Details

#included(by) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/startback/support/hooks.rb', line 20

def included(by)
  by.instance_eval %Q{
    def __befores(create = false)
      if create
        @__befores ||= (superclass.respond_to?(:__befores, false) ? superclass.__befores.dup : [])
      end
      @__befores || (superclass.respond_to?(:__befores, false) ? superclass.__befores : [])
    end

    def __afters(create = false)
      if create
        @__afters ||= (superclass.respond_to?(:__afters, false) ? superclass.__afters.dup : [])
      end
      @__afters || (superclass.respond_to?(:__afters, false) ? superclass.__afters : [])
    end

    def before_#{suffix}(&bl)
      __befores(true) << bl
    end

    def after_#{suffix}(&bl)
      __afters(true) << bl
    end
  }
end