Class: MegliHelper::CommonHelper
- Inherits:
-
Object
- Object
- MegliHelper::CommonHelper
- Defined in:
- lib/helpers/common_helper.rb
Class Method Summary collapse
-
.nil_or_empty?(text) ⇒ Boolean
Check if text is null or empty.
-
.to_boolean(value) ⇒ Boolean
Convert string value to boolean.
-
.to_decimal(decimal, decimal_format = '%8.2f', currency = nil, currency_before = true) ⇒ String
Transform float value to a decimal formatted.
-
.transform_hash_keys_in_snake_case(hash_object) ⇒ Array/Hash
Transform keys of hash or array of hashes in snake case.
-
.valid_json?(json) ⇒ Boolean
Check if the JSON passed is valid or not.
-
.wait_until_condition(condition, condition_status = true, timeout = 20, sleep_for = 0.25) ⇒ Object
Wait until a condition passed happen.
Class Method Details
.nil_or_empty?(text) ⇒ Boolean
Check if text is null or empty
10 11 12 13 14 |
# File 'lib/helpers/common_helper.rb', line 10 def self.nil_or_empty?(text) (text.nil? || text.to_s.empty? || text.empty?) rescue false end |
.to_boolean(value) ⇒ Boolean
Convert string value to boolean
19 20 21 22 23 24 25 26 |
# File 'lib/helpers/common_helper.rb', line 19 def self.to_boolean(value) return false if nil_or_empty? value return true if %w(true 1 y yes).include? value.to_s.downcase.strip return false if %w(false 0 n no).include? value.to_s.downcase.strip false rescue false end |
.to_decimal(decimal, decimal_format = '%8.2f', currency = nil, currency_before = true) ⇒ String
Transform float value to a decimal formatted
34 35 36 37 38 39 40 41 |
# File 'lib/helpers/common_helper.rb', line 34 def self.to_decimal(decimal, decimal_format = '%8.2f', currency = nil, currency_before = true) new_decimal = (decimal_format % decimal).strip unless nil_or_empty?(currency) return currency + new_decimal if currency_before return new_decimal + currency end new_decimal end |
.transform_hash_keys_in_snake_case(hash_object) ⇒ Array/Hash
Transform keys of hash or array of hashes in snake case
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/helpers/common_helper.rb', line 46 def self.transform_hash_keys_in_snake_case(hash_object) case hash_object when Array hash_object.map { |v| transform_hash_keys_in_snake_case(v) } when Hash Hash[hash_object.map { |k, v| [k.to_s.underscore, transform_hash_keys_in_snake_case(v)] }] else hash_object end end |
.valid_json?(json) ⇒ Boolean
Check if the JSON passed is valid or not
60 61 62 63 64 65 66 |
# File 'lib/helpers/common_helper.rb', line 60 def self.valid_json?(json) return false if nil_or_empty? json JSON.parse(json) true rescue false end |
.wait_until_condition(condition, condition_status = true, timeout = 20, sleep_for = 0.25) ⇒ Object
Wait until a condition passed happen
73 74 75 76 77 |
# File 'lib/helpers/common_helper.rb', line 73 def self.wait_until_condition(condition, condition_status = true, timeout = 20, sleep_for = 0.25) Timeout.timeout(timeout) do sleep sleep_for until condition.call == condition_status end end |