Class: Assertion::Inverter

Inherits:
Object
  • Object
show all
Defined in:
lib/assertion/inverter.rb

Overview

Builds inversions for instances of some ‘Assertion::Base` subclass

Examples:

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)"]>

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#sourceClass (readonly)

Returns The ‘Assertion::Base` sublcass to build negators for.

Returns:

  • (Class)

    The ‘Assertion::Base` sublcass to build negators for



24
25
26
# File 'lib/assertion/inverter.rb', line 24

def source
  @source
end

Class Method Details

.[](hash = {}) ⇒ Check::State

Initializes an assertion, builds its inversion, and applies it to the data

Returns:

  • (Check::State)

    The state of the assertion being applied to its attributes



56
57
58
# File 'lib/assertion/inverter.rb', line 56

def [](hash = {})
  new(hash).call
end

.initialize(source) ⇒ Object



35
36
37
38
# File 'lib/assertion/inverter.rb', line 35

def initialize(source)
  @source = source
  freeze
end

.new(hash = {}) ⇒ Assertion::Inverter::Inversion

Initializes a [#source] object and builds a negator for it

Parameters:

  • hash (Hash) (defaults to: {})

    The hash of attributes to apply the assertion to

Returns:

  • (Assertion::Inverter::Inversion)


# File 'lib/assertion/inverter.rb', line 26