Module: Finalist

Defined in:
lib/finalist.rb,
lib/finalist/version.rb

Defined Under Namespace

Modules: ModuleMethods, SyntaxMethods Classes: OverrideFinalMethodError

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



17
18
19
20
21
22
# File 'lib/finalist.rb', line 17

def self.extended(base)
  super
  base.extend(SyntaxMethods)
  base.singleton_class.extend(SyntaxMethods)
  base.extend(ModuleMethods) if base.instance_of?(Module)
end

.finalized_methodsObject



24
25
26
# File 'lib/finalist.rb', line 24

def self.finalized_methods
  @finalized_methods ||= {}
end

Instance Method Details

#method_added(symbol) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/finalist.rb', line 95

def method_added(symbol)
  super

  meth = 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(":")}", self, super_method.owner, meth, :method_added)
    end
    super_method = super_method.super_method
  end
end

#singleton_method_added(symbol) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/finalist.rb', line 108

def 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)
      raise OverrideFinalMethodError.new("#{super_method} at #{super_method.source_location.join(":")} is overrided\n  by #{meth} at #{meth.source_location.join(":")}", self, super_method.owner, meth, :singleton_method_added)
    end
    super_method = super_method.super_method
  end
end