Sha256: 7849abec2b408d4399b8e9dc32018173bb672640c7b3218c9fcf542c89ca237b

Contents?: true

Size: 1.75 KB

Versions: 70

Compression:

Stored size: 1.75 KB

Contents

require "insist/namespace"

require "insist/assert"
require "insist/comparators"
require "insist/enumerables"
require "insist/failure"
require "insist/nil"
require "insist/predicates"
require "insist/raises"

# Insist on correctness.
#
#
# Example:
#
#     data = { "hello" => "world" }
#     insist { data["hello"] } == "world"
#
# This class aims to work similarly to how rspec's "should" stuff does, but
# instead of molesting Object allows you to neatly wrap values with blocks
# while still checking for expected values.
class Insist
  class Failure < StandardError; end

  include Insist::Comparators
  include Insist::Enumerables
  include Insist::Nil
  include Insist::Assert
  include Insist::Raises
  include Insist::Predicates

  # Create a new insist with a block.
  #
  # Example:
  #
  #   Insist.new { value }
  #
  # Better:
  #
  #   insist { value }
  def initialize(&block)
    @callback = block
  end # def initialize

  def value
    # TODO(sissel): make caching the value optional
    @value ||= @callback.call
    return @value
  end # def value
end # class Insist

# Like Insist, but rejects (fails) on truthy values instead falsey ones.
class Reject < Insist
  def assert(truthy, message)
    raise Insist::Failure.new(message) if truthy
  end # def assert
end # class Reject

module Kernel
  # A shortcut to 'Insist.new'
  #
  # Example:
  #
  #     insist { "hello world" } != "fizzle"
  def insist(&block)
    return Insist.new(&block)
  end # def insist

  # A shortcut to 'Reject.new'
  #
  # Example:
  #
  #     # This will fail (raises Insist::Failure)
  #     reject { [1,2,3,4] }.include?(3)
  #
  #     # This will succeed
  #     reject { [1,2,3,4] }.include?(4)
  def reject(&block)
    return Reject.new(&block)
  end # def reject
end # module Kernel

Version data entries

70 entries across 66 versions & 18 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/insist-1.0.0/lib/insist.rb