From bb6ce632da81d515d4270e893e83708dedf55ec3 Mon Sep 17 00:00:00 2001 From: Sergey Kucher Date: Thu, 22 Oct 2015 15:40:49 +0500 Subject: [PATCH] add field 'single' and processor 'counter' pc4-15459 --- lib/treasury/fields/single.rb | 16 +++++++++++++ lib/treasury/processors/counter.rb | 37 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/treasury/fields/single.rb create mode 100644 lib/treasury/processors/counter.rb diff --git a/lib/treasury/fields/single.rb b/lib/treasury/fields/single.rb new file mode 100644 index 0000000..760b7ea --- /dev/null +++ b/lib/treasury/fields/single.rb @@ -0,0 +1,16 @@ +module Treasury + module Fields + module Single + extend ActiveSupport::Concern + + module ClassMethods + protected + + def init_accessor(params) + @accessing_object = extract_object(params) + @accessing_field = nil + end + end + end + end +end diff --git a/lib/treasury/processors/counter.rb b/lib/treasury/processors/counter.rb new file mode 100644 index 0000000..07ab12e --- /dev/null +++ b/lib/treasury/processors/counter.rb @@ -0,0 +1,37 @@ +module Treasury + module Processors + module Counter + include Single + + protected + + def process_insert + increment_current_value if satisfied? + end + + def process_update + if satisfied? + increment_current_value unless was_counted? + else + decrement_current_value if was_counted? + end + end + + def process_delete + decrement_current_value if was_counted? + end + + def satisfied? + condition? @event.raw_data + end + + def was_counted? + condition? @event.raw_prev_data + end + + def condition?(data) + raise NotImplemenedError + end + end + end +end \ No newline at end of file