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



56
57
58
59
60
61
62
# File 'lib/applitools/utils/utils.rb', line 56

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

#camelcase(str) ⇒ Object



16
17
18
19
# File 'lib/applitools/utils/utils.rb', line 16

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

#camelcase_hash_keys(hash) ⇒ Object



44
45
46
# File 'lib/applitools/utils/utils.rb', line 44

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

#capitalize(str) ⇒ Object



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

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

#capitalize_hash_keys(hash) ⇒ Object



48
49
50
# File 'lib/applitools/utils/utils.rb', line 48

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

#deep_stringify_keys(value) ⇒ Object



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

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



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/applitools/utils/utils.rb', line 100

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



70
71
72
73
# File 'lib/applitools/utils/utils.rb', line 70

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

#stringify_for_hash(value) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/applitools/utils/utils.rb', line 75

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



52
53
54
# File 'lib/applitools/utils/utils.rb', line 52

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

#symbolize_keys(hash) ⇒ Object



64
65
66
67
68
# File 'lib/applitools/utils/utils.rb', line 64

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

#to_method_name(str) ⇒ Object



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

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



12
13
14
# File 'lib/applitools/utils/utils.rb', line 12

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

#underscore(str) ⇒ Object



8
9
10
# File 'lib/applitools/utils/utils.rb', line 8

def underscore(str)
  str.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
end

#underscore_hash_keys(hash) ⇒ Object



40
41
42
# File 'lib/applitools/utils/utils.rb', line 40

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

#wrap(object) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/applitools/utils/utils.rb', line 30

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