Module: Finalist::ModuleMethods

Defined in:
lib/finalist.rb

Instance Method Summary collapse

Instance Method Details

#extended(base) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/finalist.rb', line 68

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

    return if Finalist.disabled?

    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)
        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)
            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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/finalist.rb', line 45

def included(base)
  super

  return if Finalist.disabled?

  base.extend(Finalist)

  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)
            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, :included)
          end

          super_method = super_method.super_method
        end
      end
    end
  end
end