0.0
No release in over 3 years
Low commit activity in last 3 years
Extends Ruby to support class interfaces
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.14
>= 12.3.3
~> 3.0
 Project Readme

class_interface

Gem downloads License: MIT

Ruby gem to extend Ruby to support class interfaces you may know them from other programming languages like C++ or Java.

Raises a variety of different exceptions when the requirements of the interface are not met.

That can be very handy in teams to declare requirements for specific types of classes.

Contents

  • Installation
  • Usage
  • Documentation
  • Contributing

Installation

Add this line to your application's Gemfile:

gem 'class_interface'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install class_interface

Usage

Defining an interface

It is a good idea, to start an interface name by convention with an I, following by a capitalized camel case constant name, i.e.: IMyInterface, IHouse, IClassName, ...

class IExample
  MIN_AGE = Integer
  DEFAULT_ENV = String
  SOME_CONSTANT = nil

  def self.some_static_method
  end

  def some_instance_method
  end
end

Implementing an interface

class MyImplementation
  MIN_AGE = 21
  DEFAULT_ENV = 'dev' 
  SOME_CONSTANT = 'some_value'
  
  def specific_method
    puts "very specific"
  end
  
  def self.some_static_method
    puts "static method is implemented!"
  end
  
  def some_instance_method
    # implementation
  end
  
  def self.another_methods
    # implementation
  end
  
  implements IExample
end

Documentation

#implements(InterfaceClassConstant)

InterfaceClassConstant must be a valid InterfaceClassConstant or a String, containing a valid InterfaceClassConstant, i.e.: IMyInterface / "IMyInterface"

Methods

All defined methods in the interface class must be implemented in the implementing class. The parameter count must be the same. A distinction is made between static and dynamic methods.

Constant Types

All CONSTANTS defined in the interface class must be implemented in the implementing class. CONSTANTS of interfaces may be defined with nil, to allow all types of definitions in the implementing class.

Otherwise to specify a type, assign its class constant, e.g. String, Array, MyCustomClass, ... If a specified type is defined, it is mandatory for the implementation to use that type.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/class_interface. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.