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



513
514
515
516
517
518
# File 'lib/criteria.rb', line 513

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.



498
499
500
# File 'lib/criteria.rb', line 498

def column
  @column
end

#operatorObject (readonly)

Returns the value of attribute operator.



498
499
500
# File 'lib/criteria.rb', line 498

def operator
  @operator
end

#valueObject

Returns the value of attribute value.



497
498
499
# File 'lib/criteria.rb', line 497

def value
  @value
end

Instance Method Details

#&(criterion) ⇒ Object

AND this with another criterion



537
538
539
540
541
# File 'lib/criteria.rb', line 537

def &(criterion)
  if !criterion.nil?
    to_criteria(:AND).and(criterion)
  end
end

#associationsObject



524
525
526
527
528
529
530
# File 'lib/criteria.rb', line 524

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

#bound_classObject



520
521
522
# File 'lib/criteria.rb', line 520

def bound_class
  @column.bound_class
end

#columnsObject

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



570
571
572
# File 'lib/criteria.rb', line 570

def columns
  [@column]
end

#countObject



565
566
567
# File 'lib/criteria.rb', line 565

def count
  to_criteria.count
end

#firstObject

Allow Criteria-esque calls directly on this Criterion



557
558
559
# File 'lib/criteria.rb', line 557

def first
  to_criteria.first
end

#inspect(indent = 0) ⇒ Object



590
591
592
# File 'lib/criteria.rb', line 590

def inspect(indent=0)
  to_s
end

#listObject



561
562
563
# File 'lib/criteria.rb', line 561

def list
  to_criteria.list
end

#notObject



532
533
534
# File 'lib/criteria.rb', line 532

def not
  NotCriterion.new(self)
end

#to_criteria(operator = :AND) ⇒ Object

Convert this lone Criterion into a proper Criteria object. This is used by the following first(), list() and count() methods to provide a shortcut to the Criteria behaviour



552
553
554
# File 'lib/criteria.rb', line 552

def to_criteria(operator=:AND)
  bound_class.new_criteria(self, operator)
end

#to_hashObject



579
580
581
582
583
584
# File 'lib/criteria.rb', line 579

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

#to_sObject



586
587
588
# File 'lib/criteria.rb', line 586

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

#to_where_sqlObject

Convert this criterion into WHERE SQL



575
576
577
# File 'lib/criteria.rb', line 575

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

#|(criterion) ⇒ Object

OR this with another criterion



544
545
546
547
548
# File 'lib/criteria.rb', line 544

def |(criterion)
  if !criterion.nil?
    to_criteria(:OR).or(criterion)
  end
end