1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/arel/nodes/binary.rb
eileencodes ce7bbc3685
Refactor fetch_attribute
Similar to the work done in #38636, instead of using case statements we
can make these classes respond to `fetch_attribute`.

New classes can implement `fetch_attribute` instead of adding to the
case statement, it's more object oriented, and nicer looking.

Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
2020-03-13 13:38:32 -04:00

68 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module Arel # :nodoc: all
module Nodes
class Binary < Arel::Nodes::NodeExpression
attr_accessor :left, :right
def initialize(left, right)
super()
@left = left
@right = right
end
def initialize_copy(other)
super
@left = @left.clone if @left
@right = @right.clone if @right
end
def hash
[self.class, @left, @right].hash
end
def eql?(other)
self.class == other.class &&
self.left == other.left &&
self.right == other.right
end
alias :== :eql?
end
module FetchAttribute
def fetch_attribute
if left.is_a?(Arel::Attributes::Attribute)
yield left
elsif right.is_a?(Arel::Attributes::Attribute)
yield right
end
end
end
class Between < Binary; include FetchAttribute; end
class NotIn < Binary; include FetchAttribute; end
class GreaterThan < Binary; include FetchAttribute; end
class GreaterThanOrEqual < Binary; include FetchAttribute; end
class NotEqual < Binary; include FetchAttribute; end
class LessThan < Binary; include FetchAttribute; end
class LessThanOrEqual < Binary; include FetchAttribute; end
class Or < Binary
def fetch_attribute(&block)
left.fetch_attribute(&block) && right.fetch_attribute(&block)
end
end
%w{
As
Assignment
Join
Union
UnionAll
Intersect
Except
}.each do |name|
const_set name, Class.new(Binary)
end
end
end