Class: RecordCache::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/record_cache/query.rb

Overview

Container for the Query parameters

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(equality = nil) ⇒ Query

Returns a new instance of Query.



7
8
9
10
11
12
# File 'lib/record_cache/query.rb', line 7

def initialize(equality = nil)
  @wheres = equality || {}
  @sort_orders = []
  @limit = nil
  @where_values = {}
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



5
6
7
# File 'lib/record_cache/query.rb', line 5

def limit
  @limit
end

#sort_ordersObject (readonly)

Returns the value of attribute sort_orders.



5
6
7
# File 'lib/record_cache/query.rb', line 5

def sort_orders
  @sort_orders
end

#wheresObject (readonly)

Returns the value of attribute wheres.



5
6
7
# File 'lib/record_cache/query.rb', line 5

def wheres
  @wheres
end

Instance Method Details

#cache_keyObject

DEPRECATED: retrieve a unique key for this Query (used in RequestCache)



52
53
54
# File 'lib/record_cache/query.rb', line 52

def cache_key
  @cache_key ||= generate_key
end

#order_by(attribute, ascending = true) ⇒ Object

Add a sort order to the query



39
40
41
# File 'lib/record_cache/query.rb', line 39

def order_by(attribute, ascending = true)
  @sort_orders << [attribute.to_s, ascending]
end

#sorted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/record_cache/query.rb', line 43

def sorted?
  @sort_orders.size > 0
end

#to_sObject

DEPRECATED



57
58
59
60
61
62
63
64
65
66
# File 'lib/record_cache/query.rb', line 57

def to_s
  s = "SELECT "
  s << @wheres.map{|k,v| "#{k} = #{v.inspect}"}.join(" AND ")
  if sorted?
    order_by_clause = @sort_orders.map{|attr,asc| "#{attr} #{asc ? 'ASC' : 'DESC'}"}.join(', ')
    s << " ORDER_BY #{order_by_clause}"
  end
  s << " LIMIT #{@limit}" if @limit
  s
end

#where(attribute, values) ⇒ Object

Set equality of an attribute (usually found in where clause)



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

def where(attribute, values)
  @wheres[attribute.to_sym] = values if attribute
end

#where_value(attribute, type = :integer) ⇒ Object

Retrieve the single value for the given attribute from the where statements Returns nil if the attribute is not present, or if it contains multiple values

Parameters:

  • attribute:

    the attribute name

  • type:

    the type to be retrieved, :integer or :string (defaults to :integer)



32
33
34
35
36
# File 'lib/record_cache/query.rb', line 32

def where_value(attribute, type = :integer)
  values = where_values(attribute, type)
  return nil unless values && values.size == 1
  values.first
end

#where_values(attribute, type = :integer) ⇒ Object

Retrieve the values for the given attribute from the where statements Returns nil if no the attribute is not present

Parameters:

  • attribute:

    the attribute name

  • type:

    the type to be retrieved, :integer or :string (defaults to :integer)



23
24
25
26
# File 'lib/record_cache/query.rb', line 23

def where_values(attribute, type = :integer)
  return @where_values[attribute] if @where_values.key?(attribute)
  @where_values[attribute] ||= array_of_values(@wheres[attribute], type)
end