Class: Kangaroo::Model::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/kangaroo/model/relation.rb

Instance Method Summary collapse

Instance Method Details

#[](n) ⇒ Kangaroo::Model::Base #[](n, m) ⇒ Array<Kangaroo::Model::Base> #[](n..m) ⇒ Array<Kangaroo::Model::Base>

Shortcut to set limit and offset and execute queries immediately. If #limit or #offset are already set, [] is equal to #to_a[]

Overloads:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/kangaroo/model/relation.rb', line 161

def [] start_or_range, stop = nil
  if @limit_clause || @offset_clause
    return to_a[start_or_range, stop] if stop
    return to_a[start_or_range]
  end

  c = _clone

  c.offset_clause = if start_or_range.is_a?(Range)
    range_end = start_or_range.end
    range_end += 1 unless start_or_range.exclude_end?

    c.limit_clause = range_end - start_or_range.begin
    start_or_range.begin
  elsif stop
    c.limit_clause = stop
    start_or_range
  else
    c.limit_clause = 1
    start_or_range
  end

  (stop.nil? && Integer===start_or_range) ? c.to_a.first : c.to_a
end

#context(context = {}) ⇒ Relation

Clone this relation and add a context

Parameters:

  • context (Hash) (defaults to: {})

Returns:



131
132
133
134
135
# File 'lib/kangaroo/model/relation.rb', line 131

def context context = {}
  _clone._tap do |c|
    c.context_clause = c.context_clause.merge(context)
  end
end

#countObject Also known as: size, length

Count how many records fulfill this conditions



67
68
69
# File 'lib/kangaroo/model/relation.rb', line 67

def count
  @target.count_by @where_clauses, search_parameters
end

#exists?Boolean

Check if a record with fulfilling this conditions exist

Returns:

  • (Boolean)


53
54
55
# File 'lib/kangaroo/model/relation.rb', line 53

def exists?
  @target.search(@where_clauses, search_parameters.merge(:limit => 1)).present?
end

#find(ids) ⇒ Object

Find record(s) by id(s)



58
59
60
61
62
63
64
# File 'lib/kangaroo/model/relation.rb', line 58

def find ids
  records = where(:id => ids)

  Array === ids ?
  records.all :
  records.first
end

#firstObject

Return only the first record



43
44
45
# File 'lib/kangaroo/model/relation.rb', line 43

def first
  limit(1).to_a.first
end

#lastObject

Return only the last record



48
49
50
# File 'lib/kangaroo/model/relation.rb', line 48

def last
  reverse.first
end

#limit(limit) ⇒ Relation

Clone this relation and (re)set the limit clause

Parameters:

  • limit (Number)

    maximum records to retriebe

Returns:



100
101
102
103
104
# File 'lib/kangaroo/model/relation.rb', line 100

def limit limit
  _clone._tap do |c|
    c.limit_clause = limit
  end
end

#offset(offset) ⇒ Relation

Clone this relation and (re)set the offset clause

Parameters:

  • offset (Number)

    number of records to skip

Returns:



110
111
112
113
114
# File 'lib/kangaroo/model/relation.rb', line 110

def offset offset
  _clone._tap do |c|
    c.offset_clause = offset
  end
end

#order(column, desc = false) ⇒ Object

Clone this relation and add an order instruction

Parameters:

  • column (String, Symbol)

    field to order by

  • desc (boolean) (defaults to: false)

    true to order descending



141
142
143
144
145
146
# File 'lib/kangaroo/model/relation.rb', line 141

def order column, desc = false
  column = column.to_s + " desc" if desc
  _clone._tap do |c|
    c.order_clause += [column.to_s]
  end
end

#reverseObject

Reverse all order clauses



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kangaroo/model/relation.rb', line 74

def reverse
  if @order_clause.blank?
    order('id', true)
  else
    _clone._tap do |c|
      c.order_clause = c.order_clause.map do |order|
        reverse_order order
      end
    end
  end
end

#select(*selects) ⇒ Relation

Clone this relation and add select expressions to select clause

Parameters:

  • selects (Array, String, Symbol)

    fields to retrieve

Returns:



120
121
122
123
124
125
# File 'lib/kangaroo/model/relation.rb', line 120

def select *selects
  selects = selects.flatten.map &:to_s
  _clone._tap do |c|
    c.select_clause += selects
  end
end

#to_aObject Also known as: all

Submit the query



37
38
39
# File 'lib/kangaroo/model/relation.rb', line 37

def to_a
  @target.search_and_read @where_clauses, search_parameters, read_parameters
end

#where(condition) ⇒ Relation

Clone this relation and add the condition to the where clause

Parameters:

  • condition (Hash, Array, String)

Returns:



90
91
92
93
94
# File 'lib/kangaroo/model/relation.rb', line 90

def where condition
  _clone._tap do |c|
    c.where_clauses += [condition]
  end
end