No commit activity in last 3 years
No release in over 3 years
Plain Old Ruby Object Implementation of Contract
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0
~> 3.0
~> 0.58
~> 0.16
~> 0.9

Runtime

 Project Readme

Build Status Maintainability Cult Of Martians

SimpleContracts

Sponsored by Evil Martians

Plain Old Ruby Object Implementation of Contract

This project contains the most simple implementation of Contract written in Ruby (and maybe later in other languages).

The Contract is inspired by Design by Contracts approach and pushes Fail Fast techinque further.

So, Contract is a class with the only public method , that validates some action/behavior agains Contract Rules:

  • Guarantees - the rules that SHOULD be valid for each check of behavior
  • Expectations - list of all expected states that COULD be valid for the behavior check

Contract validates, that:

  • ALL Guarantees were met
  • AT LEAST ONE Expectations was met

Otherwise, Contract raises an exception with details, at least on what step behavior was broken.

Installation

Add this line to your application's Gemfile:

gem 'simple_contracts'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_contracts

Usage

  class TwitterContract < SimpleContracts::Base
    def initialize(post)
      super
      @post = post
    end

    private

    def guarantee_verified_delete
      return true if Twitter::REST::Client.statuses(@post.tweet_id).empty?
      false
    end

    def expect_some_action1
      ...
    end

    def expect_some_action2
      ...
    end

    # ... other rules
  end

  @post = Post.find(params.require(:post_id))

  # Use synchronously, (raises exception, "Fails Fast"™):
  TwitterContract.(@post, async: false) { TwitterAPI.destroy(@post) }

  # Use asynchronously (does not affect TwitterAPI.destroy,
  # but tracks any problems with TwitterContract validation)
  TwitterContract.(@post) { TwitterAPI.destroy(@post) }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bibendi/simple_contracts.

License

The gem is available as open source under the terms of the MIT License.