class RecordCache::Scope

Attributes

model_class[R]
query[R]

Public Class Methods

new(model_class, query) click to toggle source
# File lib/record_cache/scope.rb, line 5
def initialize(model_class, query)
  @model_class = model_class
  @query       = query
end

Public Instance Methods

conditions() click to toggle source
# File lib/record_cache/scope.rb, line 41
def conditions
  @conditions ||= begin
    query.collect do |field, scope|
      if defined?(AntiObject) and scope.kind_of?(AntiObject)
        scope  = ~scope
        invert = true
      end

      if scope.nil?
        op = invert ? 'IS NOT' : 'IS'
        "#{field} #{op} NULL"
      elsif scope.is_a?(Array)
        op = invert ? 'NOT IN' : 'IN'
        model_class.send(:sanitize_sql, ["#{field} #{op} (?)", scope])
      else
        op = invert ? '!=' : '='
        model_class.send(:sanitize_sql, ["#{field} #{op} ?", scope])
      end
    end.join(' AND ')
  end
  @conditions
end
empty?() click to toggle source
# File lib/record_cache/scope.rb, line 10
def empty?
  query.empty?
end
fields() click to toggle source
# File lib/record_cache/scope.rb, line 14
def fields
  query.keys
end
match?(field, value) click to toggle source
# File lib/record_cache/scope.rb, line 30
def match?(field, value)
  scope = query[field]
  if defined?(AntiObject) and scope.kind_of?(AntiObject)
    scope  = ~scope
    invert = true
  end

  match = [*scope].include?(value)
  invert ? !match : match
end
match_current?(model) click to toggle source
# File lib/record_cache/scope.rb, line 18
def match_current?(model)
  fields.all? do |field|
    match?( field, model.send(field) )
  end
end
match_previous?(model) click to toggle source
# File lib/record_cache/scope.rb, line 24
def match_previous?(model)
  fields.all? do |field|
    match?( field, model.attr_was(field) )
  end
end