Skip to content

Commit

Permalink
Fix memory leak for ruby 3.2 (#882)
Browse files Browse the repository at this point in the history
* Fix memory leak for ruby 3.2

* Add changelog

* Update documentation
  • Loading branch information
konalegi authored Apr 20, 2023
1 parent 1e9a1e0 commit 827591d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bugs Fixed

* [#874](https://github.com/toptal/chewy/pull/874): Fix `chewy:journal:clean` task for ruby 3.x. ([@muk-ai](https://github.com/muk-ai))
* [#882](https://github.com/toptal/chewy/pull/882): Fix memory leak during `chewy:reset` for ruby 3.2 ([@konalegi](https://github.com/konalegi))

## 7.3.0 (2023-04-03)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ class ProductsIndex < Chewy::Index
field :name
# simply use crutch-fetched data as a value:
field :category_names, value: ->(product, crutches) { crutches.categories[product.id] }
field :category_names, value: ->(product, crutches) { crutches[:categories][product.id] }
end
```

Expand Down
22 changes: 15 additions & 7 deletions lib/chewy/index/crutch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ class Crutches
def initialize(index, collection)
@index = index
@collection = collection
@index._crutches.each_key do |name|
singleton_class.class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{name}
@#{name} ||= @index._crutches[:#{name}].call @collection
end
METHOD
end
@crutches_instances = {}
end

def method_missing(name, *, **)
return self[name] if @index._crutches.key?(name)

super
end

def respond_to_missing?(name, include_private = false)
@index._crutches.key?(name) || super
end

def [](name)
@crutches_instances[name] ||= @index._crutches[:"#{name}"].call(@collection)
end
end

Expand Down

0 comments on commit 827591d

Please sign in to comment.