I'm in the process of creating a library that powers a network of APIs. The center of that library is currently a Client class which each API client subclasses. Since I'm the one writing all my APIs, they'll all function similarly (restful, authorization through access_token, etc).
However unlike other ruby API Client libraries (Twitter's, etc), the client class should not be instantiated directly. That's because the library isn't restricted to a single API. Instead, each API client will subclass the Client class. My question is as follows:
Is there a way to require that a Ruby Class is only initialized through a subclass?
Additionally, in reading this question I decided that a class is better over a mixin here.
For those that want code, here's an example:
class A
def initialize(options = {})
#what goes on in here doesn't really matter for the purpose of this question
#I just don't want it to be initialized directly
options.each do |k,v|
instance_variable_set("@#{k}",v) unless v.nil?
end
end
end
class B < A
attr_accessor :class_specific_opt
def initialize( options = {} )
@class_specific_opt = "val" unless options[:class_specific_opt].nil?
super(options)
end
end
Any thoughts?