class ArelExtensions::Nodes::Case

Attributes

case[RW]
conditions[RW]
default[RW]

Public Class Methods

new(expression = nil, default = nil) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 11
def initialize expression = nil, default = nil
  @case = expression
  @conditions = []
  @default = default
end

Public Instance Methods

==(other)
Alias for: eql?
as(other) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 101
def as other
  Arel::Nodes::As.new self, Arel.sql(other)
end
else(expression) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 77
def else expression
  @default = Case::Else.new expression
  self
end
eql?(other) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 93
def eql? other
  self.class == other.class &&
    self.case == other.case &&
    self.conditions == other.conditions &&
    self.default == other.default
end
Also aliased as: ==
hash() click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 89
def hash
  [@case, @conditions, @default].hash
end
initialize_copy(other) click to toggle source
Calls superclass method
# File lib/arel_extensions/nodes/case.rb, line 82
def initialize_copy other
  super
  @case = @case.clone if @case
  @conditions = @conditions.map { |x| x.clone }
  @default = @default.clone if @default
end
return_type() click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 45
def return_type
  obj = if @conditions.length > 0
          @conditions.last.right
    elsif @default
      @default.expr
    end
  if obj.respond_to?(:return_type)
    obj.return_type
  else
    case obj
    when Integer, Float
      :number
    when Date, DateTime, Time
      :datetime
    when Arel::Attributes::Attribute
      Arel.column_of(obj.relation.table_name, obj.name.to_s)&.type || :string
    else
      :string
    end
  end
end
then(expression) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 72
def then expression
  @conditions.last.right = expression
  self
end
when(condition, expression = nil) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 67
def when condition, expression = nil
  @conditions << Case::When.new(condition, expression)
  self
end