class RecordCache::Query

Container for the Query parameters

Attributes

limit[R]
sort_orders[R]
wheres[R]

Public Class Methods

new(equality = nil) click to toggle source
# File lib/record_cache/query.rb, line 7
def initialize(equality = nil)
  @wheres = equality || {}
  @sort_orders = []
  @limit = nil
  @where_values = {}
end

Public Instance Methods

cache_key() click to toggle source

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

# File lib/record_cache/query.rb, line 52
def cache_key
  @cache_key ||= generate_key
end
limit=(limit) click to toggle source
# File lib/record_cache/query.rb, line 47
def limit=(limit)
  @limit = limit.to_i
end
order_by(attribute, ascending = true) click to toggle source

Add a sort order to the query

# File lib/record_cache/query.rb, line 39
def order_by(attribute, ascending = true)
  @sort_orders << [attribute.to_s, ascending]
end
sorted?() click to toggle source
# File lib/record_cache/query.rb, line 43
def sorted?
  @sort_orders.size > 0
end
to_s() click to toggle source

DEPRECATED

# 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) click to toggle source

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

# 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) click to toggle source

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 @param attribute: the attribute name @param type: the type to be retrieved, :integer or :string (defaults to :integer)

# 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) click to toggle source

Retrieve the values for the given attribute from the where statements Returns nil if no the attribute is not present @param attribute: the attribute name @param type: the type to be retrieved, :integer or :string (defaults to :integer)

# 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

Private Instance Methods

array_of_values(values, type) click to toggle source
# File lib/record_cache/query.rb, line 81
def array_of_values(values, type)
  return nil unless values
  values = values.is_a?(Array) ? values.dup : [values]
  values.compact!
  if type == :integer
    values = values.map{|value| value.to_i} unless values.first.is_a?(Fixnum)
    return nil unless values.all?{ |value| value && value > 0 } # all values must be positive integers
  elsif type == :string
    values = values.map{|value| value.to_s} unless values.first.is_a?(String)
  end
  values
end
generate_key() click to toggle source
# File lib/record_cache/query.rb, line 70
def generate_key
  key = ""
  key << @limit.to_s if @limit
  key << @sort_orders.map{|attr,asc| "#{asc ? '+' : '-'}#{attr}"}.join if sorted?
  if @wheres.any?
    key << "?"
    key << @wheres.map{|k,v| "#{k}=#{v.inspect}"}.join("&")
  end
  key
end