Class: MonadicExceptions::ResultSanitizer

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

Overview

ResultSanitizer hold methods responsible to perform string transformations on the exception name

Instance Method Summary collapse

Instance Method Details

#apply_transformations_to_string(input, transformations) ⇒ String

This function apply any number of transformations (a proc that receive the value and return the transformed value) into a string input.

Parameters:

  • input (String)

    A target to start the transformations

  • transformations (Array<Proc>)

    An list of procs that perform pure transformation at a target

Returns:

  • (String)

    The modified input



27
28
29
30
31
32
33
34
35
# File 'lib/result_sanitizer.rb', line 27

def apply_transformations_to_string(input, transformations)
  modified_string = input.dup

  transformations.each do |transformation|
    modified_string = transformation.call(modified_string)
  end

  modified_string
end

#remove_non_ascii_characters(input) ⇒ String

Returns without any character outside the ascii range.

Parameters:

  • input (String)

Returns:

  • (String)

    without any character outside the ascii range



18
19
20
# File 'lib/result_sanitizer.rb', line 18

def remove_non_ascii_characters(input)
  input.gsub(/[^\x00-\x7F]/, '')
end

#treat_exception_name(exception) ⇒ Symbol

Parameters:

  • exception (String)

    before calling this method call the .to_s method on the exception at hand.

Returns:

  • (Symbol)


8
9
10
11
12
13
14
# File 'lib/result_sanitizer.rb', line 8

def treat_exception_name(exception)
  apply_transformations_to_string(exception, [
                                    proc { |x| x.downcase },
                                    proc { |x| remove_non_ascii_characters(x) },
                                    proc { |x| x.to_sym }
                                  ])
end