Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

conditionz

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

conditionz

  • 0.0.8
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

conditionz

Description

Semantic Design-By-Contract pre-conditions and post-conditions.

The purpose is to raise an error when a certain condition or more are not met.

See

http://en.wikipedia.org/wiki/Design_by_contract

http://en.wikipedia.org/wiki/Precondition

http://en.wikipedia.org/wiki/Postcondition

Installation

Add to your Gemfile:

gem 'conditionz'

Then run:

bundle install

Usage

Suppose you have the following class:

class MyTime
  attr_accessor :hour, :minute

  def initialize(new_hour, new_minute)
    hour = new_hour
    minute = new_minute
  end
end

Now you wish that initialize method parameters are within a certain range. Include the pre condition, and add the precondition

include Conditionz

class MyTime
  attr_accessor :hour, :minute

  def initialize(new_hour, new_minute)
    Precondition.require 0 <= new_hour && new_hour <= 23, "Hour must be between 0 and 23"
    Precondition.require 0 <= new_minute && new_minute <= 59, "Minute must be between 0 and 59"

    hour = new_hour
    minute = new_minute
  end
end

Notice the call to Precondition.require, which receives a predicate (a boolean expression), and an optional message string or proc.

Here's an example with proc:

Precondition.require 0 <= new_hour && new_hour <= 23, Proc.new { "Hour must be between 0 and 23 but got #{new_hour}" }

Analogously, you can ensure post conditions: (In this example, instead of include, the fully qualified name is used

class MyQueue
  attr_accessor :size

  def push(item)
    # some implementation of pushing to queue...

    Conditionz::PostCondition.ensure size > 0
  end
end

Not directly related, and preferably unit tested, but could be useful - assert a predicate

def foo
  first_result = first_calculation()
  Assert.that first_result > 0, "result should have been positive"
  second_result = sqrt first_result
end

License

see MIT-LICENSE

FAQs

Package last updated on 07 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc