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_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.



736
737
738
739
740
# File 'lib/active_record/refined/ast.rb', line 736

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.



734
735
736
# File 'lib/active_record/refined/ast.rb', line 734

def default
  @default
end

#operandObject (readonly)

Returns the value of attribute operand.



734
735
736
# File 'lib/active_record/refined/ast.rb', line 734

def operand
  @operand
end

#whensObject (readonly)

Returns the value of attribute whens.



734
735
736
# File 'lib/active_record/refined/ast.rb', line 734

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)


779
780
781
782
783
784
785
786
# File 'lib/active_record/refined/ast.rb', line 779

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) ⇒ AST::Case

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

Returns:



759
760
761
# File 'lib/active_record/refined/ast.rb', line 759

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

#thenObject

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)


753
754
755
# File 'lib/active_record/refined/ast.rb', line 753

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

#to_arel(table, model) ⇒ Object

Raises:

  • (ArgumentError)


763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/active_record/refined/ast.rb', line 763

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) ⇒ AST::Case::When

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

Returns:

  • (AST::Case::When)


744
745
746
# File 'lib/active_record/refined/ast.rb', line 744

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