Git Product home page Git Product logo

rint's Introduction

Gem Version build status coverage report

Rint (Ruby Interface)

Rint provides a way to implement behaviour defined by Interface files, warning when the desired behaviour is missing.

Example:

require 'interface'
module Playable
  include Interface

  def initialize
    must_implement :play, play_quietly: 1
  end
end

class Instrument
  implements Playable
end

Instrument.new # will throw: Interface::Error::NotImplementedError: Expected Instrument to implement play for interface Playable

As showed in the example above,must_implement allows you enforce the implementation of the methods, specifying the arity. When not specified, only the method implementation will be checked.

There is also a CLI to generate the interfaces from the command line:

$ rint c Playable play play_quietly:1

will generate lib/playable.rb (namespaces are also supported).

Installation

Add it to your Gemfile:

gem 'rint'

and run

$ bundle install

Goals

Duck typing can be evil if not used correctly. While Ruby applications can get a good level of confidence about issues arisen by duck typing, the fear of a method missing exception or constant changes to a class may lead the developer to use safety checks such as respond_to? or raise Errors when a method is missing.

This implementation of rint wraps what other static languages use and encapsulates the safety check in a common place, also making explicit the behaviour through the word "implements" and expecting the developer to guess the behaviour of a class without looking at docs or partially implemented code.

It also provides a centralised way to implement the interface pattern without having the code throwing an error in a hidden method.

Options

The environment variable DISABLE_RUBY_INTERFACE can be set to 1 in order to globally disable the interfaces - no Error will get thrown. This might be particularly useful in production for performance reasons if we are confident enough through tests that the interfaces are all implemented.

Contributing

  • Fork it

  • 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 new Pull Request

  • Enjoy!

License

MIT

Free Software, Hell Yeah!

Copyright 2015 James Lopez https://github.com/bluegod/rint

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.