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 104
def as other
  Arel::Nodes::As.new self, Arel::Nodes::SqlLiteral.new(other)
end
else(expression) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 80
def else expression
  @default = Case::Else.new expression
  self
end
eql?(other) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 96
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 92
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 85
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 44
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
      begin
        Arel::Table.engine.connection.schema_cache.columns_hash(obj.relation.table_name)[obj.name.to_s].type
      rescue Exception
        :string
      end
    else
      :string
    end
  end
end
then(expression) click to toggle source
# File lib/arel_extensions/nodes/case.rb, line 75
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 70
def when condition, expression = nil
  @conditions << Case::When.new(condition, expression)
  self
end