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/case.rb
Kevin Deisz 5cf36e2ea2
Make And and Case into expression nodes
Allows aliasing, predications, ordering, and various other functions on `And` and `Case` nodes. This brings them in line with other nodes like `Binary` and `Unary`.
2019-01-24 14:35:52 -05:00

55 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Arel # :nodoc: all
module Nodes
class Case < Arel::Nodes::NodeExpression
attr_accessor :case, :conditions, :default
def initialize(expression = nil, default = nil)
@case = expression
@conditions = []
@default = default
end
def when(condition, expression = nil)
@conditions << When.new(Nodes.build_quoted(condition), expression)
self
end
def then(expression)
@conditions.last.right = Nodes.build_quoted(expression)
self
end
def else(expression)
@default = Else.new Nodes.build_quoted(expression)
self
end
def initialize_copy(other)
super
@case = @case.clone if @case
@conditions = @conditions.map { |x| x.clone }
@default = @default.clone if @default
end
def hash
[@case, @conditions, @default].hash
end
def eql?(other)
self.class == other.class &&
self.case == other.case &&
self.conditions == other.conditions &&
self.default == other.default
end
alias :== :eql?
end
class When < Binary # :nodoc:
end
class Else < Unary # :nodoc:
end
end
end