validates_each :thing1 do |row, attr, value|
row.errors.add_to_base 'Supply thing1 or thing2, but not both'
unless value.blank? ^ row.thing2.blank?
end
Even better, could someone write validates_mutual_exclusivity, in the
usual 2 lines of code or less?
How about something like:
validates_each :thing1 do |row, attr, value|
row.errors.add_to_base 'Supply thing1 or thing2, but not both' \
unless value.blank? ^ row.thing2.blank?
end
Thanks! Now I remember why I don't twiddle bits any more! I will report back if it works.
Next riddle: Our "Community Agreements" discourage 'unless'. So if you distribute a 'not' into a boolean expression, you get if value.nil? Q value.nil?, where Q is the opposite operator from ^, right? If so, what's Q?
> validates_each :thing1 do |row, attr, value|
> row.errors.add_to_base 'Supply thing1 or thing2, but not both' \
> unless value.blank? ^ row.thing2.blank?
> end
That one worked.
> Next riddle: Our "Community Agreements" discourage 'unless'. So if you
> distribute a 'not' into a boolean expression, you get if value.nil? Q
> value.nil?, where Q is the opposite operator from ^, right? If so,
> what's Q?
row.errors.add_to_base 'Supply thing1 or thing2, but not both' \
if (value.blank? ? row.thing2.blank? : !row.thing2.blank?)
if you look back at the original expansion of ^
[ ok, here it is: a ^ b => (a & !b) | (!a & b) ]
you'll note a similar form in the ?: expression's equivalent:
a ? !b : b
If your "Community Agreements" have a similarly silly ban on the ?:
expression, then don't take the last hop (or throw the !(a^b) after
the 'if').
Now now - someone with "agile" in their home URI should know better
than to call any Community Agreement "silly".
Our coding standard challenges us to reduce unless abuse, It's still
better than stating the negative at the end of an error line:
errors.add "bad thing should not happen" unless good_thing()
We stayed with the unless. Thanks for the math - that was awesome!
Great to see that the "standard" isn't absolute -- I guess i read "discourage" as "prohibit". Anyway, I wouldn't be agile if I didn't challenge things that conflicted with my own opinions. (And 'arbitrary' limitations on the use of language features is one of those things that I despise.)