-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Callbacks proxy #35
Callbacks proxy #35
Changes from 8 commits
b3324cd
74eac42
b1b1f7d
2b97dda
1156ca8
44ac840
d74c12e
59fb224
99f36ce
b303034
afd6a29
ba34b54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,76 +2,82 @@ module Kredis::Attributes | |
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def kredis_proxy(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_proxy(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_string(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_string(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_integer(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_integer(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_decimal(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_decimal(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_datetime(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_datetime(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_flag(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_flag(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
|
||
define_method("#{name}?") do | ||
send(name).marked? | ||
end | ||
end | ||
|
||
def kredis_enum(name, key: nil, values:, default:, config: :shared) | ||
kredis_connection_with __method__, name, key, values: values, default: default, config: config | ||
def kredis_enum(name, key: nil, values:, default:, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, values: values, default: default, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_json(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_json(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_list(name, key: nil, typed: :string, config: :shared) | ||
kredis_connection_with __method__, name, key, typed: typed, config: config | ||
def kredis_list(name, key: nil, typed: :string, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, typed: typed, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_unique_list(name, limit: nil, key: nil, typed: :string, config: :shared) | ||
kredis_connection_with __method__, name, key, limit: limit, typed: typed, config: config | ||
def kredis_unique_list(name, limit: nil, key: nil, typed: :string, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, limit: limit, typed: typed, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_set(name, key: nil, typed: :string, config: :shared) | ||
kredis_connection_with __method__, name, key, typed: typed, config: config | ||
def kredis_set(name, key: nil, typed: :string, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, typed: typed, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_slot(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_slot(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_slots(name, available:, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, available: available, config: config | ||
def kredis_slots(name, available:, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, available: available, config: config, after_change: after_change | ||
end | ||
|
||
def kredis_counter(name, key: nil, config: :shared) | ||
kredis_connection_with __method__, name, key, config: config | ||
def kredis_counter(name, key: nil, config: :shared, after_change: nil) | ||
kredis_connection_with __method__, name, key, config: config, after_change: after_change | ||
end | ||
|
||
private | ||
def kredis_connection_with(method, name, key, **options) | ||
ivar_symbol = :"@#{name}_#{method}" | ||
type = method.to_s.sub("kredis_", "") | ||
callback = options.delete(:after_change) | ||
|
||
define_method(name) do | ||
if instance_variable_defined?(ivar_symbol) | ||
instance_variable_get(ivar_symbol) | ||
else | ||
instance_variable_set(ivar_symbol, Kredis.send(type, kredis_key_evaluated(key) || kredis_key_for_attribute(name), **options)) | ||
instance_variable_set(ivar_symbol, | ||
Kredis::CallbacksProxy.new( | ||
Kredis.send(type, kredis_key_evaluated(key) || kredis_key_for_attribute(name), **options), | ||
self, | ||
callback) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think assigning the proxy to an explaining variable could help with the multi-line nature here. |
||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Kredis::CallbacksProxy | ||
attr_reader :type | ||
delegate :to_s, to: :type | ||
|
||
def initialize(type, record, callback) | ||
@type, @record, @callback = type, record, callback | ||
end | ||
|
||
def method_missing(method, *args, **kwargs, &block) | ||
result = @type.send(method, *args, **kwargs, &block) | ||
|
||
if @type.callback_operations&.include? method | ||
if @callback.respond_to? :call | ||
@callback.call(@record, @type) | ||
elsif @callback.is_a? Symbol | ||
@record.send(@callback, @record, @type) | ||
end | ||
end | ||
|
||
result | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ class Kredis::Types::Counter < Kredis::Types::Proxying | |
|
||
attr_accessor :expires_in | ||
|
||
def callback_operations | ||
%i[increment decrement reset].freeze | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like a configuration-level method that shouldn't be exposed directly through the API. What about using a constant instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then you also don't have a to call freeze. And for style, space after opening and before closing 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm too used to |
||
|
||
def increment(by: 1) | ||
multi do | ||
set 0, ex: expires_in, nx: true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think the instance method callback should probably take a parameter, yeah? Like they don't in Active Record/Model. Only the procs do that.