Module: Aladdin::Support::WeakComparator

Extended by:
WeakComparator
Included in:
Aladdin::Submission, WeakComparator
Defined in:
lib/aladdin/support/weak_comparator.rb

Overview

Provides a rich comparison with weak-typing.

See Also:

Instance Method Summary collapse

Instance Method Details

#same?(submitted, saved) ⇒ Boolean, Hash

Compares submitted against saved and returns a simple diff. Assumes that:

  • saved is a ruby-marshalled value that carries rich type information

  • submitted is a user-submitted value that is either a string or an hash

Booleans

If saved is true, then it will accept any submitted value that begins with ‘T’ or ‘t’. Similarly, if saved is false, then it will accept any submitted value that begins with ‘F’ or ‘f’.

Numerics

If saved is a numeric, then it will accept any submitted value that is numerically equivalent to saved. For example, if saved is 0, then both ‘0.0’ and ‘0’ will be accepted.

Parameters:

  • submitted (String, Hash)
  • saved (String, Numeric, Boolean, Hash)

Returns:

  • (Boolean, Hash)

    diff



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aladdin/support/weak_comparator.rb', line 31

def same?(, saved)
  case saved
  when Hash
    Hash ===  and same_hash? , saved
  when String then saved == 
  when Numeric then saved == JSON.parse(%|[#{}]|).first
  when TrueClass, FalseClass then saved.to_s[0] == .downcase[0]
  else false end
rescue
  false
end