Module: Crosstest::Core::Util

Defined in:
lib/crosstest/core/util.rb

Overview

Stateless utility methods used in different contexts. Essentially a mini PassiveSupport library.

Defined Under Namespace

Modules: Hashable, String Classes: HTML, Highlight

Class Method Summary collapse

Class Method Details

.duration(total) ⇒ String

Returns a formatted string representing a duration in seconds.

Parameters:

  • total (Integer)

    the total number of seconds

Returns:

  • (String)

    a formatted string of the form (XmYY.00s)



95
96
97
98
99
100
# File 'lib/crosstest/core/util.rb', line 95

def self.duration(total)
  total = 0 if total.nil?
  minutes = (total / 60).to_i
  seconds = (total - (minutes * 60))
  format('(%dm%.2fs)', minutes, seconds)
end

.from_logger_level(const) ⇒ Symbol

Returns the symbol represenation of a logging levels for a given standard library Logger::Severity constant.

Parameters:

  • const (Integer)

    Logger::Severity constant value for a logging level (Logger::DEBUG, Logger::INFO, Logger::WARN, Logger::ERROR, Logger::FATAL)

Returns:

  • (Symbol)

    symbol representation of the logging level



39
40
41
42
43
44
45
46
47
# File 'lib/crosstest/core/util.rb', line 39

def self.from_logger_level(const)
  case const
  when ::Logger::DEBUG then :debug
  when ::Logger::INFO then :info
  when ::Logger::WARN then :warn
  when ::Logger::ERROR then :error
  else :fatal
  end
end

.stringified_hash(obj) ⇒ Object

Returns a new Hash with all key values coerced to strings. All keys within a Hash are coerced by calling #to_s and hashes with arrays and other hashes are traversed.

Parameters:

  • obj (Object)

    the hash to be processed. While intended for hashes, this method safely processes arbitrary objects

Returns:

  • (Object)

    a converted hash with all keys as strings



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/crosstest/core/util.rb', line 77

def self.stringified_hash(obj)
  if obj.is_a?(Hash)
    obj.each_with_object({}) do |(k, v), h|
      h[k.to_s] = stringified_hash(v)
    end
  elsif obj.is_a?(Array)
    obj.each_with_object([]) do |e, a|
      a << stringified_hash(e)
    end
  else
    obj
  end
end

.symbolized_hash(obj) ⇒ Object

Returns a new Hash with all key values coerced to symbols. All keys within a Hash are coerced by calling #to_sym and hashes within arrays and other hashes are traversed.

Parameters:

  • obj (Object)

    the hash to be processed. While intended for hashes, this method safely processes arbitrary objects

Returns:

  • (Object)

    a converted hash with all keys as symbols



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crosstest/core/util.rb', line 56

def self.symbolized_hash(obj)
  if obj.is_a?(Hash)
    obj.each_with_object({}) do |(k, v), h|
      h[k.to_sym] = symbolized_hash(v)
    end
  elsif obj.is_a?(Array)
    obj.each_with_object([]) do |e, a|
      a << symbolized_hash(e)
    end
  else
    obj
  end
end

.to_logger_level(symbol) ⇒ Integer

Returns the standard library Logger level constants for a given symbol representation.

Parameters:

  • symbol (Symbol)

    symbol representation of a logger level (:debug, :info, :warn, :error, :fatal)

Returns:

  • (Integer)

    Logger::Severity constant value or nil if input is not valid



26
27
28
29
30
# File 'lib/crosstest/core/util.rb', line 26

def self.to_logger_level(symbol)
  return nil unless [:debug, :info, :warn, :error, :fatal].include?(symbol)

  ::Logger.const_get(symbol.to_s.upcase)
end