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.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disable=(v) ⇒ Object



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

def self.disable=(v)
  @disable = v
end

.disabled?Boolean



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

def self.disabled?
  @disable
end

.enabled?Boolean



27
28
29
# File 'lib/finalist.rb', line 27

def self.enabled?
  !disabled?
end

.extended(base) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/finalist.rb', line 31

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

.finalized_methodsObject



40
41
42
# File 'lib/finalist.rb', line 40

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

Instance Method Details

#method_added(symbol) ⇒ Object



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

def method_added(symbol)
  super

  return if Finalist.disabled?

  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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/finalist.rb', line 122

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