Module: Applitools::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/applitools/utils/utils.rb,
lib/applitools/utils/image_utils.rb,
lib/applitools/utils/eyes_selenium_utils.rb,
lib/applitools/utils/image_delta_compressor.rb

Defined Under Namespace

Modules: EyesSeleniumUtils, ImageDeltaCompressor, ImageUtils

Constant Summary collapse

RESAMPLE_INCREMENTALLY_FRACTION =
2

Instance Method Summary collapse

Instance Method Details

#boolean_value(value) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/applitools/utils/utils.rb', line 66

def boolean_value(value)
  if value
    true
  else
    false
  end
end

#camelcase(str) ⇒ Object



26
27
28
29
# File 'lib/applitools/utils/utils.rb', line 26

def camelcase(str)
  tokens = str.split('_')
  uncapitalize(tokens.shift) + tokens.map(&:capitalize).join
end

#camelcase_hash_keys(hash) ⇒ Object



54
55
56
# File 'lib/applitools/utils/utils.rb', line 54

def camelcase_hash_keys(hash)
  convert_hash_keys(hash, :camelcase)
end

#capitalize(str) ⇒ Object



31
32
33
# File 'lib/applitools/utils/utils.rb', line 31

def capitalize(str)
  str[0, 1].upcase + str[1..-1]
end

#capitalize_hash_keys(hash) ⇒ Object



58
59
60
# File 'lib/applitools/utils/utils.rb', line 58

def capitalize_hash_keys(hash)
  convert_hash_keys(hash, :capitalize)
end

#deep_stringify_keys(value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/applitools/utils/utils.rb', line 97

def deep_stringify_keys(value)
  case value
    when Hash
      value.each_with_object({}) do |(key, val), result|
        result[key.to_s] = deep_stringify_keys(val)
      end
    when Array
      value.map { |e| deep_stringify_keys(e) }
    else
      value
  end
end

#deep_symbolize_keys(value) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/applitools/utils/utils.rb', line 110

def deep_symbolize_keys(value)
  case value
    when Hash
      value.each_with_object({}) do |(key, val), result|
        result[key.to_sym] = deep_symbolize_keys(val)
      end
    when Array
      value.map { |e| deep_symbolize_keys(e) }
    else
      value
  end
end

#extract_options!(array) ⇒ Object



80
81
82
83
# File 'lib/applitools/utils/utils.rb', line 80

def extract_options!(array)
  return array.pop if array.last.instance_of? Hash
  {}
end

#stringify_for_hash(value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/applitools/utils/utils.rb', line 85

def stringify_for_hash(value)
  return value.stringify if value.respond_to? :stringify
  case value
  when Array
    value.map { |el| stringify_for_hash(el) }.join('')
  when Hash
    value.keys.sort { |a, b| b.to_s <=> a.to_s }.map { |k| "#{k}#{stringify_for_hash(value[k])}" }.join('')
  else
    value.to_s
  end
end

#stringify_hash_keys(hash) ⇒ Object



62
63
64
# File 'lib/applitools/utils/utils.rb', line 62

def stringify_hash_keys(hash)
  convert_hash_keys(hash, :stringify)
end

#symbolize_keys(hash) ⇒ Object



74
75
76
77
78
# File 'lib/applitools/utils/utils.rb', line 74

def symbolize_keys(hash)
  hash.each_with_object({}) do |(k, v), memo|
    memo[k.to_sym] = v
  end
end

#to_method_name(str) ⇒ Object



35
36
37
38
# File 'lib/applitools/utils/utils.rb', line 35

def to_method_name(str)
  str = str.to_s if !str.is_a?(String) && str.respond_to?(:to_s)
  underscore(str).gsub(%r{\/}, '__')
end

#uncapitalize(str) ⇒ Object



22
23
24
# File 'lib/applitools/utils/utils.rb', line 22

def uncapitalize(str)
  str[0, 1].downcase + str[1..-1]
end

#underscore(str) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/applitools/utils/utils.rb', line 8

def underscore(str)
  str = str.gsub('::', '/')
  str = str.gsub(/([A-Z]{2,})(?=[A-Z][a-z])|([a-z\d])([A-Z])/o) do
    if Regexp.last_match(1) # Handles groups of uppercase letters like "HTMLParser"
      "#{Regexp.last_match(1)}_"
    else # Handles camel-case transitions like "camelCase"
      "#{Regexp.last_match(2)}_#{Regexp.last_match(3)}"
    end
  end

  # Step 3: Convert hyphens to underscores and downcase everything
  str.tr('-', '_').downcase
end

#underscore_hash_keys(hash) ⇒ Object



50
51
52
# File 'lib/applitools/utils/utils.rb', line 50

def underscore_hash_keys(hash)
  convert_hash_keys(hash, :underscore)
end

#wrap(object) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/applitools/utils/utils.rb', line 40

def wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end