class Capacitor::Updater

Attributes

count_delta[R]
counter_id[R]
field[R]
id[R]
model[R]

Public Class Methods

new(counter_id, count_delta) click to toggle source
# File lib/capacitor/updater.rb, line 5
def initialize(counter_id, count_delta)
  @count_delta = count_delta.to_i
  @counter_id = counter_id
  @model, @id, @field = self.class.parse_counter_id(counter_id)
end
parse_counter_id(counter_id) click to toggle source

Internal: Expect a counter_id in the form: classname:object_id:field_name

Returns: model, object_id, :field

# File lib/capacitor/updater.rb, line 32
def self.parse_counter_id(counter_id)
  classname, object_id, field_name = counter_id.split(':')
  [classname.constantize, object_id.to_i, field_name.to_sym]
end

Public Instance Methods

inspect() click to toggle source

Public: Returns a string of useful debug info

# File lib/capacitor/updater.rb, line 25
def inspect
  "counter_id=#{counter_id} old_count=#{old_count} count_delta=#{count_delta}"
end
old_count() click to toggle source

Public: Returns the counter value from the database

# File lib/capacitor/updater.rb, line 20
def old_count
  model.find(id)[field]
end
update() click to toggle source

Public: Updates the counter with the new count delta

If count_delta is zero, does nothing

# File lib/capacitor/updater.rb, line 14
def update
  return if count_delta.zero?
  model.update_counters(id, field => count_delta)
end