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: Pending

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#as, #asc, #desc

Constructor Details

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

Returns a new instance of Case.



398
399
400
401
402
# File 'lib/active_record/refined/ast.rb', line 398

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

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



396
397
398
# File 'lib/active_record/refined/ast.rb', line 396

def default
  @default
end

#operandObject (readonly)

Returns the value of attribute operand.



396
397
398
# File 'lib/active_record/refined/ast.rb', line 396

def operand
  @operand
end

#whensObject (readonly)

Returns the value of attribute whens.



396
397
398
# File 'lib/active_record/refined/ast.rb', line 396

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.

Raises:

  • (ArgumentError)


434
435
436
437
438
439
440
441
# File 'lib/active_record/refined/ast.rb', line 434

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



414
415
416
# File 'lib/active_record/refined/ast.rb', line 414

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

#thenObject

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)


410
411
412
# File 'lib/active_record/refined/ast.rb', line 410

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

#to_arel(table, model) ⇒ Object

Raises:

  • (ArgumentError)


418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/active_record/refined/ast.rb', line 418

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

#when(value = nil, &block) ⇒ Object



404
405
406
# File 'lib/active_record/refined/ast.rb', line 404

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