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

127 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-02-24 01:45:50 -05:00
2018-02-24 02:41:47 -05:00
module Arel # :nodoc: all
2010-08-16 20:26:12 -04:00
module Nodes
class Binary < Arel::Nodes::NodeExpression
2010-08-16 20:26:12 -04:00
attr_accessor :left, :right
2018-02-24 01:45:50 -05:00
def initialize(left, right)
2013-05-17 18:43:54 -04:00
super()
2010-08-16 20:26:12 -04:00
@left = left
@right = right
end
2010-12-14 23:31:49 -05:00
2018-02-24 01:45:50 -05:00
def initialize_copy(other)
2010-12-14 23:31:49 -05:00
super
@left = @left.clone if @left
@right = @right.clone if @right
end
def hash
[self.class, @left, @right].hash
end
2018-02-24 01:45:50 -05:00
def eql?(other)
self.class == other.class &&
self.left == other.left &&
self.right == other.right
end
alias :== :eql?
2010-08-16 20:26:12 -04:00
end
2010-12-14 21:13:06 -05:00
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 GreaterThan < Binary
include FetchAttribute
def invert
Arel::Nodes::LessThanOrEqual.new(left, right)
end
end
class GreaterThanOrEqual < Binary
include FetchAttribute
def invert
Arel::Nodes::LessThan.new(left, right)
end
end
class LessThan < Binary
include FetchAttribute
def invert
Arel::Nodes::GreaterThanOrEqual.new(left, right)
end
end
class LessThanOrEqual < Binary
include FetchAttribute
def invert
Arel::Nodes::GreaterThan.new(left, right)
end
end
class IsDistinctFrom < Binary
include FetchAttribute
def invert
Arel::Nodes::IsNotDistinctFrom.new(left, right)
end
end
class IsNotDistinctFrom < Binary
include FetchAttribute
def invert
Arel::Nodes::IsDistinctFrom.new(left, right)
end
end
class NotEqual < Binary
include FetchAttribute
def invert
Arel::Nodes::Equality.new(left, right)
end
end
class NotIn < Binary
include FetchAttribute
def invert
Arel::Nodes::In.new(left, right)
end
end
class Or < Binary
def fetch_attribute(&block)
left.fetch_attribute(&block) && right.fetch_attribute(&block)
end
end
2010-12-14 21:13:06 -05:00
%w{
As
2010-12-15 00:06:16 -05:00
Assignment
Join
Union
UnionAll
2011-01-22 09:16:53 -05:00
Intersect
Except
2010-12-14 21:13:06 -05:00
}.each do |name|
2010-12-15 00:06:16 -05:00
const_set name, Class.new(Binary)
2010-12-14 21:13:06 -05:00
end
2010-08-16 20:26:12 -04:00
end
end