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
1 change: 1 addition & 0 deletions lib/virtual.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'virtual/method_defined'
require 'virtual/virtual_method'
require 'virtual/pure_method'
require 'virtual/protocol_method'
Expand Down
15 changes: 15 additions & 0 deletions lib/virtual/method_defined.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Virtual
module MethodDefined
def self.call(target_class, method_name)
target_class.method_defined?(method_name, true) ||
target_class.private_method_defined?(method_name, true)
end

module SourceLocation
def self.get(target_class, method_name)
method = target_class.instance_method(method_name)
method.source_location.join(':')
end
end
end
end
5 changes: 5 additions & 0 deletions lib/virtual/pure_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ module PureMethod
Error = Class.new(RuntimeError)

def self.define(target_class, method_name)
if MethodDefined.(target_class, method_name)
method_source_location = MethodDefined::SourceLocation.get(target_class, method_name)
raise Error, "Pure virtual (abstract) method #{method_name} of #{target_class.name} must not already be implemented (Source Location: #{method_source_location})"
end

target_class.send(:define_method, method_name) do |*args|
raise Error, "Pure virtual (abstract) method #{method_name} of #{self.class.name} must be implemented"
end
Expand Down
5 changes: 5 additions & 0 deletions lib/virtual/virtual_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def self.define(target_class, method_name, &blk)
blk ||= proc do |*|
end

if MethodDefined.(target_class, method_name)
method_source_location = MethodDefined::SourceLocation.get(target_class, method_name)
raise Error, "Virtual (abstract) method #{method_name} of #{target_class.name} must not already be implemented (Source Location: #{method_source_location})"
end

target_class.send(:define_method, method_name, &blk)
end
end
Expand Down