Class: Moab::VerificationResult

Inherits:
Object
  • Object
show all
Defined in:
lib/moab/verification_result.rb

Overview

A recursive “Tree” type object for verifications

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, verified = false, details = nil) ⇒ VerificationResult

Returns a new instance of VerificationResult.

Parameters:

  • entity (#to_s)

    The name of the entity being verified

  • verified (Boolean) (defaults to: false)
  • details (Hash) (defaults to: nil)


19
20
21
22
23
24
# File 'lib/moab/verification_result.rb', line 19

def initialize(entity, verified = false, details = nil)
  @entity = entity.to_s  # force to string
  @verified = !!verified # rubocop:disable Style/DoubleNegation
  @details = details
  @subentities = Array.new
end

Instance Attribute Details

#detailsHash

Returns The details of the comparisons that were made.

Returns:

  • (Hash)

    The details of the comparisons that were made



11
12
13
# File 'lib/moab/verification_result.rb', line 11

def details
  @details
end

#entityString

Returns The name of the entity that was verified.

Returns:

  • (String)

    The name of the entity that was verified



5
6
7
# File 'lib/moab/verification_result.rb', line 5

def entity
  @entity
end

#subentitiesArray<VerificationResult>

Returns The subentities, if any, on which this verification is based.

Returns:

  • (Array<VerificationResult>)

    The subentities, if any, on which this verification is based



14
15
16
# File 'lib/moab/verification_result.rb', line 14

def subentities
  @subentities
end

#verifiedBoolean

Returns The true/false outcome of the verification.

Returns:

  • (Boolean)

    The true/false outcome of the verification



8
9
10
# File 'lib/moab/verification_result.rb', line 8

def verified
  @verified
end

Class Method Details

.verify_truth(entity, expression, details = nil) ⇒ VerificationResult

Deprecated.

Just use the constructor

Returns The result of evaluating the expression.

Parameters:

  • entity (#to_s)

    The name of the entity being verified

  • expression (Object)

    The expression that will be evaluated as true or false

  • details (Object) (defaults to: nil)

    optional details that could be reported

Returns:



40
41
42
# File 'lib/moab/verification_result.rb', line 40

def self.verify_truth(entity, expression, details = nil)
  new(entity, expression, details)
end

.verify_value(entity, expected, found) ⇒ VerificationResult

Returns The result of comparing the expected and found values.

Parameters:

  • entity (#to_s)

    The name of the entity being verified

  • expected (Object)

    The expected value

  • found (Object)

    The found value

Returns:



30
31
32
33
# File 'lib/moab/verification_result.rb', line 30

def self.verify_value(entity, expected, found)
  details = { 'expected' => expected, 'found' => found }
  new(entity, (expected == found), details)
end

Instance Method Details

#to_hash(verbose = false, level = 0) ⇒ Hash

Returns The verification result serialized to a hash.

Parameters:

  • verbose (Boolean) (defaults to: false)

    If true, always provide all details of the verification

  • level (Integer) (defaults to: 0)

    Used to test the depth of recursion

Returns:

  • (Hash)

    The verification result serialized to a hash



53
54
55
56
57
58
59
60
# File 'lib/moab/verification_result.rb', line 53

def to_hash(verbose = false, level = 0)
  hash = { 'verified' => verified }
  if verbose || !verified
    hash['details'] = details || subentities_to_hash(verbose, level)
  end
  return hash if level > 0
  { entity => hash }
end

#to_json(verbose = false) ⇒ String

Returns The verification result serialized to JSON.

Parameters:

  • verbose (Boolean) (defaults to: false)

    If true, always provide all details of the verification

Returns:

  • (String)

    The verification result serialized to JSON



46
47
48
# File 'lib/moab/verification_result.rb', line 46

def to_json(verbose = false)
  JSON.pretty_generate(to_hash(verbose))
end