Sha256: b23d3943d6655a3ed83e3aeb345ae625796e8303e36852c8f4bb3acab04b61b4

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

# encoding: utf-8

module Assertion

  # Describes the inversion of the assertion object
  #
  # The inversion decorates the source assertion switching its message
  # (from falsey to truthy) and reverting its `check`.
  #
  # @example
  #   IsAdult = Assertion.about :name, :age do
  #     age >= 18
  #   end
  #
  #   assertion = IsAdult.new
  #   inversion = Inversion.new(assertion)
  #
  #   assertion.call.valid? == inversion.call.invalid? # => true
  #
  # @api private
  #
  class Inversion < Base

    # @!scope class
    # @!method new(assertion)
    # Creates the inversion for the selected assertion object
    #
    # @param [Assertion::Base] assertion The assertion being inverted
    #
    # @return [Assertion::Inversion]

    # @private
    def initialize(assertion)
      @assertion = assertion
      freeze
    end

    # @!attribute [r] assertion
    #
    # @return [Assertion::Base] The assertion being inverted
    #
    attr_reader :assertion

    # The translated message describing the state of assertion
    #
    # @param [Boolean] state
    #
    # @return [String]
    #
    def message(state = nil)
      assertion.message !state
    end

    # Checks the current state of the assertion
    #
    # @return [Boolean]
    #
    def check
      !assertion.check
    end

  end # class Inversion

end # module Assertion

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assertion-0.2.1 lib/assertion/inversion.rb
assertion-0.2.0 lib/assertion/inversion.rb