Class: MegliHelper::CommonHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/common_helper.rb

Class Method Summary collapse

Class Method Details

.nil_or_empty?(text) ⇒ Boolean

Check if text is null or empty

Parameters:

  • text (String)

    text to be checked

Returns:

  • (Boolean)

    true if text is nil or empty and false if not



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

Parameters:

  • value (String)

    value to be converted

Returns:

  • (Boolean)
    nil

    if string is not a valid boolean it will return nil



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

Parameters:

  • decimal (Float)

    value to be formatted

  • decimal_format (defaults to: '%8.2f')

    formmated used to format the value

  • currency (String) (defaults to: nil)

    currency signal to be included in the decimal like: $, £, R$, etc.

  • currency_before (Boolean) (defaults to: true)

    where the currency must be included (before or after decimal)

Returns:

  • (String)

    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

Parameters:

  • hash_object (Array/Hash)

    object that will be formatted

Returns:

  • (Array/Hash)

    keys converted 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

Parameters:

  • json (String)

    json to be verified

Returns:

  • (Boolean)

    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

Parameters:

  • condition (Proc)

    condition block to be validated

  • condition_status (Boolean) (defaults to: true)

    condition status to be used to define if condition is satisfied or not

  • timeout (Integer) (defaults to: 20)

    timeout to wait for the condition happen

  • sleep_for (Float) (defaults to: 0.25)

    value used to call the condition block



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