predications.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. module Arel
  2. module Predications
  3. def not_eq other
  4. Nodes::NotEqual.new self, other
  5. end
  6. def not_eq_any others
  7. grouping_any :not_eq, others
  8. end
  9. def not_eq_all others
  10. grouping_all :not_eq, others
  11. end
  12. def eq other
  13. Nodes::Equality.new self, other
  14. end
  15. def eq_any others
  16. grouping_any :eq, others
  17. end
  18. def eq_all others
  19. grouping_all :eq, others
  20. end
  21. def in other
  22. case other
  23. when Arel::SelectManager
  24. Arel::Nodes::In.new(self, other.ast)
  25. when Range
  26. if other.exclude_end?
  27. left = Nodes::GreaterThanOrEqual.new(self, other.begin)
  28. right = Nodes::LessThan.new(self, other.end)
  29. Nodes::And.new [left, right]
  30. else
  31. Nodes::Between.new(self, Nodes::And.new([other.begin, other.end]))
  32. end
  33. else
  34. Nodes::In.new self, other
  35. end
  36. end
  37. def in_any others
  38. grouping_any :in, others
  39. end
  40. def in_all others
  41. grouping_all :in, others
  42. end
  43. def not_in other
  44. case other
  45. when Arel::SelectManager
  46. Arel::Nodes::NotIn.new(self, other.ast)
  47. when Range
  48. if other.exclude_end?
  49. left = Nodes::LessThan.new(self, other.begin)
  50. right = Nodes::GreaterThanOrEqual.new(self, other.end)
  51. Nodes::Or.new left, right
  52. else
  53. left = Nodes::LessThan.new(self, other.begin)
  54. right = Nodes::GreaterThan.new(self, other.end)
  55. Nodes::Or.new left, right
  56. end
  57. else
  58. Nodes::NotIn.new self, other
  59. end
  60. end
  61. def not_in_any others
  62. grouping_any :not_in, others
  63. end
  64. def not_in_all others
  65. grouping_all :not_in, others
  66. end
  67. def matches other
  68. Nodes::Matches.new self, other
  69. end
  70. def matches_any others
  71. grouping_any :matches, others
  72. end
  73. def matches_all others
  74. grouping_all :matches, others
  75. end
  76. def does_not_match other
  77. Nodes::DoesNotMatch.new self, other
  78. end
  79. def does_not_match_any others
  80. grouping_any :does_not_match, others
  81. end
  82. def does_not_match_all others
  83. grouping_all :does_not_match, others
  84. end
  85. def gteq right
  86. Nodes::GreaterThanOrEqual.new self, right
  87. end
  88. def gteq_any others
  89. grouping_any :gteq, others
  90. end
  91. def gteq_all others
  92. grouping_all :gteq, others
  93. end
  94. def gt right
  95. Nodes::GreaterThan.new self, right
  96. end
  97. def gt_any others
  98. grouping_any :gt, others
  99. end
  100. def gt_all others
  101. grouping_all :gt, others
  102. end
  103. def lt right
  104. Nodes::LessThan.new self, right
  105. end
  106. def lt_any others
  107. grouping_any :lt, others
  108. end
  109. def lt_all others
  110. grouping_all :lt, others
  111. end
  112. def lteq right
  113. Nodes::LessThanOrEqual.new self, right
  114. end
  115. def lteq_any others
  116. grouping_any :lteq, others
  117. end
  118. def lteq_all others
  119. grouping_all :lteq, others
  120. end
  121. private
  122. def grouping_any method_id, others
  123. nodes = others.map {|expr| send(method_id, expr)}
  124. Nodes::Grouping.new nodes.inject { |memo,node|
  125. Nodes::Or.new(memo, node)
  126. }
  127. end
  128. def grouping_all method_id, others
  129. Nodes::Grouping.new Nodes::And.new(others.map {|expr| send(method_id, expr)})
  130. end
  131. end
  132. end