Class: Criteria::Criterion

Inherits:
Object
  • Object
show all
Defined in:
lib/criteria.rb

Direct Known Subclasses

NotCriterion

Constant Summary collapse

NOT_EQUAL =
"<>"
EQUAL =
"="
GREATER_THAN =
">"
GREATER_THAN_OR_EQUAL =
">="
LESS_THAN =
"<"
LESS_THAN_OR_EQUAL =
"<="
IN =
"IN"
NOT_IN =
"NOT IN"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, operator = nil, value = nil, opts = {}) ⇒ Criterion

Create a new criteria. Generally, you will not call this directly, rather create it via the Column instance.

The constructor takes a Column instance, an operator string and an optional value



426
427
428
429
430
431
# File 'lib/criteria.rb', line 426

def initialize(column, operator=nil, value=nil, opts={})
  @column = column
  @operator = operator
  @value = value
  @opts = opts
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



411
412
413
# File 'lib/criteria.rb', line 411

def column
  @column
end

#operatorObject (readonly)

Returns the value of attribute operator.



411
412
413
# File 'lib/criteria.rb', line 411

def operator
  @operator
end

#valueObject

Returns the value of attribute value.



410
411
412
# File 'lib/criteria.rb', line 410

def value
  @value
end

Instance Method Details

#&(criterion) ⇒ Object

AND this with another criterion



446
447
448
449
450
451
452
453
# File 'lib/criteria.rb', line 446

def &(criterion)
  if !criterion.nil?
    c = Criteria.new
    c.and self
    c.and criterion
    c
  end
end

#associationsObject



433
434
435
436
437
438
439
# File 'lib/criteria.rb', line 433

def associations
  if @column.is_a? Association
    [@column.association_name]
  else
    []
  end
end

#columnsObject

Return a list of columns associated with this criterion, always an array with one element



466
467
468
# File 'lib/criteria.rb', line 466

def columns
  [@column]
end

#notObject



441
442
443
# File 'lib/criteria.rb', line 441

def not
  NotCriterion.new(self)
end

#to_hashObject



475
476
477
478
479
480
# File 'lib/criteria.rb', line 475

def to_hash
  {
    :include => self.associations,
    :conditions => to_where_sql
  }
end

#to_sObject



482
483
484
# File 'lib/criteria.rb', line 482

def to_s
  "#{self.class}(#{to_where_sql})"
end

#to_where_sqlObject

Convert this criterion into WHERE SQL



471
472
473
# File 'lib/criteria.rb', line 471

def to_where_sql 
  "#{@column.to_sql_name} #{@operator} #{@column.quote_value(@value)}"
end

#|(criterion) ⇒ Object

OR this with another criterion



456
457
458
459
460
461
462
463
# File 'lib/criteria.rb', line 456

def |(criterion)
  if !criterion.nil?
    c = Criteria.new
    c.or self
    c.or criterion
    c
  end
end