From bde5abaf32f15845c730fd14e9b440f95ce1895a Mon Sep 17 00:00:00 2001 From: Jose Irizarry Date: Thu, 24 Jul 2014 14:50:14 -0400 Subject: [PATCH] add custom options --- README.md | 3 ++- .../cache/counters/buffer_counter/enqueuer.rb | 11 +++++++---- lib/counter/cache/options_parser.rb | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1846f8e..137ac8a 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ Or install it yourself as: Counter caches are configured on the models from the perspective of the child model to the parent that contains the counter. -#### Basic Counter with recalculation: +#### Basic Counter with recalculation and custom options: ```ruby class Post @@ -106,6 +106,7 @@ class Post relation: :user, relation_class_name: "User", method: :calculate_posts_count, # This is a method on the user. + custom_options: { email: ->(user) { user.email }, foo: "bar" } # custom_options hash available for further processing in Worker. end ``` diff --git a/lib/counter/cache/counters/buffer_counter/enqueuer.rb b/lib/counter/cache/counters/buffer_counter/enqueuer.rb index e80f611..830c901 100644 --- a/lib/counter/cache/counters/buffer_counter/enqueuer.rb +++ b/lib/counter/cache/counters/buffer_counter/enqueuer.rb @@ -4,13 +4,13 @@ module Counters class BufferCounter class Enqueuer < Struct.new(:options, :source_object_class_name, :relation_id, :relation_class, :counter_class_name) def enqueue!(source_object) - create_and_enqueue(options.wait(source_object), options.cached?) - create_and_enqueue(options.recalculation_delay, false) if options.recalculation? + create_and_enqueue(options.wait(source_object), options.cached?, source_object) + create_and_enqueue(options.recalculation_delay, false, source_object) if options.recalculation? end private - def create_and_enqueue(delay, cached) + def create_and_enqueue(delay, cached, source_object) options.worker_adapter.enqueue(delay, source_object_class_name, { relation_class_name: relation_class, @@ -18,7 +18,10 @@ def create_and_enqueue(delay, cached) column: options.column, method: options.method, cache: cached, - counter: counter_class_name }) + counter: counter_class_name, + custom_options: options.custom_options(source_object) + } + ) end end end diff --git a/lib/counter/cache/options_parser.rb b/lib/counter/cache/options_parser.rb index 49d50fa..ab8d187 100644 --- a/lib/counter/cache/options_parser.rb +++ b/lib/counter/cache/options_parser.rb @@ -54,6 +54,22 @@ def wait(source_object) end end + def custom_options(source_object) + if options[:custom_options] + custom_options = {} + options[:custom_options].each do |option_name, option_value| + if option_value.respond_to?(:call) + custom_options[option_name] = option_value.call(source_object) + else + custom_options[option_name] = option_value + end + end + return custom_options + else + nil + end + end + def recalculation_delay options[:recalculation_delay] || Counter::Cache.configuration.recalculation_delay end