diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-01-05 23:05:16 -0200 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-01-05 23:05:16 -0200 |
commit | dfd22638c2773d843ea1d2e56992a6ac355b3ecb (patch) | |
tree | 1bf97bcb2f6928a32b6acb93ea9886925a0e21b4 /lib/arel/nodes | |
parent | 347c7786f8e7ea0e9643ff707ce1ace8b3969d6c (diff) | |
parent | 4c7e50f9328aca4e294b41fce0832bf6ac4a939a (diff) | |
download | rails-dfd22638c2773d843ea1d2e56992a6ac355b3ecb.tar.gz rails-dfd22638c2773d843ea1d2e56992a6ac355b3ecb.tar.bz2 rails-dfd22638c2773d843ea1d2e56992a6ac355b3ecb.zip |
Merge pull request #400 from felixbuenemann/case-node
Implement CASE Conditional Expression
Diffstat (limited to 'lib/arel/nodes')
-rw-r--r-- | lib/arel/nodes/case.rb | 57 |
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 |