Class: Adocca::Thomas::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/thomas.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, corpus, word, amount) ⇒ Update

Returns a new instance of Update.



23
24
25
26
27
28
# File 'lib/thomas.rb', line 23

def initialize(model, corpus, word, amount)
  @model_name = model.name
  @corpus = corpus
  @word = word
  @amount = amount
end

Instance Method Details

#adjust(n) ⇒ Object



29
30
31
# File 'lib/thomas.rb', line 29

def adjust(n)
  @amount += n
end

#executeObject



32
33
34
35
36
37
38
39
# File 'lib/thomas.rb', line 32

def execute
	model = Module.const_get(@model_name)
  if model.connection.update(model.sanitizeSQL(["UPDATE #{model.table_name} SET amount = amount + ? WHERE corpus = ? AND word = ?",
                                                @amount, @corpus, @word])) == 0
    model.create(:corpus => @corpus, :word => @word, :amount => @amount)
  end
  model.expire_cached_namespace('CachedSupermodel:' + model.name + ':find_by_corpus_and_word:' + [@corpus, @word].inspect)
end