Module: CMDx::Utils::Normalize

Extended by:
Normalize
Included in:
Normalize
Defined in:
lib/cmdx/utils/normalize.rb

Overview

Provides normalization utilities for a variety of objects into consistent formats.

Instance Method Summary collapse

Instance Method Details

#exception(exception) ⇒ String

Normalizes an exception into a string representation.

Examples:

From exception

Normalize.exception(StandardError.new("test"))
# => "[StandardError] test"

Parameters:

  • exception (Exception)

    The exception to normalize

Returns:

  • (String)

    The normalized exception string

Rbs:

  • (Exception exception) -> String



22
23
24
# File 'lib/cmdx/utils/normalize.rb', line 22

def exception(exception)
  "[#{exception.class}] #{exception.message}"
end

#statuses(object) ⇒ Array<String>

Normalizes an object into an array of unique status strings.

Examples:

From array of symbols

Normalize.statuses([:success, :pending, :success])
# => ["success", "pending"]

From single value

Normalize.statuses(:success)
# => ["success"]

From nil

Normalize.statuses(nil)
# => []

Parameters:

  • object (Object)

    The object to normalize into status strings

Returns:

  • (Array<String>)

    Unique status strings

Rbs:

  • (untyped object) -> Array



43
44
45
46
47
48
# File 'lib/cmdx/utils/normalize.rb', line 43

def statuses(object)
  ary = Wrap.array(object)
  return EMPTY_ARRAY if ary.empty?

  ary.map(&:to_s).uniq
end