Module: Polytrix::Util
- Defined in:
- lib/polytrix/util.rb
Overview
Stateless utility methods used in different contexts. Essentially a mini PassiveSupport library.
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.
-
.outdent!(string) ⇒ String
Modifes the given string to strip leading whitespace on each line, the amount which is calculated by using the first line of text.
-
.shell_helpers ⇒ String
Returns a set of Bourne Shell (AKA /bin/sh) compatible helper functions.
-
.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.
-
.wrap_command(cmd) ⇒ String
Generates a command (or series of commands) wrapped so that it can be invoked on a remote instance or locally.
Class Method Details
.duration(total) ⇒ String
Returns a formatted string representing a duration in seconds.
103 104 105 106 107 108 |
# File 'lib/polytrix/util.rb', line 103 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.
43 44 45 46 47 48 49 50 51 |
# File 'lib/polytrix/util.rb', line 43 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 |
.outdent!(string) ⇒ String
Modifes the given string to strip leading whitespace on each line, the amount which is calculated by using the first line of text.
140 141 142 |
# File 'lib/polytrix/util.rb', line 140 def self.outdent!(string) string.gsub!(/^ {#{string.index(/[^ ]/)}}/, '') end |
.shell_helpers ⇒ String
Returns a set of Bourne Shell (AKA /bin/sh) compatible helper functions. This function is usually called inline in a string that will be executed remotely on a test instance.
149 150 151 152 153 |
# File 'lib/polytrix/util.rb', line 149 def self.shell_helpers IO.read(File.join( File.dirname(__FILE__), %w[.. .. support download_helpers.sh] )) 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/polytrix/util.rb', line 83 def self.stringified_hash(obj) if obj.is_a?(Hash) obj.reduce({}) do |h, (k, v)| h[k.to_s] = stringified_hash(v) h end elsif obj.is_a?(Array) obj.reduce([]) do |a, e| a << stringified_hash(e) a 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/polytrix/util.rb', line 60 def self.symbolized_hash(obj) if obj.is_a?(Hash) obj.reduce({}) do |h, (k, v)| h[k.to_sym] = symbolized_hash(v) h end elsif obj.is_a?(Array) obj.reduce([]) do |a, e| a << symbolized_hash(e) a end else obj end end |
.to_logger_level(symbol) ⇒ Integer
Returns the standard library Logger level constants for a given symbol representation.
30 31 32 33 34 |
# File 'lib/polytrix/util.rb', line 30 def self.to_logger_level(symbol) return nil unless [:debug, :info, :warn, :error, :fatal].include?(symbol) Logger.const_get(symbol.to_s.upcase) end |
.wrap_command(cmd) ⇒ String
Generates a command (or series of commands) wrapped so that it can be invoked on a remote instance or locally.
This method uses the Bourne shell (/bin/sh) to maximize the chance of cross platform portability on Unixlike systems.
118 119 120 121 122 123 124 |
# File 'lib/polytrix/util.rb', line 118 def self.wrap_command(cmd) cmd = 'false' if cmd.nil? cmd = 'true' if cmd.to_s.empty? cmd = cmd.sub(/\n\Z/, '') if cmd =~ /\n\Z/ "sh -c '\n#{cmd}\n'" end |