Class: Criteria::Criterion
- Inherits:
-
Object
- Object
- Criteria::Criterion
- Defined in:
- lib/criteria.rb
Direct Known Subclasses
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
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#&(criterion) ⇒ Object
AND this with another criterion.
- #associations ⇒ Object
- #bound_class ⇒ Object
-
#columns ⇒ Object
Return a list of columns associated with this criterion, always an array with one element.
- #count ⇒ Object
-
#first ⇒ Object
Allow Criteria-esque calls directly on this Criterion.
-
#initialize(column, operator = nil, value = nil, opts = {}) ⇒ Criterion
constructor
Create a new criteria.
- #inspect(indent = 0) ⇒ Object
- #list ⇒ Object
- #not ⇒ Object
-
#to_criteria(operator = :AND) ⇒ Object
Convert this lone Criterion into a proper Criteria object.
- #to_hash ⇒ Object
- #to_s ⇒ Object
-
#to_where_sql ⇒ Object
Convert this criterion into WHERE SQL.
-
#|(criterion) ⇒ Object
OR this with another criterion.
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
#column ⇒ Object (readonly)
Returns the value of attribute column.
498 499 500 |
# File 'lib/criteria.rb', line 498 def column @column end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
498 499 500 |
# File 'lib/criteria.rb', line 498 def operator @operator end |
#value ⇒ Object
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 |
#associations ⇒ Object
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_class ⇒ Object
520 521 522 |
# File 'lib/criteria.rb', line 520 def bound_class @column.bound_class end |
#columns ⇒ Object
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 |
#count ⇒ Object
565 566 567 |
# File 'lib/criteria.rb', line 565 def count to_criteria.count end |
#first ⇒ Object
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 |
#list ⇒ Object
561 562 563 |
# File 'lib/criteria.rb', line 561 def list to_criteria.list end |
#not ⇒ Object
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_hash ⇒ Object
579 580 581 582 583 584 |
# File 'lib/criteria.rb', line 579 def to_hash { :include => self.associations, :conditions => to_where_sql } end |
#to_s ⇒ Object
586 587 588 |
# File 'lib/criteria.rb', line 586 def to_s "#{self.class}(#{to_where_sql})" end |
#to_where_sql ⇒ Object
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 |