https://github.com/magynhard/class_interface
Ruby gem to provide an interface pattern for classes
https://github.com/magynhard/class_interface
class-interface interface interface-pattern pattern ruby ruby-gem
Last synced: about 1 year ago
JSON representation
Ruby gem to provide an interface pattern for classes
- Host: GitHub
- URL: https://github.com/magynhard/class_interface
- Owner: magynhard
- License: mit
- Created: 2019-03-02T11:20:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-08T18:49:41.000Z (about 3 years ago)
- Last Synced: 2025-05-01T18:06:04.923Z (about 1 year ago)
- Topics: class-interface, interface, interface-pattern, pattern, ruby, ruby-gem
- Language: Ruby
- Homepage:
- Size: 20.5 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# class_interface
[](https://rubygems.org/gems/class_interface)

[](LICENSE)
> 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](#installation)
* [Usage](#usage)
* [Documentation](#documentation)
* [Contributing](#contributing)
Add this line to your application's Gemfile:
```ruby
gem 'class_interface'
```
And then execute:
$ bundle install
Or install it yourself as:
$ gem install class_interface
### 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, ...`
```ruby
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
```ruby
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
```ruby
#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.
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](http://contributor-covenant.org) code of conduct.