Module: Draisine::SalesforceComparisons

Defined in:
lib/draisine/util/salesforce_comparisons.rb

Constant Summary collapse

JSON_TIME_REGEX =
/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d{3})?[+\-]\d{2}:\d{2}\z/
EPSILON =
1e-10

Class Method Summary collapse

Class Method Details

.fp_equals?(value, other) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/draisine/util/salesforce_comparisons.rb', line 45

def fp_equals?(value, other)
  (value - other).abs <= EPSILON
end

.normalize_string(string) ⇒ Object



41
42
43
# File 'lib/draisine/util/salesforce_comparisons.rb', line 41

def normalize_string(string)
  string.gsub(/\W/, "")
end

.numeric?(value) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/draisine/util/salesforce_comparisons.rb', line 49

def numeric?(value)
  value.kind_of?(Numeric)
end

.salesforce_cleanup(value) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/draisine/util/salesforce_comparisons.rb', line 24

def salesforce_cleanup(value)
  case value
  when String
    Draisine::Encoding.convert_to_utf_and_sanitize(value)
  else
    value
  end
end

.salesforce_coerce(value) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/draisine/util/salesforce_comparisons.rb', line 33

def salesforce_coerce(value)
  if value.kind_of?(DateTime) || value.kind_of?(Time) || value.kind_of?(Date) ||
     value =~ JSON_TIME_REGEX
    value = value.to_time.utc.change(usec: 0)
  end
  value
end

.salesforce_equals?(value, other) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/draisine/util/salesforce_comparisons.rb', line 7

def salesforce_equals?(value, other)
  value = salesforce_cleanup(value)
  other = salesforce_cleanup(other)
  value = salesforce_coerce(value)
  other = salesforce_coerce(other)

  return fp_equals?(value, other) if numeric?(value) && numeric?(other)
  return value == other if value.class != other.class

  case value
  when String
    normalize_string(value) == normalize_string(other)
  else
    value == other
  end
end