module Modis::Index

Public Class Methods

included(base) click to toggle source
# File lib/modis/index.rb, line 5
def self.included(base)
  base.extend ClassMethods
  base.instance_eval do
    bootstrap_indexes
  end
end

Private Instance Methods

add_to_indexes(redis) click to toggle source
# File lib/modis/index.rb, line 60
def add_to_indexes(redis)
  return if indexed_attributes.empty?

  indexed_attributes.each do |attribute|
    key = index_key(attribute, read_attribute(attribute))
    redis.sadd(key, id)
  end
end
index_key(attribute, value) click to toggle source
# File lib/modis/index.rb, line 56
def index_key(attribute, value)
  self.class.index_key(attribute, value)
end
indexed_attributes() click to toggle source
# File lib/modis/index.rb, line 52
def indexed_attributes
  self.class.indexed_attributes
end
remove_from_indexes(redis) click to toggle source
# File lib/modis/index.rb, line 69
def remove_from_indexes(redis)
  return if indexed_attributes.empty?

  indexed_attributes.each do |attribute|
    key = index_key(attribute, read_attribute(attribute))
    redis.srem(key, id)
  end
end
update_indexes(redis) click to toggle source
# File lib/modis/index.rb, line 78
def update_indexes(redis)
  return if indexed_attributes.empty?

  (changes.keys & indexed_attributes).each do |attribute|
    old_value, new_value = changes[attribute]
    old_key = index_key(attribute, old_value)
    new_key = index_key(attribute, new_value)
    redis.smove(old_key, new_key, id)
  end
end