Class: ActiveRecord::Refined::AST::Case

Inherits:
Node
  • Object
show all
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: When

Instance Method Summary collapse

Methods included from Arithmetics

#&, #*, #+, #-, #/, #<<, #>>, #^, #bitwise_and, #bitwise_not, #bitwise_or, #bitwise_xor, #|, #~

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_text, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #keys, #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

#as, #asc, #collate, #desc

Constructor Details

#initialize(operand = nil, whens = [], default = NOTHING) ⇒ Case

Returns a new instance of Case.



867
868
869
870
871
# File 'lib/active_record/refined/ast.rb', line 867

def initialize(operand = nil, whens = [], default = NOTHING)
  @operand = operand
  @whens = whens
  @default = default
end

Instance Method Details

#else(value = nil, &block) ⇒ AST::Case

ELSE value, as a value or a block, closing the CASE. Without one the CASE gives NULL where no when matched.

Returns:



890
891
892
# File 'lib/active_record/refined/ast.rb', line 890

def else(value = nil, &block)
  Case.new(operand, whens, Case.argument(:else, value, block))
end

#then ⇒ Object

THEN, which belongs after a when; here it says so. Kernel#then is on every object, so then in the wrong place would be answered by it -- with no block, silently, with an Enumerator.

Raises:

  • (ArgumentError)


884
885
886
# File 'lib/active_record/refined/ast.rb', line 884

def then(*)
  raise ArgumentError, "then follows a when, and there is none to follow here"
end

#when(value = nil, &block) ⇒ AST::Case::When

The next WHEN: a value to compare the operand against, or a condition as a value or a block.

Returns:



875
876
877
# File 'lib/active_record/refined/ast.rb', line 875

def when(value = nil, &block)
  When.new(self, Case.argument(:when, value, block))
end