Class: Where
- Inherits:
-
Object
- Object
- Where
- Defined in:
- lib/where.rb
Defined Under Namespace
Classes: Clause
Instance Attribute Summary collapse
-
#clauses ⇒ Object
readonly
Returns the value of attribute clauses.
-
#default_params ⇒ Object
Returns the value of attribute default_params.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
Instance Method Summary collapse
- #&(params) ⇒ Object
-
#and(*params, &block) ⇒ Object
(also: #<<)
Appends an and expression to your where clause.
-
#and_not(*params, &block) ⇒ Object
Same as and, but negates the whole expression.
-
#blank? ⇒ Boolean
(also: #empty?)
Determines if any clauses have been added.
-
#initialize(criteria_or_options = nil, *params) {|_self| ... } ⇒ Where
constructor
Constructs a new where clause.
- #initialize_copy(from) ⇒ Object
-
#or(*params, &block) ⇒ Object
Appends an or expression to your where clause.
-
#or_not(*params, &block) ⇒ Object
Same as or, but negates the whole expression.
-
#to_s(format = nil) ⇒ Object
(also: #to_sql)
Converts the where clause to a SQL string.
- #|(params) ⇒ Object
Constructor Details
#initialize(criteria_or_options = nil, *params) {|_self| ... } ⇒ Where
Constructs a new where clause
optionally, you can provide a criteria, like the following:
Where.initialize "joke_title = ?", "He says, 'Under there', to which I reply, 'under where?'"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/where.rb', line 43 def initialize(=nil, *params, &block) @clauses=Array.new @target = self if .is_a?(Hash) criteria = nil = else criteria = = {} end self.and(criteria, *params) unless criteria.nil? self.default_params = [:default_params] || {} yield(self) if block_given? end |
Instance Attribute Details
#clauses ⇒ Object (readonly)
Returns the value of attribute clauses.
37 38 39 |
# File 'lib/where.rb', line 37 def clauses @clauses end |
#default_params ⇒ Object
Returns the value of attribute default_params.
36 37 38 |
# File 'lib/where.rb', line 36 def default_params @default_params end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
37 38 39 |
# File 'lib/where.rb', line 37 def target @target end |
Class Method Details
Instance Method Details
#&(params) ⇒ Object
102 103 104 |
# File 'lib/where.rb', line 102 def &(params) self.and(*params) end |
#and(*params, &block) ⇒ Object Also known as: <<
72 73 74 |
# File 'lib/where.rb', line 72 def and(*params, &block) @target.append_clause(params, "AND", &block) end |
#and_not(*params, &block) ⇒ Object
Same as and, but negates the whole expression
98 99 100 |
# File 'lib/where.rb', line 98 def and_not(*params, &block) @target.append_clause(params, "AND NOT", &block) end |
#blank? ⇒ Boolean Also known as: empty?
153 154 155 |
# File 'lib/where.rb', line 153 def blank? @clauses.empty? end |
#initialize_copy(from) ⇒ Object
59 60 61 |
# File 'lib/where.rb', line 59 def initialize_copy(from) @clauses = from.instance_variable_get("@clauses").clone end |
#or(*params, &block) ⇒ Object
88 89 90 |
# File 'lib/where.rb', line 88 def or(*params, &block) @target.append_clause(params, "OR", &block) end |
#or_not(*params, &block) ⇒ Object
Same as or, but negates the whole expression
93 94 95 |
# File 'lib/where.rb', line 93 def or_not(*params, &block) @target.append_clause(params, "OR NOT", &block) end |
#to_s(format = nil) ⇒ Object Also known as: to_sql
Converts the where clause to a SQL string.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/where.rb', line 119 def to_s(format=nil) output="" @clauses.each_index{|index| omit_conjuction = (index==0) output << @clauses[index].to_s(omit_conjuction) # Omit the clause if index=0 } case format when :where output.empty? ? "" : " WHERE #{output}" else output.empty? ? "(true)" : output end end |
#|(params) ⇒ Object
106 107 108 |
# File 'lib/where.rb', line 106 def |(params) self.or(*params) end |