Skip to content

Commit

Permalink
Fix warnings from Ruby 2.7
Browse files Browse the repository at this point in the history
Add specs for it.
  • Loading branch information
AlexWayfer committed Jan 24, 2020
1 parent 202dd81 commit 559ef78
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,33 @@ def prepend_memery_module!
end

def define_memoized_method!(method_name, condition: nil, ttl: nil)
mod_id = @_memery_module.object_id
visibility = Memery.method_visibility(self, method_name)
raise ArgumentError, "Method #{method_name} is not defined on #{self}" unless visibility

@_memery_module.module_eval do
define_method(method_name) do |*args, &block|
if block || (condition && !instance_exec(&condition))
return super(*args, &block)
end
method_key = "#{method_name}_#{@_memery_module.object_id}"

key = "#{method_name}_#{mod_id}"
# Change to regular call of `define_method` after Ruby 2.4 drop
@_memery_module.send :define_method, method_name, (lambda do |*args, **kwargs, &block|
if block || (condition && !instance_exec(&condition))
return kwargs.any? ? super(*args, **kwargs, &block) : super(*args, &block)
end

store = (@_memery_memoized_values ||= {})[key] ||= {}
args_key = [args, kwargs]

if store.key?(args) && (ttl.nil? || Memery.monotonic_clock <= store[args][:time] + ttl)
return store[args][:result]
end
store = (@_memery_memoized_values ||= {})[method_key] ||= {}

result = super(*args)
@_memery_memoized_values[key][args] = { result: result, time: Memery.monotonic_clock }
result
if store.key?(args_key) &&
(ttl.nil? || Memery.monotonic_clock <= store[args_key][:time] + ttl)
return store[args_key][:result]
end

send(visibility, method_name)
end
result = kwargs.any? ? super(*args, **kwargs) : super(*args)
@_memery_memoized_values[method_key][args_key] =
{ result: result, time: Memery.monotonic_clock }
result
end)

@_memery_module.send(visibility, method_name)
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/memery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def not_memoized; end
[x, y]
end

memoize def m_kwargs(x, y: 42)
CALLS << [x, y]
[x, y]
end

memoize def m_double_splat(x, **kwargs)
CALLS << [x, kwargs]
[x, kwargs]
end

def m_condition
CALLS << __method__
__method__
Expand Down Expand Up @@ -141,6 +151,22 @@ def m; end
end
end

context "method with keyword args" do
specify do
values = [ a.m_kwargs(1, y: 2), a.m_kwargs(1, y: 2), a.m_kwargs(1, y: 3) ]
expect(values).to eq([[1, 2], [1, 2], [1, 3]])
expect(CALLS).to eq([[1, 2], [1, 3]])
end
end

context "method with double splat argument" do
specify do
values = [ a.m_double_splat(1, y: 2), a.m_double_splat(1, y: 2), a.m_double_splat(1, y: 3) ]
expect(values).to eq([[1, { y: 2 }], [1, { y: 2 }], [1, { y: 3 }]])
expect(CALLS).to eq([[1, { y: 2 }], [1, { y: 3 }]])
end
end

context "calling method with block" do
specify do
values = []
Expand Down

0 comments on commit 559ef78

Please sign in to comment.