class Counter::Cache::Counters::BufferCounter::Saver

Public Instance Methods

save!() { |old_value, new_value, relation_object, column| ... } click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 6
def save!
  return unless relation_object

  old_value = current_column_value
  new_value = calculate_new_value

  save_new_value!(new_value)

  yield old_value, new_value, relation_object, options.column if block_given?
end

Private Instance Methods

calculate_new_value() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 25
def calculate_new_value
  return current_column_value + counter_value.to_i if options.cached? && counter_value
  non_cached_count.to_i
end
constantized_relation() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 64
def constantized_relation
  Object.const_get(options.relation_class_name)
end
counter_value() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 40
def counter_value
  @counter_value ||= get.tap { |v| reset if v }
end
counting_data_store() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 56
def counting_data_store
  Counter::Cache.configuration.counting_data_store
end
current_column_value() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 36
def current_column_value
  relation_object.send(options.column).to_i
end
get() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 44
def get
  counting_data_store.get(key)
end
key() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 52
def key
  Key.new(nil, options).to_s
end
non_cached_count() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 30
def non_cached_count
  method = options.method
  return relation_object.send(method) if method && relation_object.respond_to?(method)
  relation_object.send(options.source_object_class_name.table_name).count
end
relation_object() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 60
def relation_object
  @relation_object ||= constantized_relation.find_by_id(options.relation_id)
end
reset() click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 48
def reset
  counting_data_store.del(key)
end
save_new_value!(value) click to toggle source
# File lib/counter/cache/counters/buffer_counter/saver.rb, line 19
def save_new_value!(value)
  relation_object.send("#{options.column}=", value)
  relation_object.send("#{options.touch_column}=", DateTime.now) if options.touch_column
  relation_object.save!(validate: false)
end