Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/type_toolkit/ext/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ class Module
def interface!
TypeToolkit.make_interface!(self)
end

def abstract!
super if defined?(super)
TypeToolkit.make_abstract!(self)
end
end
8 changes: 8 additions & 0 deletions lib/type_toolkit/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def make_interface!(mod)
mod.extend(TypeToolkit::MethodDefRecorder)
mod.extend(TypeToolkit::HasAbstractMethods)
end

#: (Class[top]) -> void
def make_abstract!(klass)
klass.extend(TypeToolkit::DSL)
klass.extend(TypeToolkit::MethodDefRecorder)
klass.extend(TypeToolkit::HasAbstractMethods)
klass.include(TypeToolkit::AbstractInstanceMethodReceiver)
end
end

# This module is extended onto any module that represents an interface.
Expand Down
36 changes: 36 additions & 0 deletions spec/interface_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,42 @@ def something_else; end
end
end

describe "abstract!" do
it "makes a class abstract" do
klass = Class.new do
abstract!

abstract def my_method = assert_never_called!
end

assert klass.abstract_method?(:my_method)
end

it "delegates to an existing abstract! if one is defined" do
called = false

# Simulate another gem (e.g. Rails) defining abstract! on Object,
# which is Module's superclass. Module#abstract! should call super to reach it.
Object.define_method(:abstract!) { called = true }

klass = Class.new
klass.abstract!

assert called, "expected the other abstract! to have been called via super"
assert_respond_to klass, :abstract_instance_methods
ensure
Object.remove_method(:abstract!)
end

it "works when no parent abstract! exists" do
klass = Class.new do
abstract!
end

assert_respond_to klass, :abstract_instance_methods
end
end

describe "A class that fully implements the interface, partially via inheritance" do
before do
@class = PartiallyInheritsItsImpl
Expand Down