Class: Spider::Model::Query
Overview
The Query combines a Condition and a Request, offering convenience methods and allowing to further specify how the data should be returned.
Instance Attribute Summary collapse
-
#condition ⇒ Object
The Condition instance.
-
#limit ⇒ Object
Limit the returned results to :limit objects.
-
#offset ⇒ Object
Skip the first :offset objects.
-
#order ⇒ Object
An array of element-direction (:asc or :desc) pairs.
-
#page(page, rows) ⇒ Object
readonly
Returns the value of attribute page.
-
#page_rows ⇒ Object
readonly
Returns the value of attribute page_rows.
-
#polymorphs ⇒ Object
Requests subclasses of the queried model.
-
#request ⇒ Object
The Request instance.
Class Method Summary collapse
-
.where(*params) ⇒ Object
Instantiates a new query, calling Condition#where on the condition.
Instance Method Summary collapse
-
#clone ⇒ Object
Returns a deep copy.
- #first ⇒ Object
-
#initialize(condition = nil, request = nil, &proc) ⇒ Query
constructor
Parameters are a Condition and a Request.
- #only_one ⇒ Object
- #only_one? ⇒ Boolean
-
#only_polymorphs ⇒ Object
Requests only polymorphs.
-
#order_by(*elements) ⇒ Object
Sets required oreder.
-
#polymorphs? ⇒ Boolean
information methods #.
-
#select(*elements) ⇒ Object
Adds each element in the given list to the request.
-
#where(condition = nil, &proc) ⇒ Object
Takes an argument or a block.
-
#with_polymorph(type, request = nil) ⇒ Object
Requests a polymorph.
- #with_superclass ⇒ Object
Constructor Details
#initialize(condition = nil, request = nil, &proc) ⇒ Query
Parameters are a Condition and a Request. If a block is given, it will be parsed by the Condition.
31 32 33 34 35 36 37 38 39 |
# File 'lib/spiderfw/model/query.rb', line 31 def initialize(condition = nil, request=nil, &proc) @condition = condition.is_a?(Condition) ? condition : Condition.new(condition) @request = request.is_a?(Request) ? request : Request.new(request) @polymorphs = [] @order = [] if (proc) @condition = Condition.new(&proc) end end |
Instance Attribute Details
#condition ⇒ Object
The Condition instance
19 20 21 |
# File 'lib/spiderfw/model/query.rb', line 19 def condition @condition end |
#limit ⇒ Object
Limit the returned results to :limit objects
15 16 17 |
# File 'lib/spiderfw/model/query.rb', line 15 def limit @limit end |
#offset ⇒ Object
Skip the first :offset objects
13 14 15 |
# File 'lib/spiderfw/model/query.rb', line 13 def offset @offset end |
#order ⇒ Object
An array of element-direction (:asc or :desc) pairs
11 12 13 |
# File 'lib/spiderfw/model/query.rb', line 11 def order @order end |
#page(page, rows) ⇒ Object (readonly)
Returns the value of attribute page.
23 24 25 |
# File 'lib/spiderfw/model/query.rb', line 23 def page @page end |
#page_rows ⇒ Object (readonly)
Returns the value of attribute page_rows.
22 23 24 |
# File 'lib/spiderfw/model/query.rb', line 22 def page_rows @page_rows end |
#polymorphs ⇒ Object
Requests subclasses of the queried model
17 18 19 |
# File 'lib/spiderfw/model/query.rb', line 17 def polymorphs @polymorphs end |
#request ⇒ Object
The Request instance
21 22 23 |
# File 'lib/spiderfw/model/query.rb', line 21 def request @request end |
Class Method Details
.where(*params) ⇒ Object
Instantiates a new query, calling Condition#where on the condition.
26 27 28 |
# File 'lib/spiderfw/model/query.rb', line 26 def self.where(*params) return self.class.new.condition.where(*params) end |
Instance Method Details
#clone ⇒ Object
Returns a deep copy.
157 158 159 160 161 162 163 164 |
# File 'lib/spiderfw/model/query.rb', line 157 def clone cl = self.class.new(@condition.clone, @request.clone) cl.order = @order.clone cl.offset = @offset cl.limit = @limit cl.polymorphs = @polymorphs.clone return cl end |
#first ⇒ Object
123 124 125 |
# File 'lib/spiderfw/model/query.rb', line 123 def first self.limit = 1 end |
#only_one ⇒ Object
127 128 129 130 |
# File 'lib/spiderfw/model/query.rb', line 127 def only_one self.limit = 1 @only_one = true end |
#only_one? ⇒ Boolean
133 134 135 |
# File 'lib/spiderfw/model/query.rb', line 133 def only_one? @only_one end |
#only_polymorphs ⇒ Object
Requests only polymorphs. (see Request#only_polymorphs).
115 116 117 |
# File 'lib/spiderfw/model/query.rb', line 115 def only_polymorphs @request.only_polymorphs end |
#order_by(*elements) ⇒ Object
Sets required oreder. Arguments can be:
-
an element-direction pair, or
-
a list of ‘element direction’ strings, or
-
a list of elements (:asc direction will be implied).
Example:
query.order_by(:element, :desc)
query.order_by('name desc', :rating)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/spiderfw/model/query.rb', line 67 def order_by(*elements) if (elements.length == 2 && [:asc, :desc].include?(elements[1])) @order << elements return self end elements.each do |l| if (l.is_a?(String)) parts = l.split(' ') name = parts[0] order = parts[1] else parts = [l, :asc] end raise "Order elements must be strings or symbols" unless parts[0].is_a?(String) || parts[0].is_a?(Symbol) @order << [parts[0], parts[1]] end return self end |
#polymorphs? ⇒ Boolean
information methods #
152 153 154 |
# File 'lib/spiderfw/model/query.rb', line 152 def polymorphs? # :nodoc: @polymorphs.length > 0 end |
#select(*elements) ⇒ Object
Adds each element in the given list to the request.
87 88 89 90 91 92 |
# File 'lib/spiderfw/model/query.rb', line 87 def select(*elements) elements.each do |element| @request.request(element.to_s) end return self end |
#where(condition = nil, &proc) ⇒ Object
Takes an argument or a block. If given an argument, will use it as a Condition. If given a block, will use it on the Condition.
96 97 98 99 100 |
# File 'lib/spiderfw/model/query.rb', line 96 def where(condition=nil, &proc) condition = Condition.new(&proc) unless (condition) @condition << condition return self end |
#with_polymorph(type, request = nil) ⇒ Object
Requests a polymorph.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/spiderfw/model/query.rb', line 103 def with_polymorph(type, request=nil) query = self.class.new(query) unless query.is_a?(self.class) @polymorphs << type unless request request = Request.new type.primary_keys.each{ |k| request[k.name] = true } end @request.with_polymorphs(type, request) return self end |
#with_superclass ⇒ Object
119 120 121 |
# File 'lib/spiderfw/model/query.rb', line 119 def with_superclass @request.with_superclass end |