Project

delivered

0.01
There's a lot of open issues
Simple runtime type checking for Ruby method signatures
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Delivered: Simple runtime type checking for Ruby method signatures

Signed, Sealed, Delivered 🎹

Delivered gives you the ability to define method signatures in Ruby, and have them checked at runtime. This is useful for ensuring that your methods are being called with the correct arguments, and for providing better error messages when they are not. It also serves as a nice way of documenting your methods using actual code instead of comments.

Usage

Simply define a method signature using the sig method directly before the method to be checked, and Delivered will check that the method is being called with the correct arguments and types.

class User
  extend Delivered::Signature

  sig String, age: Integer
  def create(name, age:)
    "User #{name} created with age #{age}"
  end
end

If an invalid argument is given to User#create, for example, if age is a String instead of the required Integer, a Delivered::ArgumentError exception will be raised.

Return Types

You can also check the return value of the method by passing a Hash with an Array as the key, and the value as the return type to check.

sig [String, age: Integer] => String
def create(name, age:)
  "User #{name} created with age #{age}"
end

Or by placing the return type in a block to sig.

sig(String, age: Integer) { String }
def create(name, age:)
  "User #{name} created with age #{age}"
end

Delivered Types

As well as Ruby's native types (ie. String, Integer, etc.), Delivered provides a couple of extra types in Delivered::Types.

You can call these directly with Delivered::Types.Boolean, or for brevity, assign Delivered::Types to T in your classes:

class User
  extend Delivered::Signature
  T = Delivered::Types
end

The following examples all use the T alias, and assumes the above.

Boolean

Value MUST be true or false. Does not support "truthy" or "falsy" values.

sig validate: T.Boolean
def create(validate:); end

Any

Value MUST be any of the given list of values, that is, the value must be one of the given list.

sig T.Any(:male, :female)
def create(gender); end

If no type is given, the value CAN be any type or value.

sig save: T.Any
def create(save: nil); end

You can also pass nil to allow a nil value alongside any other types you provide.

sig T.Any(String, nil)
def create(save = nil); end

Nilable

When a type is given, the value MUST be nil OR of the given type.

sig save: T.Nilable(String)
def create(save: nil); end

sig T.Nilable(String)
def update(name = nil); end

If no type is given, the value CAN be nil. This essentially allows any value, including nil.

sig save: T.Nilable
def create(save: nil); end

You may notice that Nilable is interchangeable with Any. The following are equivilent:

sig save: T.Nilable
def create(save: nil); end
sig save: T.Any
def create(save: nil); end

As are these:

sig T.Nilable(String)
def update(name = nil); end
sig T.Any(String, nil)
def update(name = nil); end