Skip to content

Commit

Permalink
Merge pull request #58 from dry-rb/deprecate-blocks-in-memoizable
Browse files Browse the repository at this point in the history
Add warning about memoization block-accepting methods
  • Loading branch information
flash-gordon authored Jul 8, 2021
2 parents 8f2a47b + 10854fc commit e42dcd8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/dry/core/memoizable.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "dry/core/deprecations"

module Dry
module Core
module Memoizable
@@ -80,6 +82,15 @@ def #{method.name}
mapping = parameters.to_h { |k, v = nil| [k, v] }
params, binds = declaration(parameters, mapping)

if parameters.last[0].eql?(:block)
Deprecations.warn(<<~WARN)
Memoization for block-accepting methods isn't safe.
Every call creates a new block instance bloating cached results.
In the future, blocks will still be allowed but won't participate in
cache key calculation.
WARN
end

module_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{method.name}(#{params.join(", ")})
key = [:"#{method.name}", #{binds.join(", ")}].hash

0 comments on commit e42dcd8

Please sign in to comment.