Module: StringUtils
- Defined in:
- lib/utils/string_utils.rb
Overview
Module containing string utilities for cli-proton-ruby clients
Class Method Summary collapse
- .sha1_hash(value) ⇒ Object
-
.str_is_bool?(value) ⇒ Boolean
Function to check if string variable is convertible to client bool value ==== Returns true if string variable is convertible to client bool value, false otherwise.
-
.str_is_float?(value) ⇒ Boolean
- Function to check if string variable is convertible to float ==== Parameters value
-
string variable to convert ==== Returns true if string variable is convertible to float, false otherwise.
-
.str_is_int?(value) ⇒ Boolean
- Function to check if string variable is convertible to integer ==== Parameters value
-
string variable to convert ==== Returns true if string variable is convertible to integer, false otherwise.
-
.str_to_bool(value) ⇒ Object
- Function to convert string variable to client bool value (yes/no|True/False|true/false) ==== Parameters value
-
string variable to convert ==== Returns bool value of the variable ==== Raises ArgumentError for invalid argument.
Class Method Details
.sha1_hash(value) ⇒ Object
75 76 77 |
# File 'lib/utils/string_utils.rb', line 75 def self.sha1_hash(value) Digest::SHA1.hexdigest value.to_s end |
.str_is_bool?(value) ⇒ Boolean
Function to check if string variable is convertible to client bool value
Returns
true if string variable is convertible to client bool value, false otherwise
43 44 45 46 47 48 49 50 51 |
# File 'lib/utils/string_utils.rb', line 43 def self.str_is_bool?(value) begin str_to_bool value rescue ArgumentError return false end return true end |
.str_is_float?(value) ⇒ Boolean
Function to check if string variable is convertible to float
Parameters
- value
-
string variable to convert
Returns
true if string variable is convertible to float, false otherwise
36 37 38 |
# File 'lib/utils/string_utils.rb', line 36 def self.str_is_float?(value) !Float(value).nil? rescue false end |
.str_is_int?(value) ⇒ Boolean
Function to check if string variable is convertible to integer
Parameters
- value
-
string variable to convert
Returns
true if string variable is convertible to integer, false otherwise
27 28 29 |
# File 'lib/utils/string_utils.rb', line 27 def self.str_is_int?(value) !Integer(value).nil? rescue false end |
.str_to_bool(value) ⇒ Object
Function to convert string variable to client bool value (yes/no|True/False|true/false)
Parameters
- value
-
string variable to convert
Returns
bool value of the variable
Raises
ArgumentError for invalid argument
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/utils/string_utils.rb', line 61 def self.str_to_bool(value) # If positive value if ["yes", "True", "true"].include?(value) # Return true return true # If negative value elsif ["no", "False", "false"].include?(value) # Return false return false end # If value is not convertible, raise ArgumentError raise ArgumentError, "invalid value for Boolean(): \"#{value}\"" end |