Class: Case

Inherits:
Expression show all
Defined in:
lib/eno/expressions.rb

Constant Summary collapse

S_WHEN =
'when %s then %s'
S_ELSE =
'else %s'
S_CASE =
'case %s end'
S_SPACE =
' '

Constants inherited from Expression

Expression::S_AND, Expression::S_DIV, Expression::S_EQ, Expression::S_GT, Expression::S_GTE, Expression::S_LT, Expression::S_LTE, Expression::S_MINUS, Expression::S_MOD, Expression::S_MUL, Expression::S_NEQ, Expression::S_OR, Expression::S_PLUS, Expression::S_TILDE

Instance Attribute Summary

Attributes inherited from Expression

#members, #props

Instance Method Summary collapse

Methods inherited from Expression

#!=, #!@, #%, #&, #*, #+, #-, #/, #<, #<=, #==, #=~, #>, #>=, #^, #as, #cast, #desc, #in, #inner_join, #join, #not_in, #not_null?, #null?, #over, #|

Constructor Details

#initialize(conditions) ⇒ Case

Returns a new instance of Case.



179
180
181
# File 'lib/eno/expressions.rb', line 179

def initialize(conditions)
  @props = conditions
end

Instance Method Details

#to_sql(sql) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/eno/expressions.rb', line 188

def to_sql(sql)
  conditions = @props.inject([]) { |a, (k, v)|
    if k.is_a?(Symbol) && k == :default
      a
    else
      a << (S_WHEN % [sql.quote(k), sql.quote(v)])
    end
  }
  if default = @props[:default]
    conditions << (S_ELSE % sql.quote(default))
  end

  S_CASE % conditions.join(S_SPACE)
end