Class: Criteria
- Inherits:
-
Object
- Object
- Criteria
- Defined in:
- lib/criteria.rb
Overview
Criteria is a collection of Criterion as well as additional constraints regarding the order, limit and offset
see the readme for usage examples.
Defined Under Namespace
Modules: VERSION Classes: Association, Column, Criterion, ManyToOneAssociation, NotCriterion, OneToManyAssociation, Order
Instance Attribute Summary collapse
-
#default_operator ⇒ Object
Returns the value of attribute default_operator.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#offset ⇒ Object
Returns the value of attribute offset.
Instance Method Summary collapse
-
#&(criterion) ⇒ Object
AND this with another criterion.
- #<<(c) ⇒ Object
- #add(c = nil, operator = self.default_operator) {|c = Criteria.new| ... } ⇒ Object
-
#and(c = nil, &block) ⇒ Object
AND a criterion with the existing Criteria.
-
#ands ⇒ Object
Return a modifiable array of AND’d criteria.
- #associations ⇒ Object
-
#columns ⇒ Object
Return a unique list of column objects that are referenced in this query.
-
#group_by ⇒ Object
Get the collection of columns to group by.
-
#initialize(c = nil) {|_self| ... } ⇒ Criteria
constructor
Create a new criteria, optionally pass in a Criterion object You can also pass a block, and self will be yielded.
-
#or(c = nil, &block) ⇒ Object
OR a criterion with the existing Criteria.
-
#order_by ⇒ Object
Get the collection of Order objects to order by.
-
#ors ⇒ Object
Returns a modifiable array of OR’d criteria.
-
#select ⇒ Object
Returns an array of column’s to be selected.
-
#tables ⇒ Object
Get a read-only array of all the table names that will be included in this query.
- #to_group_by_sql ⇒ Object
-
#to_hash ⇒ Object
FIXME: this returns a list of table names in :include, whereas it should contain the relationship names.
- #to_order_by_sql ⇒ Object
- #to_s ⇒ Object
- #to_select_sql ⇒ Object
-
#to_where_sql ⇒ Object
Convert the AND and OR statements into the WHERE SQL.
-
#|(criterion) ⇒ Object
OR this with another criterion.
Constructor Details
#initialize(c = nil) {|_self| ... } ⇒ Criteria
Create a new criteria, optionally pass in a Criterion object You can also pass a block, and self will be yielded
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/criteria.rb', line 20 def initialize(c=nil) # :yields: self @and = [] @or = [] @order_by = [] @group_by = [] @select = [] @limit = nil @offset = nil @joins = [] @default_operator = :or self.add(c) if c.is_a? Criterion yield(self) if block_given? end |
Instance Attribute Details
#default_operator ⇒ Object
Returns the value of attribute default_operator.
16 17 18 |
# File 'lib/criteria.rb', line 16 def default_operator @default_operator end |
#limit ⇒ Object
Returns the value of attribute limit.
16 17 18 |
# File 'lib/criteria.rb', line 16 def limit @limit end |
#offset ⇒ Object
Returns the value of attribute offset.
16 17 18 |
# File 'lib/criteria.rb', line 16 def offset @offset end |
Instance Method Details
#&(criterion) ⇒ Object
AND this with another criterion
92 93 94 |
# File 'lib/criteria.rb', line 92 def &(criterion) self.and(criterion) end |
#<<(c) ⇒ Object
70 71 72 73 |
# File 'lib/criteria.rb', line 70 def <<(c) raise "<< does not accept a block, perhaps you were trying to pass it to Criteria.new?" if block_given? add(c) end |
#add(c = nil, operator = self.default_operator) {|c = Criteria.new| ... } ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/criteria.rb', line 75 def add(c=nil, operator=self.default_operator) yield(c = Criteria.new) if c.nil? and block_given? # puts "#{self} << OR #{c}" if c.is_a? Column raise "You cannot directly #{operator.to_s.upcase} an instanceof Column, you must call some sort of expression (eq, ne, gt, ge, etc) on it." end if operator==:or @or << c else @and << c end self end |
#and(c = nil, &block) ⇒ Object
AND a criterion with the existing Criteria
61 62 63 |
# File 'lib/criteria.rb', line 61 def and(c=nil, &block) add(c, :and, &block) end |
#ands ⇒ Object
Return a modifiable array of AND’d criteria
36 37 38 |
# File 'lib/criteria.rb', line 36 def ands @and end |
#associations ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/criteria.rb', line 150 def associations out = [] (@and + @or).each do |c| out+=c.associations end out end |
#columns ⇒ Object
Return a unique list of column objects that are referenced in this query
134 135 136 137 138 139 140 141 142 |
# File 'lib/criteria.rb', line 134 def columns columns = [] (@and + @or).each do |c| c.columns.each do |c2| columns << c2 end end columns.uniq end |
#group_by ⇒ Object
Get the collection of columns to group by
51 52 53 |
# File 'lib/criteria.rb', line 51 def group_by @group_by end |
#or(c = nil, &block) ⇒ Object
OR a criterion with the existing Criteria
66 67 68 |
# File 'lib/criteria.rb', line 66 def or(c=nil, &block) add(c, :or, &block) end |
#order_by ⇒ Object
Get the collection of Order objects to order by
56 57 58 |
# File 'lib/criteria.rb', line 56 def order_by @order_by end |
#ors ⇒ Object
Returns a modifiable array of OR’d criteria
41 42 43 |
# File 'lib/criteria.rb', line 41 def ors @ors end |
#select ⇒ Object
Returns an array of column’s to be selected
46 47 48 |
# File 'lib/criteria.rb', line 46 def select @select end |
#tables ⇒ Object
Get a read-only array of all the table names that will be included in this query
146 147 148 |
# File 'lib/criteria.rb', line 146 def tables columns.collect {|c| c.table_name }.uniq end |
#to_group_by_sql ⇒ Object
125 126 127 |
# File 'lib/criteria.rb', line 125 def to_group_by_sql @group_by.size>0 ? @group_by.collect {|g| g.to_s}.join(",") : nil end |
#to_hash ⇒ Object
FIXME: this returns a list of table names in :include, whereas it should contain the relationship names
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/criteria.rb', line 189 def to_hash { :include => self.associations, :conditions => self.to_where_sql, :limit => self.limit, :offset => self.offset, :order => self.to_order_by_sql, :group => self.to_group_by_sql, :select => self.to_select_sql } end |
#to_order_by_sql ⇒ Object
121 122 123 |
# File 'lib/criteria.rb', line 121 def to_order_by_sql @order_by.size>0 ? @order_by.collect {|o| o.to_s}.join(",") : nil end |
#to_s ⇒ Object
201 202 203 |
# File 'lib/criteria.rb', line 201 def to_s "Criteria(#{to_hash.inspect})" end |
#to_select_sql ⇒ Object
129 130 131 |
# File 'lib/criteria.rb', line 129 def to_select_sql @select.size>0 ? @select.collect{|s| s.to_s}.join(",") : nil end |
#to_where_sql ⇒ Object
Convert the AND and OR statements into the WHERE SQL
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/criteria.rb', line 102 def to_where_sql and_clauses = [] if @or.size>0 c = @or.collect {|c| c.to_where_sql}.join(" OR ") if @and.size>0 and_clauses << "(#{c})" else and_clauses << c end end if @and.size>0 and_clauses << @and.collect {|c| c.to_where_sql} end and_clauses.size>0 ? "(#{and_clauses.join(" AND ")})" : "" end |
#|(criterion) ⇒ Object
OR this with another criterion
97 98 99 |
# File 'lib/criteria.rb', line 97 def |(criterion) self.or(criterion) end |