I'm writing a framework for querying the Mediawiki API. I have a Page
class which represents articles on the wiki, and I've also got a Category
class, which is-a Page
with more specific methods (like being able to count the number of members in the category. I've also got a method Page#category?
which determines if an instantiated Page
object is actually representative of a Mediawiki category page, by querying the API to determine the namespace of the article.
class Page
def initialize(title)
# do initialization stuff
end
def category?
# query the API to get the namespace of the page and then...
namespace == CATEGORY_NAMESPACE
end
end
class Category < Page
# ...
end
What I would like to do is be able to detect if the user of my framework tries to instantiate a Mediawiki category using a Page object (ie. Page.new("Category:My Category")
), and if so, instantiate a Category
object, instead of a Page
object, directly from the Page
constructor.
It seems to me that this should be possible because it's reminiscent of single table inheritance in Rails, but I'm not sure how to go about getting it to work.