Module: FlagpoleSitta::ExistenceHash

Extended by:
ActiveSupport::Concern
Defined in:
lib/flagpole_sitta/existence_hash.rb

Overview

A ‘hash’ in the cache which you can use to check for the existence of an object

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#existence_hash_destory_updateObject



169
170
171
# File 'lib/flagpole_sitta/existence_hash.rb', line 169

def existence_hash_destory_update
  self.update_existence_hash(false)
end

#existence_hash_save_updateObject



165
166
167
# File 'lib/flagpole_sitta/existence_hash.rb', line 165

def existence_hash_save_update
  self.update_existence_hash(true)
end

#update_existence_hash(alive) ⇒ Object

Updates the ‘hash’ on save of any of its records.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/flagpole_sitta/existence_hash.rb', line 174

def update_existence_hash alive

  begin
    #Get the Current Class and the Old Class in case the object changed classes.
    #If its the base object, ie type is nil, then return class as the old_clazz.
    #If the object doesn't have the type field assume it can't change classes.
    new_clazz = self.has_attribute?('type') ? (self.type || self.class.to_s) : self.class.to_s
    old_clazz = self.has_attribute?('type') ? (self.type_was || self.class.to_s) : self.class.to_s
    superclazz = self.class.get_super_with_existence_hash

    #Old key is where it was, and new is where it is going.
    new_key = new_clazz.respond_to?(:constantize) ? self.send("#{new_clazz.constantize.route_id}") : nil
    old_key = old_clazz.respond_to?(:constantize) ? self.send("#{old_clazz.constantize.route_id}_was") : nil

    new_main_key = superclazz.eh_key_gen new_key, :class => new_clazz
    old_main_key = superclazz.eh_key_gen old_key, :class => old_clazz

    flag_key = superclazz.eh_key_gen "Flag"
    flag = FlagpoleSitta::CommonFs.flagpole_cache_read(flag_key)

    if flag.nil?
      flag = self.class.initialize_existence_hash
    end
    
    #If its a new record add it to the 'hash'
    if self.new_record?
      flag[:count] = flag[:count] + 1
      #if there are empty containers use them
      if flag[:empty] > -1
        #Find any empty container to use by popping it off of the top of the "stack".
        empty_key = superclazz.eh_key_gen flag[:empty], :emptystack => true
        i = FlagpoleSitta::CommonFs.flagpole_cache_read(empty_key)
        #Sense its going to be used remove its reference from the Stack.
        FlagpoleSitta::CommonFs.flagpole_cache_delete(empty_key)
        #Update the empty on flag to now hit the newest none used container on the stack.
        flag[:empty] = flag[:empty] - 1
      #Else add a space to the end.
      else
        #AR - update the array's end point
        flag[:space] = flag[:space] + 1
        i = flag[:space]
      end
      hash = {:type => new_clazz, :num => self.has_attribute?('num') ? (self.num || 0) : 0, :pos => i}
    #If its an already existing record them get its existence hash, and then remove it from the cache.
    else
      hash = FlagpoleSitta::CommonFs.flagpole_cache_read(old_main_key)
      hash[:type] = new_clazz
    end

    array_main_key = superclazz.eh_key_gen hash[:pos]

    #Before new info gets written make sure to delete all the old records just in case. The New location before it gets used too.
    FlagpoleSitta::CommonFs.flagpole_cache_delete(new_main_key)
    FlagpoleSitta::CommonFs.flagpole_cache_delete(old_main_key)
    FlagpoleSitta::CommonFs.flagpole_cache_delete(array_main_key)

    #If the record is not being destroyed add new route_id to existence hash
    if alive
      FlagpoleSitta::CommonFs.flagpole_cache_write(new_main_key, hash)
      FlagpoleSitta::CommonFs.flagpole_cache_write(array_main_key, {:type => new_clazz, :key => new_key})
    else
      if hash[:pos] == flag[:space]
        flag[:space] = flag[:space] - 1
      else
        flag[:empty] = flag[:empty] + 1
        empty_key = superclazz.eh_key_gen flag[:empty], :emptystack => true
        FlagpoleSitta::CommonFs.flagpole_cache_write(empty_key, hash[:pos])
      end
      flag[:count] = flag[:count] - 1
    end

    FlagpoleSitta::CommonFs.flagpole_cache_write(flag_key, flag)
  rescue Exception => e  
    #Keep ending up with one of the array objects having a key of nil. Despite the fact that it would have to at least start with /view
    #becuase of the way its set up in the helper. If that happens all bets are off and just clear everything.
    Rails.cache.clear
    Rails.logger.error("EXISTANCE HASH FAILURE CACHE IS BEING NUKED :: FLAGPOLE_SITTA")
    Rails.logger.error(e.message)
    e.backtrace.each do |b|
      Rails.logger.error("\t" + b.to_s)
    end
    puts "EXISTANCE HASH FAILURE CACHE IS BEING NUKED :: FLAGPOLE_SITTA"
    puts e.message
    e.backtrace.each do |b|
        puts "\t" + b.to_s
    end
  end

end