aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/nodes
diff options
context:
space:
mode:
authorFelix Bùˆnemann <buenemann@louis.info>2015-12-19 19:40:46 +0100
committerFelix Bùˆnemann <buenemann@louis.info>2016-01-05 17:53:52 +0100
commit4c7e50f9328aca4e294b41fce0832bf6ac4a939a (patch)
tree1bf97bcb2f6928a32b6acb93ea9886925a0e21b4 /lib/arel/nodes
parent347c7786f8e7ea0e9643ff707ce1ace8b3969d6c (diff)
downloadrails-4c7e50f9328aca4e294b41fce0832bf6ac4a939a.tar.gz
rails-4c7e50f9328aca4e294b41fce0832bf6ac4a939a.tar.bz2
rails-4c7e50f9328aca4e294b41fce0832bf6ac4a939a.zip
Implement CASE Conditional Expression
Diffstat (limited to 'lib/arel/nodes')
-rw-r--r--lib/arel/nodes/case.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/arel/nodes/case.rb b/lib/arel/nodes/case.rb
new file mode 100644
index 0000000000..85f8851dbe
--- /dev/null
+++ b/lib/arel/nodes/case.rb
@@ -0,0 +1,57 @@
+module Arel
+ module Nodes
+ class Case < Arel::Nodes::Node
+ include Arel::OrderPredications
+ include Arel::Predications
+ include Arel::AliasPredication
+
+ 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