Class: Katachi::ComparisonResult

Inherits:
Object
  • Object
show all
Defined in:
lib/katachi/comparison_result.rb

Overview

A class to represent the result of a comparison. It has a value, a shape, a code, and optionally child codes. The code is a symbol that represents the result of the comparison.

Constant Summary collapse

CODES =

CODES is a Hash of the form { code: boolean }. The boolean represents whether the code is considered a match or not. For example, :match is considered a match, while :mismatch is not.

{
  # General
  class_mismatch: false,
  exact_match: true,
  match: true,
  mismatch: false,
  object_class_universal_match: true,
  # AnyOf
  any_of_match: true,
  any_of_mismatch: false,
  # Array Overall
  array_is_empty: true,
  array_is_match: true,
  array_is_mismatch: false,
  array_is_exact_match: true,
  array_class_matches_any_array: true,
  # Array Elements
  array_element_match: true,
  array_element_mismatch: false,
  # Hashes
  hash_class_matches_any_hash: true,
  hash_is_exact_match: true,
  hash_is_mismatch: false,
  hash_is_match: true,
  # Hash[extra key checks]
  hash_has_extra_keys: false,
  hash_has_no_extra_keys: true,
  # Hash[extra key checks][individual keys]
  hash_key_exactly_allowed: true,
  hash_key_match_allowed: true,
  hash_key_not_allowed: false,
  # Hash[missing key checks]
  hash_has_missing_keys: false,
  hash_has_no_missing_keys: true,
  # Hash[missing key checks][individual keys]
  hash_key_exact_match: true,
  hash_key_match: true,
  hash_key_missing: false,
  hash_key_optional: true,
  # Hash[value checks]
  hash_values_are_mismatch: false,
  hash_values_are_match: true,
  # Hash[value checks][individual kv pairs] vs Shape
  kv_match: true,
  kv_mismatch: false,
  kv_specific_match: true,
  kv_specific_mismatch: false,
  # Hash[value checks][individual kv pairs] vs Shape[individual kv pairs]
  kv_key_mismatch: false,
  kv_value_match: true,
  kv_value_mismatch: false,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, shape:, code:, child_results: nil) ⇒ ComparisonResult

Returns a new instance of ComparisonResult.

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
# File 'lib/katachi/comparison_result.rb', line 65

def initialize(value:, shape:, code:, child_results: nil)
  raise ArgumentError, "code `#{code.inspect}` must be one of #{CODES.keys.inspect}" unless CODES.key?(code)

  @value = value
  @shape = shape
  @code = code
  @child_results = child_results
  assert_child_codes_are_valid
end

Instance Attribute Details

#child_results ⇒ Object (readonly)

Returns the value of attribute child_results.



63
64
65
# File 'lib/katachi/comparison_result.rb', line 63

def child_results
  @child_results
end

#code ⇒ Object (readonly)

Returns the value of attribute code.



63
64
65
# File 'lib/katachi/comparison_result.rb', line 63

def code
  @code
end

#shape ⇒ Object (readonly)

Returns the value of attribute shape.



63
64
65
# File 'lib/katachi/comparison_result.rb', line 63

def shape
  @shape
end

#value ⇒ Object (readonly)

Returns the value of attribute value.



63
64
65
# File 'lib/katachi/comparison_result.rb', line 63

def value
  @value
end

Instance Method Details

#match? ⇒ Boolean

Returns:

  • (Boolean)


75
# File 'lib/katachi/comparison_result.rb', line 75

def match? = CODES[code]

#to_s(child_label = nil) ⇒ Object



77
# File 'lib/katachi/comparison_result.rb', line 77

def to_s(child_label = nil) = [basic_text(child_label), *child_results_text_lines].compact.join("\n")