Abstractize
A simple mixin for abstract class functionality in ruby
Installation
Add this line to your application's Gemfile:
gem 'abstractize'
And then execute:
$ bundle
Or install it yourself as:
$ gem install abstract
Usage
require 'abstractize'
class Vehicle
include Abstractize
attr_reader :wheels, :mileage
def initialize(wheels=4)
@mileage = 0
@wheels = wheels
end
define_abstract_method :drive
end
class Car < Vehicle
end
class Tricycle < Vehicle
def initialize
super(3)
end
def drive
@mileage += 0.01
end
end
vehicle = Vehicle.new(2)
# AbstractError: cannot instanciate Vehicle
car = Car.new
# => #<Car:0x007f5e130631f0 @mileage=0, @wheels=4>
car.drive
# AbstractError: not implemented
tricycle = Tricycle.new
# => #<Tricycle:0x007f5e13093148 @mileage=0, @wheels=3>
tricycle.drive
# => 0.01
tricycle.drive
# => 0.02
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
- Fork it ( https://github.com/[my-github-username]/abstract/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request