Class: Operator

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

Constant Summary collapse

S_OP =
' %s '
S_OP_EXPR =
'(%s)'
INVERSE_OP =
{
  '='   => '<>',
  '<>'  => '=',
  '<'   => '>=',
  '>'   => '<=',
  '<='  => '>',
  '>='  => '<'
}

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 collapse

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(op, *members, **props) ⇒ Operator

Returns a new instance of Operator.



336
337
338
339
340
341
342
343
344
345
346
# File 'lib/eno/expressions.rb', line 336

def initialize(op, *members, **props)
  if Operator === members[0] && op == members[0].op
    members = members[0].members + members[1..-1]
  end
  if Operator === members.last && op == members.last.op
    members = members[0..-2] + members.last.members
  end

  super(*members, **props)
  @op = op
end

Instance Attribute Details

#opObject (readonly)

Returns the value of attribute op.



334
335
336
# File 'lib/eno/expressions.rb', line 334

def op
  @op
end

Instance Method Details

#!@Object



365
366
367
368
# File 'lib/eno/expressions.rb', line 365

def !@
  inverse = INVERSE_OP[@op]
  inverse ? Operator.new(inverse, *@members) : super
end

#to_sql(sql) ⇒ Object



351
352
353
354
# File 'lib/eno/expressions.rb', line 351

def to_sql(sql)
  op_s = S_OP % @op
  S_OP_EXPR % @members.map { |m| sql.quote(m) }.join(op_s)
end