Module: Trax::Core::InheritanceHooks::ClassMethods

Defined in:
lib/trax/core/inheritance_hooks.rb

Instance Method Summary collapse

Instance Method Details

#after_inherited(&block) ⇒ Object



46
47
48
# File 'lib/trax/core/inheritance_hooks.rb', line 46

def after_inherited(&block)
  self.instance_variable_set(:@_after_inherited_block, block)
end

#inherited(subklass) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
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/trax/core/inheritance_hooks.rb', line 7

def inherited(subklass)
  super(subklass)

  if self.instance_variable_defined?(:@_on_inherited_block)
    subklass.instance_eval(&self.instance_variable_get(:@_on_inherited_block))
  end

  if subklass.class == Class
    #this supports anonymous classes created with a block passed, i.e.
    # Class.new(Enum, &Proc.new{VAL=1})
    # otherwise Class.new wont be caught by tracepoint statement below
    trace = ::TracePoint.new(:b_return) do |tracepoint|
      if tracepoint.self.class == Class
        if self.instance_variable_defined?(:@_after_inherited_block)
          subklass.instance_eval(&self.instance_variable_get(:@_after_inherited_block))
        end

        trace.disable
      end
    end

    trace.enable
  else
    trace = ::TracePoint.new(:end) do |tracepoint|
      if tracepoint.self == subklass
        if self.instance_variable_defined?(:@_after_inherited_block)
          subklass.instance_eval(&self.instance_variable_get(:@_after_inherited_block))
        end

        trace.disable
      end
    end

    trace.enable

    self
  end
end

#on_inherited(&block) ⇒ Object



50
51
52
# File 'lib/trax/core/inheritance_hooks.rb', line 50

def on_inherited(&block)
  self.instance_variable_set(:@_on_inherited_block, block)
end