Skip to content

michaeljklein/nil-passer

Repository files navigation

Nil-Passer

Status

Build Status

Maintainability

Test Coverage

Gem Version

Codacy Badge

Gitter

Introduction

Ever been sick of blocks not handling nil in Ruby, or more specifically Rails?

This gem defines the NilPasser class, which can be used to monitor all passing of blocks on an object, through a specific method, or to a class.

Each block passed is passed nil and the execution usually continues normally. (Usually meaning you probably don't want to use this in production.)

During or after execution, you can grep '^[no_nil]' on your logs to get all the results.

Loosely inspired by the bullet gem.

Examples

From the tests, we have example usage for "good, bad, and subtle" blocks:

Good

This is the control: we provide the caller's location and the identity (function) block (source):

  def test_test_ignores_good_block
    assert @log.blank?
    NilPasser.test [Rails.path], Proc.new{|x| x}
    assert @log.blank?
  end

Bad

This is a simple exception-handling case, where the block accepts nil but always raises an exception (source):

  def test_test_catches_bad_block
    assert  @log.blank?
    NilPasser.test [Rails.path], Proc.new{|x| (raise "hi")}
    assert [email protected]?
  end

Subtle

This is an example of a block that only raises an exception when passed nil (source):

  def test_test_catches_subtle_block
    assert  @log.blank?
    NilPasser.test [Rails.path], Proc.new{|x| x.nil? && (raise "hi")}
    assert [email protected]?
  end

Releases

No releases published

Packages

No packages published

Languages