Class: ActiveRecord::Refined::AST::Case
- Includes:
- Arithmetics, Predications
- Defined in:
- lib/active_record/refined/ast.rb
Overview
CASE, in both of the shapes SQL has for it. With an operand, each
when is something to compare it against; without one, each when is
a condition of its own.
Every method returns a new node rather than adding to this one, so a case kept in a variable can be branched from more than once.
Defined Under Namespace
Classes: Pending
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
-
#whens ⇒ Object
readonly
Returns the value of attribute whens.
Class Method Summary collapse
-
.argument(name, value, block) ⇒ Object
A value or a block, and exactly one of them: the block is what makes
when { :age >= 60 }read like the blocks around it, and the value is what makeswhen(10)possible at all.
Instance Method Summary collapse
- #else(value = nil, &block) ⇒ Object
-
#initialize(operand = nil, whens = [], default = NOTHING) ⇒ Case
constructor
A new instance of Case.
-
#then ⇒ Object
Kernel#then is on every object, so
thenin the wrong place would be answered by it -- with no block, silently, with an Enumerator. - #to_arel(table, model) ⇒ Object
- #when(value = nil, &block) ⇒ Object
Methods included from Arithmetics
#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~
Methods included from Predications
#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_json, #distinct_from?, #end_with?, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?
Methods inherited from Node
Constructor Details
#initialize(operand = nil, whens = [], default = NOTHING) ⇒ Case
Returns a new instance of Case.
394 395 396 397 398 |
# File 'lib/active_record/refined/ast.rb', line 394 def initialize(operand = nil, whens = [], default = NOTHING) @operand = operand @whens = whens @default = default end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
392 393 394 |
# File 'lib/active_record/refined/ast.rb', line 392 def default @default end |
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
392 393 394 |
# File 'lib/active_record/refined/ast.rb', line 392 def operand @operand end |
#whens ⇒ Object (readonly)
Returns the value of attribute whens.
392 393 394 |
# File 'lib/active_record/refined/ast.rb', line 392 def whens @whens end |
Class Method Details
.argument(name, value, block) ⇒ Object
A value or a block, and exactly one of them: the block is what makes
when { :age >= 60 } read like the blocks around it, and the value is
what makes when(10) possible at all.
430 431 432 433 434 435 436 437 |
# File 'lib/active_record/refined/ast.rb', line 430 def self.argument(name, value, block) if block raise ArgumentError, "#{name} takes a value or a block, not both" unless value.nil? return block.call end raise ArgumentError, "#{name} needs a value or a block" if value.nil? value end |
Instance Method Details
#else(value = nil, &block) ⇒ Object
410 411 412 |
# File 'lib/active_record/refined/ast.rb', line 410 def else(value = nil, &block) Case.new(operand, whens, Case.argument(:else, value, block)) end |
#then ⇒ Object
Kernel#then is on every object, so then in the wrong place would be
answered by it -- with no block, silently, with an Enumerator.
406 407 408 |
# File 'lib/active_record/refined/ast.rb', line 406 def then(*) raise ArgumentError, "then follows a when, and there is none to follow here" end |
#to_arel(table, model) ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/active_record/refined/ast.rb', line 414 def to_arel(table, model) raise ArgumentError, "case needs a when before it means anything" if whens.empty? node = operand ? Arel::Nodes::Case.new(to_arel_operand(operand, table, model)) : Arel::Nodes::Case.new whens.each do |condition, result| node.when(to_arel_argument(condition, table, model)). then(to_arel_argument(result, table, model)) end node.else(to_arel_argument(default, table, model)) unless default.equal?(NOTHING) node end |