Class: Assertion::State

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

Overview

Describes the state of the assertion applied to given arguments (the result of the checkup)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.messagesArray<String> (readonly)

Returns error messages.

Returns:

  • (Array<String>)

    error messages



32
33
34
# File 'lib/assertion/state.rb', line 32

def messages
  @messages
end

Class Method Details

.&(other) ⇒ Assertion::State Also known as: >>, +

Composes the state with the other state

Parameters:

Returns:

  • (Assertion::State)

    The composed state that carries messages from both the states



71
72
73
# File 'lib/assertion/state.rb', line 71

def &(other)
  self.class.new(valid? & other.valid?, messages + other.messages)
end

.initialize(state, *messages) ⇒ Object



22
23
24
25
26
# File 'lib/assertion/state.rb', line 22

def initialize(state, *messages)
  @state = state
  @messages = (state ? [] : messages.flatten.uniq).freeze
  freeze
end

.invalid?Boolean

Check whether a stated assertion is not satisfied by its attributes

Returns:

  • (Boolean)


46
47
48
# File 'lib/assertion/state.rb', line 46

def invalid?
  !@state
end

.new(state, *messages) ⇒ Assertion::State

Creates the immutable state instance with a corresponding error messages

Parameters:

  • state (Boolean)
  • messages (String, Array<String>)

Returns:



# File 'lib/assertion/state.rb', line 12

.valid?Boolean

Check whether a stated assertion is satisfied by its attributes

Returns:

  • (Boolean)


38
39
40
# File 'lib/assertion/state.rb', line 38

def valid?
  !invalid?
end

.validate!true

Check whether a stated assertion is satisfied by its attributes

Returns:

  • (true)

Raises:



57
58
59
# File 'lib/assertion/state.rb', line 57

def validate!
  invalid? ? fail(InvalidError.new messages) : true
end