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.
Instance Method Summary collapse
-
#same?(submitted, saved) ⇒ Boolean, Hash
Compares
submitted
againstsaved
and returns a simple diff.
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.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/aladdin/support/weak_comparator.rb', line 31 def same?(submitted, saved) case saved when Hash Hash === submitted and same_hash? submitted, saved when String then saved == submitted when Numeric then saved == JSON.parse(%|[#{submitted}]|).first when TrueClass, FalseClass then saved.to_s[0] == submitted.downcase[0] else false end rescue false end |