Abstract classes in Ruby
Ruby
Switch branches/tags
Latest commit 8541b78 Mar 31, 2015 @shuber Merge pull request #1 from PikachuEXE/patch-1
Improve Travis CI speed and add more MRI versions

README.md

Sean Huber abstract_class

Build Status Code Climate Coverage Gem Version

Abstract classes in Ruby.

Like modules, abstract classes cannot be instantiated.

Unlike modules, abstract classes can be inherited and their derived classes can be instantiated.

Check out the java or php implementations for additional examples.

Installation

gem install abstract_class

Requirements

Ruby 1.8.7+

Usage

To make a class abstract, simply extend the AbstractClass module.

module ActiveRecord
  class Base
    extend AbstractClass
  end
end

Any attempts to initialize or allocate an instance of an abstract class raises AbstractClass::Error.

ActiveRecord::Base.new      #=> AbstractClass::Error - abstract class ActiveRecord::Base can't be instantiated
ActiveRecord::Base.allocate #=> AbstractClass::Error - abstract class ActiveRecord::Base can't be allocated

Child classes can inherit from an abstract class.

class User < ActiveRecord::Base
end

Instantiation and allocation behaves like normal for descendants of abstract classes.

User.new      #=> #<User:0x003d066d5a861d>
User.allocate #=> #<User:0x007f87588491d0>

API

YARD Documentation

Testing

bundle exec rspec

Contributing

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

License

MIT - Copyright © 2011 Sean Huber