Module: Finalizable::Finalization

Defined in:
lib/finalizable/finalization.rb

Overview

Class methods

Constant Summary collapse

@@final_methods =
{}
@@somewhat_final_methods =
{}

Instance Method Summary collapse

Instance Method Details

#final(*names) ⇒ Object



10
11
12
# File 'lib/finalizable/finalization.rb', line 10

def final(*names)
  @@final_methods[self] = names
end

#method_added(method_name) ⇒ Object



18
19
20
21
# File 'lib/finalizable/finalization.rb', line 18

def method_added(method_name)
  super(method_name)
  prevent_override?(method_name)
end

#prevent_override?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/finalizable/finalization.rb', line 23

def prevent_override?(method_name)
  raise_error_if_prohibited_methods_is_overridden(method_name)
  warn_if_prohibited_methods_is_overridden(method_name)
end

#raise_error_if_prohibited_methods_is_overridden(method_name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/finalizable/finalization.rb', line 28

def raise_error_if_prohibited_methods_is_overridden(method_name)
  @@final_methods.each do |class_name, final_methods|
    ancestors = self.ancestors.dup
    ancestors.shift # remove myself from the list
    if ancestors.include?(class_name) && final_methods.include?(method_name)
      raise Finalizable::OverrideError, "Child class '#{self}' should not override parent class method '#{class_name}.#{method_name}'."
    end
  end
end

#somewhat_final(*names) ⇒ Object



14
15
16
# File 'lib/finalizable/finalization.rb', line 14

def somewhat_final(*names)
  @@somewhat_final_methods[self] = names
end

#warn_if_prohibited_methods_is_overridden(method_name) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/finalizable/finalization.rb', line 38

def warn_if_prohibited_methods_is_overridden(method_name)
  @@somewhat_final_methods.each do |class_name, final_methods|
    ancestors = self.ancestors.dup
    ancestors.shift # remove myself from the list
    if ancestors.include?(class_name) && final_methods.include?(method_name)
      warn "WARNING: Child class '#{self}' should not override parent class method '#{class_name}.#{method_name}'."
    end
  end
end