7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/bipolar_cache/sequel/plugin_alpha.rb', line 7
def bipolar_count_cache(name, **opts)
opts = opts.merge({ name: name })
method_name = opts[:method] || "#{name}_count"
define_method method_name do
bcsp_cache(**fetch_or_build_procs(**opts))
end
define_method "#{method_name}_refresh!" do
procs = fetch_or_build_procs(**opts)
actual_value = procs[:actual].call
procs[:update].call(actual_value) if actual_value != procs[:cached].call
actual_value
end
define_method "#{method_name}_increment!" do |by: 1|
procs = fetch_or_build_procs(**opts)
procs[:update].call(by + procs[:cached].call)
end
define_method "#{method_name}_decrement!" do |by: 1|
procs = fetch_or_build_procs(**opts)
procs[:update].call((-by) + procs[:cached].call)
end
end
|