Module: Finalist::ModuleMethods

Defined in:
lib/finalist.rb

Instance Method Summary collapse

Instance Method Details

#extended(base) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/finalist.rb', line 59

def extended(base)
  def base.singleton_method_added(symbol)
    super

    meth = singleton_class.instance_method(symbol)
    super_method = meth.super_method
    while super_method
      if Finalist.finalized_methods[super_method.owner]&.member?(super_method.name)
        @__finalist_tp&.disable
        @__finalist_tp = nil
        raise OverrideFinalMethodError.new("#{super_method} at #{super_method.source_location.join(":")} is overrided\n  by #{meth} at #{meth.source_location.join(":")}", singleton_class, super_method.owner, meth, :extended_singleton_method_added)
      end
      super_method = super_method.super_method
    end
  end

  base.singleton_class.ancestors.drop(1).each do |mod|
    Finalist.finalized_methods[mod]&.each do |fmeth_name|
      if meth = base.singleton_class.instance_method(fmeth_name)
        super_method = meth.super_method
        while super_method
          if Finalist.finalized_methods[super_method.owner]&.member?(super_method.name)
            base.instance_variable_get("@__finalist_tp")&.disable
            base.instance_variable_set("@__finalist_tp", nil)

            raise OverrideFinalMethodError.new("#{super_method} at #{super_method.source_location.join(":")} is overrided\n  by #{meth} at #{meth.source_location.join(":")}", base.singleton_class, super_method.owner, meth, :extended)
          end
          super_method = super_method.super_method
        end
      end
    end
  end
end

#included(base) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/finalist.rb', line 29

def included(base)
  super

  base.extend(Finalist)

  tp = TracePoint.new(:end) do |ev|
    if ev.self == base
      base.ancestors.drop(1).each do |mod|
        Finalist.finalized_methods[mod]&.each do |fmeth_name|
          if meth = base.instance_method(fmeth_name)
            super_method = meth.super_method
            while super_method
              if Finalist.finalized_methods[super_method.owner]&.member?(super_method.name)
                tp.disable
                base.instance_variable_set("@__finalist_tp", nil)
                raise OverrideFinalMethodError.new("#{super_method} at #{super_method.source_location.join(":")} is overrided\n  by #{meth} at #{meth.source_location.join(":")}", base, super_method.owner, meth, :trace_point)
              end

              super_method = super_method.super_method
            end
          end
        end
      end
      tp.disable
    end
  end
  tp.enable
  base.instance_variable_set("@__finalist_tp", tp)
end