Sha256: e3bcbf7d3c408767f03aa9fce542a61622724d0e2a399353a3385624cacab5fd

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# encoding: utf-8

module Assertion

  # Builds inversions for instances of some `Assertion::Base` subclass
  #
  # @example
  #   Adult = Assertion.about :name, :age do
  #     age >= 18
  #   end
  #
  #   joe = OpenStruct.new(name: "Joe", age: 40)
  #
  #   child = Inverter.new(Adult)
  #   child[name: "Joe"].validate!
  #   # => #<Assertion::InvalidError @messages=["Joe is an adult (age 40)"]>
  #
  class Inverter

    # @!attribute [r] source
    #
    # @return [Class] The `Assertion::Base` sublcass to build negators for
    #
    attr_reader :source

    # @!scope class
    # @!method new(source)
    # Creates an immutable inversion object for the `Assertion::Base` subclass
    #
    # @param [Class] source
    #
    # @return [Assertion::Inverter]

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

    # Initializes a [#source] object and builds a negator for it
    #
    # @param [Hash] hash The hash of attributes to apply the assertion to
    #
    # @return [Assertion::Inverter::Inversion]
    #
    def new(hash = {})
      Inversion.new source.new(hash)
    end

    # Initializes an assertion, builds its inversion, and applies it to the data
    #
    # @param (see #new)
    #
    # @return (see Assertion::Base#call)
    #
    def [](hash = {})
      new(hash).call
    end

  end # class Inverter

end # module Assertion

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assertion-0.1.0 lib/assertion/inverter.rb
assertion-0.0.1 lib/assertion/inverter.rb