class Criteria

Attributes

ids_key[R]
object_class[R]

Public Class Methods

new(base) click to toggle source
# File lib/redisant/criteria.rb, line 7
def initialize base
  if base.is_a? Relation
    @ids_key = base.redis_key
    @object_class = base.object_class
    criteria[:relation] = base
  else
    @ids_key = base.id_key
    @object_class = base
  end
end

Public Instance Methods

any?() click to toggle source
# File lib/redisant/criteria.rb, line 80
def any?
  criteria[:exists] = true
  criteria[:ids] = true
  result
end
count() click to toggle source
# File lib/redisant/criteria.rb, line 37
def count
  criteria[:count] = true
  result
end
count?() click to toggle source
# File lib/redisant/criteria.rb, line 121
def count?
  criteria[:count] == true
end
criteria() click to toggle source
# File lib/redisant/criteria.rb, line 23
def criteria
  @criteria ||= {:where => {}}
end
first(options={}) click to toggle source
# File lib/redisant/criteria.rb, line 47
def first options={}
  merge_options options
  criteria[:offset] = 0
  criteria[:limit] = 1
  criteria[:order] = :asc
  result
end
get_conditions() click to toggle source
# File lib/redisant/criteria.rb, line 137
def get_conditions
  criteria[:where]
end
get_order() click to toggle source
# File lib/redisant/criteria.rb, line 145
def get_order
  criteria[:order]
end
get_relation() click to toggle source
# File lib/redisant/criteria.rb, line 141
def get_relation
  criteria[:relation]
end
ids() click to toggle source
# File lib/redisant/criteria.rb, line 75
def ids
  criteria[:ids] = true
  self
end
ids?() click to toggle source
# File lib/redisant/criteria.rb, line 87
def ids?
  criteria[:ids] == true
end
last(options={}) click to toggle source
# File lib/redisant/criteria.rb, line 55
def last options={}
  merge_options options
  criteria[:offset] = 0
  criteria[:limit] = 1
  criteria[:order] = :desc
  result
end
limit(limit) click to toggle source
# File lib/redisant/criteria.rb, line 32
def limit limit
  criteria[:limit] = limit
  self
end
limit?() click to toggle source
# File lib/redisant/criteria.rb, line 117
def limit?
  criteria[:limit] != nil
end
load_if_needed() click to toggle source
# File lib/redisant/criteria.rb, line 154
def load_if_needed
  loaded = @loaded
  @loaded = true
  reload! unless loaded
end
loaded?() click to toggle source
# File lib/redisant/criteria.rb, line 161
def loaded?
  @loaded == true
end
method_missing(*args, &block) click to toggle source
# File lib/redisant/criteria.rb, line 149
def method_missing(*args, &block)
  load_if_needed
  @result.send(*args, &block)
end
num_conditions() click to toggle source
# File lib/redisant/criteria.rb, line 129
def num_conditions
  n = criteria[:where].keys.size
  if criteria[:relation]
    n += 1
  end
  n
end
order(options) click to toggle source
# File lib/redisant/criteria.rb, line 68
def order options
  raise Redisant::InvalidArgument.new('Invalid order') unless ['asc','desc'].include? options.to_s
  
  criteria[:order] = options
  self
end
order?() click to toggle source
# File lib/redisant/criteria.rb, line 113
def order?
  criteria[:order] != nil
end
random() click to toggle source
# File lib/redisant/criteria.rb, line 42
def random
  criteria[:random] = true
  result
end
random?() click to toggle source
# File lib/redisant/criteria.rb, line 125
def random?
  criteria[:random] == true
end
relation(relation) click to toggle source
# File lib/redisant/criteria.rb, line 18
def relation relation
  criteria[:relation] = relation
  self
end
reload!() click to toggle source
# File lib/redisant/criteria.rb, line 170
def reload!
  @result = Query.new(self).run
end
result() click to toggle source
# File lib/redisant/criteria.rb, line 165
def result
  load_if_needed
  @result
end
single?() click to toggle source
# File lib/redisant/criteria.rb, line 103
def single?
  criteria[:limit] == 1 ||
  criteria[:random] ||
  criteria[:count]
end
sort(options) click to toggle source
# File lib/redisant/criteria.rb, line 63
def sort options
  criteria[:sort] = options
  self
end
sort?() click to toggle source
# File lib/redisant/criteria.rb, line 109
def sort?
  criteria[:sort] != nil
end
where(options) click to toggle source
# File lib/redisant/criteria.rb, line 27
def where options
  merge_options options
  self
end
where?() click to toggle source
# File lib/redisant/criteria.rb, line 91
def where?
  criteria[:where].size > 0
end
where_multi?() click to toggle source
# File lib/redisant/criteria.rb, line 99
def where_multi?
  num_conditions >= 1
end
where_single?() click to toggle source
# File lib/redisant/criteria.rb, line 95
def where_single?
  num_conditions == 1
end

Private Instance Methods

merge_options(options) click to toggle source
# File lib/redisant/criteria.rb, line 176
def merge_options options
  criteria[:where].merge!(options)
end