Skip to content

Commit

Permalink
Reduce the code, remove rubocop:disable (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWayfer authored and tycooon committed Aug 29, 2019
1 parent d16064a commit fd3f84f
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def prepend_memery_module!
prepend @_memery_module
end

# rubocop:disable Metrics/MethodLength
def define_memoized_method!(method_name, condition: nil, ttl: nil)
mod_id = @_memery_module.object_id
visibility = Memery.method_visibility(self, method_name)
Expand All @@ -51,24 +50,22 @@ def define_memoized_method!(method_name, condition: nil, ttl: nil)
return super(*args, &block)
end

@_memery_memoized_values ||= {}
key = "#{method_name}_#{mod_id}"
@_memery_memoized_values[key] ||= {}
store = @_memery_memoized_values[key] || {}

store = (@_memery_memoized_values ||= {})[key] ||= {}

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

super(*args).tap do |result|
@_memery_memoized_values[key][args] = { result: result, time: Memery.monotonic_clock }
end
result = super(*args)
@_memery_memoized_values[key][args] = { result: result, time: Memery.monotonic_clock }
result
end

send(visibility, method_name)
end
end
# rubocop:enable Metrics/MethodLength
end

module InstanceMethods
Expand Down

0 comments on commit fd3f84f

Please sign in to comment.