Module: Polytrix::Util
- Defined in:
- lib/polytrix/util.rb
Overview
Stateless utility methods used in different contexts. Essentially a mini PassiveSupport library.
Defined Under Namespace
Modules: FileSystem, String Classes: HTML, Highlight
Class Method Summary collapse
-
.duration(total) ⇒ String
Returns a formatted string representing a duration in seconds.
-
.from_logger_level(const) ⇒ Symbol
Returns the symbol represenation of a logging levels for a given standard library Logger::Severity constant.
-
.stringified_hash(obj) ⇒ Object
Returns a new Hash with all key values coerced to strings.
-
.symbolized_hash(obj) ⇒ Object
Returns a new Hash with all key values coerced to symbols.
-
.to_logger_level(symbol) ⇒ Integer
Returns the standard library Logger level constants for a given symbol representation.
Class Method Details
.duration(total) ⇒ String
Returns a formatted string representing a duration in seconds.
86 87 88 89 90 91 |
# File 'lib/polytrix/util.rb', line 86 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.
30 31 32 33 34 35 36 37 38 |
# File 'lib/polytrix/util.rb', line 30 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/polytrix/util.rb', line 68 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/polytrix/util.rb', line 47 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.
17 18 19 20 21 |
# File 'lib/polytrix/util.rb', line 17 def self.to_logger_level(symbol) return nil unless [:debug, :info, :warn, :error, :fatal].include?(symbol) Logger.const_get(symbol.to_s.upcase) end |