Class: Lore::Refined_Select
- Inherits:
-
Refined_Query
- Object
- Refined_Query
- Lore::Refined_Select
- Defined in:
- lib/lore/query_shortcuts.rb
Overview
class
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Handy wrapper for <request>.entities.each { |e| … }.
-
#entities ⇒ Object
(also: #perform, #to_a, #result)
Sends request defined by previous method calls to self.
-
#entity ⇒ Object
(also: #value)
Same as #entities.first.
- #first ⇒ Object
-
#initialize(accessor, statements = {}) ⇒ Refined_Select
constructor
A new instance of Refined_Select.
-
#limit(lim, off = 0) ⇒ Object
Limit part of a query.
-
#offset(off) ⇒ Object
Offset part of a query.
-
#ordered_by(attrib, dir = :asc) ⇒ Object
(also: #order_by, #sort_by)
Adds order statement to query.
-
#polymorphic ⇒ Object
Execute as polymorphic query, joining all concrete base models of this polymorhpic model.
-
#to_i ⇒ Object
When requesting a single value, #to_i can be used to retreive the result as integer instead of calling #entity: .
-
#to_s ⇒ Object
When requesting a single value, #to_i can be used to retreive the result as string instead of calling #entity:.
-
#to_select ⇒ Object
Returns Clause instance containing this select statement.
-
#values_of(*what) ⇒ Object
(also: #value_of)
Examples: .
Methods inherited from Refined_Query
#having, #method_missing, #or, #with
Constructor Details
#initialize(accessor, statements = {}) ⇒ Refined_Select
Returns a new instance of Refined_Select.
76 77 78 79 |
# File 'lib/lore/query_shortcuts.rb', line 76 def initialize(accessor, statements={}) @polymorphic = false super(accessor, statements) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Lore::Refined_Query
Instance Method Details
#each(&block) ⇒ Object
Handy wrapper for
<request>.entities.each { |e| ... }
186 187 188 |
# File 'lib/lore/query_shortcuts.rb', line 186 def each(&block) entities.each &block end |
#entities ⇒ Object Also known as: perform, to_a, result
Sends request defined by previous method calls to self. Examples:
Car.find(10).with(Car.name.like '%BMW%').entities # -> Array of 10 instances of model klass 'Car'
Before calling #entities, the request isn’t sent, but defined only. Therefore you safely can pass a query (Clause) object to other methods, like in this example:
def filter_cars(clause)
query.with((Car.name != '') & clause).limit(10).entities
end
filder = Car.find(10).with(Car.name.like '%Audi%').limit(20) # Request is not sent yet
filter_cars(filter) # Request will be sent here
# -> Car.find(10).with(Car.name.like '%Audi%').with(Car.name != '').limit(10).entities
# -> Car.find(10).with((Car.name.like '%Audi%') & (Car.name != '')).limit(10).entities
There are other methods not defining but executing a request: #entity, #each, #to_i, #to_s
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/lore/query_shortcuts.rb', line 236 def entities if @what.nil? then if @polymorphic then result = @accessor.polymorphic_select { |entity| entity.where(@condition) entity.limit(@limit, @offset) unless @limit.nil? @order_by.each { |o| entity.order_by(o[0], o[1]) } entity.group_by(@group_by) unless @group_by.nil? entity.having(@having) unless @having.nil? entity }.to_a else result = @accessor.select { |entity| entity.where(@condition) entity.limit(@limit, @offset) unless @limit.nil? @order_by.each { |o| entity.order_by(o[0], o[1]) } entity.group_by(@group_by) unless @group_by.nil? entity.having(@having) unless @having.nil? entity }.to_a end else result = Array.new @accessor.select_values(@what) { |entity| entity.where(@condition) entity.limit(@limit, @offset) unless @limit.nil? @order_by.each { |o| entity.order_by(o[0], o[1]) } entity.group_by(@group_by) unless @group_by.nil? entity.having(@having) unless @having.nil? entity }.each { |row| if row.kind_of? Hash then result << row.values['value'] elsif row.kind_of? String then result << row else result = row end } end return result end |
#entity ⇒ Object Also known as: value
Same as #entities.first. Useful when requesting one row or a single attribute only.
104 105 106 |
# File 'lib/lore/query_shortcuts.rb', line 104 def entity entities.first end |
#first ⇒ Object
108 109 110 |
# File 'lib/lore/query_shortcuts.rb', line 108 def first entities.first end |
#limit(lim, off = 0) ⇒ Object
Limit part of a query.
Something.all_with(Something.name == 'foo').limit(10,123).entities
Is same as
Something.find(10).with(Something.name == 'foo').offset(123).entities
Is same as
Something.find(10,123).with(Something.name == 'foo').entities
And results in
SELECT * FROM something WHERE something.name = 'foo' LIMIT 10,123
171 172 173 174 175 |
# File 'lib/lore/query_shortcuts.rb', line 171 def limit(lim, off=0) @limit = lim @offset = off return self end |
#offset(off) ⇒ Object
Offset part of a query.
Something.find(10).with(Something.name == 'foo').offset(123).entities
Results in
SELECT * FROM something WHERE something.name = 'foo' LIMIT 10,123
155 156 157 158 |
# File 'lib/lore/query_shortcuts.rb', line 155 def offset(off) @offset = off return self end |
#ordered_by(attrib, dir = :asc) ⇒ Object Also known as: order_by, sort_by
Adds order statement to query. Direction is either :desc or :asc. Example:
User.find(0..10).ordered_by(:surname, :asc)
User.find(20).ordered_by([:surname, :forename], :desc)
Aliases are #order_by and #sort_by.
142 143 144 145 |
# File 'lib/lore/query_shortcuts.rb', line 142 def ordered_by(attrib, dir=:asc) @order_by << [attrib, dir] return self end |
#polymorphic ⇒ Object
Execute as polymorphic query, joining all concrete base models of this polymorhpic model.
179 180 181 182 |
# File 'lib/lore/query_shortcuts.rb', line 179 def polymorphic @polymorphic = true return self end |
#to_i ⇒ Object
When requesting a single value, #to_i can be used to retreive the result as integer instead of calling #entity:
i1 = User.all(User.id).with(User.name.like '%Christina%').to_i
i2 = User.all(User.id).with(User.name.like '%Christina%').entity.to_i
assert_equal i1, i2
130 131 132 |
# File 'lib/lore/query_shortcuts.rb', line 130 def to_i entities.first.to_i end |
#to_s ⇒ Object
When requesting a single value, #to_i can be used to retreive the result as string instead of calling #entity:
s1 = User.all(User.name).with(User.name.like '%Christina%').to_s
s2 = User.all(User.name).with(User.name.like '%Christina%').entity
assert_equal s1, s2
120 121 122 |
# File 'lib/lore/query_shortcuts.rb', line 120 def to_s entities.first.to_s end |
#to_select ⇒ Object
Returns Clause instance containing this select statement. This is needed for all Clause methods expecting an inner select. Example:
inner = Car.all(Car.car_id).with(Car.seats > 2)
Car.all.where(Car.car_id).in(inner) # method 'in' calls inner.to_select
Full example:
Car.all.where(Car.car_id).in(
Manufacturer_Car.values_of(Manufacturer_Car.car_id).limit(10).sort_by(Manufacturer_Car.name)
)
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/lore/query_shortcuts.rb', line 203 def to_select @accessor.select(@what) { |entity| entity.where(@condition) entity.limit(@limit, @offset) unless @limit.nil? @order_by.each { |o| entity.order_by(o[0], o[1]) } entity.group_by(@group_by) unless @group_by.nil? entity.having(@having) unless @having.nil? entity } end |
#values_of(*what) ⇒ Object Also known as: value_of
Examples:
Initiates request for single attribute values instead model instances. Returns value as string, array or two-dimensional array, depending from parameters passed:
Car.find(2).values_of(Car.name, Car.id).ordered_by(Car.name, :asc).entities
-> [ ['BMW318i', '3'], ['BMW Mini', '5'] ]
Car.find(2).values_of(Car.name).ordered_by(Car.name, :asc).entities
-> [ 'BMW318i', 'BMW Mini']
Car.find(1).value_of(Car.name, Car.type).with(Car.name.ilike '%BMW%').entity
-> ['BMW318i', 'Convertible']
Car.find(1).value_of(Car.name).with(Car.name.ilike '%BMW%').entity
-> 'BMW318i'
96 97 98 99 |
# File 'lib/lore/query_shortcuts.rb', line 96 def values_of(*what) @what = what return self end |