On Apr 10, 12:23 pm, David Lee <[EMAIL PROTECTED]> wrote: > A way to implement 'AND' and 'OR' conditional statements using hashes > > {:a => 1}.or(:b => 3) > # => (a=1) OR (b=3) > > {:a => 1, :b => 3} > # => (a=1) AND (b=3) > > {:a => 1}.and(:b => 3) > # => {:a => 1, :b => 3} > # => (a=1) AND (b=3) > > {:a => 1, :b => 3}.or{:a => 2} > # => ((a=1) AND (b=3)) OR (a=2) > > Notice, that associativity is equal for and and or statements because > ruby would evaluate left-to-right: > > {:a => 1}.or(:b => 3).and(:c => 4) > # => ((a=1) OR (b=3)) AND (c=4) > > You can change that by surrounding grouped conditions with > parentheses: > > {:a => 1}.and({:b => 2}.or(:b => 3).and(:c => 2)) > # => (a=1) AND ((b=2 OR b=3) AND c=2) > > The 'and' method can check for clashes: > > {:a => 2}.and(:a => 3) > # => ERROR: column a has two clashing conditions > > {:a.gt => 2}.and(:a.gt => 3) > # => ERROR: column a has two clashing conditions > > {:a.gt => 2}.and(:a.lt => 10) > # => (a > 2) AND (a < 10) > > This can be implemented using ConditionAndGroup and ConditionOrGroup: > > Hash.and(Hash) => Hash # merged > > Hash.and(anything_else) => ConditionAndGroup > > Hash.or(anything) => ConditionOrGroup > > ConditionAndGroup.and(anything) => ConditionAndGroup > > ConditionOrGroup.and(anything) => ConditionAndGroup > > ConditionAndGroup.or(anything) => ConditionOrGroup > > ConditionOrGroup.or(anything) => ConditionOrGroup
I like this idea. Please try to work on an implementation if you have time. If not, I'll try to do so at some point. Jeremy --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sequel-talk" group. To post to this group, send email to sequel-talk@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en -~----------~----~----~----~------~----~------~--~---