Module: Chimera::Indexes::InstanceMethods

Defined in:
lib/chimera/indexes.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_to_all_indexObject



172
173
174
# File 'lib/chimera/indexes.rb', line 172

def add_to_all_index
  self.class.connection(:redis).lpush(self.class.key_for_all_index, self.id)
end

#check_index_constraintsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chimera/indexes.rb', line 113

def check_index_constraints
  self.class.defined_indexes.each do |name, props|
    case props[:type]
    when :unique then
      if val = @attributes[name.to_sym] and !val.blank?
        index_key = self.class.key_for_index(:unique, name,val.to_s)
        if k = self.class.connection(:redis).get(index_key)
          if k.to_s != self.id.to_s
            raise(Chimera::Error::UniqueConstraintViolation, val)
          end
        end
      end # if val
    end # case
  end
end

#create_indexesObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chimera/indexes.rb', line 129

def create_indexes
  add_to_all_index
  self.class.defined_indexes.each do |name, props|
    case props[:type]
    when :find then
      if val = @attributes[name.to_sym] and !val.blank?
        index_key = self.class.key_for_index(:find, name, val.to_s)
        self.class.connection(:redis).sadd(index_key, self.id)
      end
    when :unique then
      if val = @attributes[name.to_sym] and !val.blank?
        index_key = self.class.key_for_index(:unique, name,val.to_s)
        if self.class.connection(:redis).exists(index_key)
          raise(Chimera::Error::UniqueConstraintViolation, val)
        else
          self.class.connection(:redis).set(index_key, self.id)
        end
      end
    when :search then
      if val = @attributes[name.to_sym] and !val.blank?
        val.to_s.split(" ").each do |word|
          word = word.downcase.stem
          next if Chimera::Indexes::SEARCH_EXCLUDE_LIST.include?(word)
          index_key = self.class.key_for_index(:search, name, word)
          self.class.connection(:redis).sadd(index_key, self.id)
        end
      end
    end
  end
  create_geo_indexes
end

#destroy_indexesObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chimera/indexes.rb', line 85

def destroy_indexes
  remove_from_all_index
  self.class.defined_indexes.each do |name, props|
    case props[:type]
    when :find then
      if val = @orig_attributes[name.to_sym] and !val.blank?
        index_key = self.class.key_for_index(:find, name,val.to_s)
        self.class.connection(:redis).srem(index_key, self.id)
      end
    when :unique then
      if val = @orig_attributes[name.to_sym] and !val.blank?
        index_key = self.class.key_for_index(:unique, name,val.to_s)
        self.class.connection(:redis).del(index_key)
      end
    when :search then
      if val = @orig_attributes[name.to_sym] and !val.blank?
        val.to_s.split(" ").each do |word|
          word = word.downcase.stem
          next if Chimera::Indexes::SEARCH_EXCLUDE_LIST.include?(word)
          index_key = self.class.key_for_index(:search, name, word)
          self.class.connection(:redis).srem(index_key, self.id)
        end
      end
    end
  end
  destroy_geo_indexes
end

#remove_from_all_indexObject



168
169
170
# File 'lib/chimera/indexes.rb', line 168

def remove_from_all_index
  self.class.connection(:redis).lrem(self.class.key_for_all_index, 0, self.id)
end

#update_indexesObject



161
162
163
164
165
166
# File 'lib/chimera/indexes.rb', line 161

def update_indexes
  destroy_indexes
  destroy_geo_indexes
  create_indexes
  create_geo_indexes
end