Class: OnlyofficeFileHelper::StringHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/onlyoffice_file_helper/string_helper.rb

Overview

Helper for Strings

Class Method Summary collapse

Class Method Details

.generate_random_number(value = nil) ⇒ String

Generate random number with prefix



17
18
19
# File 'lib/onlyoffice_file_helper/string_helper.rb', line 17

def generate_random_number(value = nil)
  "#{value}: #{Time.now.nsec}"
end

.generate_random_string(length_string = 32) ⇒ String

Random string with specific length



10
11
12
# File 'lib/onlyoffice_file_helper/string_helper.rb', line 10

def generate_random_string(length_string = 32)
  (0...length_string).map { (('a'..'z').to_a + ('A'..'Z').to_a)[rand(52)] }.join
end

.get_result_string_of_comparison(compare_parameter, first_element, second_element, accuracy = 0.01) ⇒ String

Return ‘Result’ String of comparison of two strings



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/onlyoffice_file_helper/string_helper.rb', line 26

def get_result_string_of_comparison(compare_parameter,
                                    first_element,
                                    second_element,
                                    accuracy = 0.01)
  if first_element.is_a?(Float) && second_element.is_a?(Float)
    difference = (first_element - second_element).abs
    difference >= accuracy ? "Difference between parameters in #{compare_parameter} is #{difference}" : ''
  elsif first_element.to_s == second_element.to_s
    ''
  else
    "Difference in #{compare_parameter}. From case: #{first_element}. From result: #{second_element}"
  end
end

.to_bool(string) ⇒ True, False

Convert string to boolean value



43
44
45
46
47
48
49
50
# File 'lib/onlyoffice_file_helper/string_helper.rb', line 43

def to_bool(string)
  str = string.to_s
  if str.casecmp('false').zero?
    false
  elsif str.casecmp('true').zero?
    true
  end
end