Class: Spider::Model::Query

Inherits:
Object show all
Defined in:
lib/spiderfw/model/query.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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.



46
47
48
49
50
51
52
53
54
# File 'lib/spiderfw/model/query.rb', line 46

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

#conditionCondition

The Condition instance



24
25
26
# File 'lib/spiderfw/model/query.rb', line 24

def condition
  @condition
end

#group_by_elementsArray



33
34
35
# File 'lib/spiderfw/model/query.rb', line 33

def group_by_elements
  @group_by_elements
end

#limitFixnum

Limit the returned results to :limit objects



18
19
20
# File 'lib/spiderfw/model/query.rb', line 18

def limit
  @limit
end

#offsetFixnum

Skip the first :offset objects



15
16
17
# File 'lib/spiderfw/model/query.rb', line 15

def offset
  @offset
end

#orderArray

An array of element-direction (:asc or :desc) pairs



12
13
14
# File 'lib/spiderfw/model/query.rb', line 12

def order
  @order
end

#page(page, rows) ⇒ self (readonly)

Pagination: request the given page, for given rows per page



31
32
33
# File 'lib/spiderfw/model/query.rb', line 31

def page
  @page
end

#page_rowsFixnum (readonly)



29
30
31
# File 'lib/spiderfw/model/query.rb', line 29

def page_rows
  @page_rows
end

#polymorphsArray

Requests subclasses of the queried model



21
22
23
# File 'lib/spiderfw/model/query.rb', line 21

def polymorphs
  @polymorphs
end

#requestRequest

The Request instance



27
28
29
# File 'lib/spiderfw/model/query.rb', line 27

def request
  @request
end

Class Method Details

.where(*params) ⇒ Object

Instantiates a new query, calling Condition#where on the condition. See Query#new for arguments Return #Condition



38
39
40
# File 'lib/spiderfw/model/query.rb', line 38

def self.where(*params)
    return self.class.new.condition.where(*params)
end

Instance Method Details

#cloneQuery

Returns a deep copy.



205
206
207
208
209
210
211
212
213
# File 'lib/spiderfw/model/query.rb', line 205

def clone
    cl = self.class.new(@condition.clone, @request.clone)
    cl.order = @order.clone
    cl.offset = @offset
    cl.limit = @limit
    cl.polymorphs = @polymorphs.clone
    cl.group_by_elements = @group_by_elements.clone if @group_by_elements
    return cl
end

#group_by(*elements) ⇒ self

Elements to group_by (used by some mappers for aggregate queries)



112
113
114
115
116
# File 'lib/spiderfw/model/query.rb', line 112

def group_by(*elements)
    @group_by_elements ||= []
    @group_by_elements += elements
    return self
end

#only_onevoid

This method returns an undefined value.

Request only the first result.



169
170
171
172
# File 'lib/spiderfw/model/query.rb', line 169

def only_one
    self.limit = 1
    @only_one = true
end

#only_one?bool



175
176
177
# File 'lib/spiderfw/model/query.rb', line 175

def only_one?
    @only_one
end

#only_polymorphsRequest

Requests only polymorphs. (see Request#only_polymorphs).



156
157
158
# File 'lib/spiderfw/model/query.rb', line 156

def only_polymorphs
    @request.only_polymorphs
end

#order_by(*elements) ⇒ self

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)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/spiderfw/model/query.rb', line 90

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?bool



199
200
201
# File 'lib/spiderfw/model/query.rb', line 199

def polymorphs?
    @polymorphs.length > 0
end

#select(*elements) ⇒ self

Adds each element in the given list to the request.



121
122
123
124
125
126
# File 'lib/spiderfw/model/query.rb', line 121

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.



132
133
134
135
136
# File 'lib/spiderfw/model/query.rb', line 132

def where(condition=nil, &proc)
    condition = Condition.new(&proc) unless (condition)
    @condition << condition
    return self
end

#with_polymorph(type, request = nil) ⇒ self

Requests a polymorph. This means that the mapper will try to differentiate the result into the subclasses given here.



143
144
145
146
147
148
149
150
151
152
# File 'lib/spiderfw/model/query.rb', line 143

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_superclassRequest

Load also objects that belong to the superclass, and don’t have this subclass. See #Request#with_superclass.



163
164
165
# File 'lib/spiderfw/model/query.rb', line 163

def with_superclass
    @request.with_superclass
end