Module: Modis::Index::ClassMethods

Defined in:
lib/modis/index.rb

Instance Method Summary collapse

Instance Method Details

#bootstrap_indexes(parent = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/modis/index.rb', line 13

def bootstrap_indexes(parent = nil)
  class << self
    attr_accessor :indexed_attributes
  end

  self.indexed_attributes = parent ? parent.indexed_attributes.dup : []
end

#index(attribute) ⇒ Object

Raises:



21
22
23
24
25
# File 'lib/modis/index.rb', line 21

def index(attribute)
  attribute = attribute.to_s
  raise IndexError, "No such attribute '#{attribute}'" unless attributes.key?(attribute)
  indexed_attributes << attribute
end

#index_for(attribute, value) ⇒ Object



35
36
37
38
39
40
# File 'lib/modis/index.rb', line 35

def index_for(attribute, value)
  Modis.with_connection do |redis|
    key = index_key(attribute, value)
    redis.smembers(key).map(&:to_i)
  end
end

#index_key(attribute, value) ⇒ Object



42
43
44
# File 'lib/modis/index.rb', line 42

def index_key(attribute, value)
  "#{absolute_namespace}:index:#{attribute}:#{value.inspect}"
end

#where(query) ⇒ Object

Raises:



27
28
29
30
31
32
33
# File 'lib/modis/index.rb', line 27

def where(query)
  raise IndexError, 'Queries using multiple indexes is not currently supported.' if query.keys.size > 1
  attribute, value = query.first
  ids = index_for(attribute, value)
  return [] if ids.empty?
  find_all(ids)
end