Skip to content

Fix module's .included not run sometimes #387

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased](https://github.com/panorama-ed/memo_wise/compare/v1.12.0...HEAD)

**Gem enhancements:** none
**Gem enhancements:**

- Prevented overwriting of `included` in modules prepending `MemoWise` [[#387]](https://github.com/panorama-ed/memo_wise/pull/387))

_No breaking changes!_

Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ Results using Ruby 3.4.3:

|Method arguments|`alt_memery` (2.1.0)|`dry-core`\* (1.1.0)|`memery` (1.7.0)|`memoist3` (1.0.0)|`short_circu_it` (0.29.3)|
|--|--|--|--|--|--|
|`()` (none)|12.27x|0.58x|3.36x|2.79x|18.43x|
|`(a)`|9.50x|0.99x|3.79x|14.93x|14.16x|
|`(a, b)`|7.51x|0.83x|2.96x|11.84x|11.19x|
|`(a:)`|14.32x|1.00x|6.46x|19.60x|12.81x|
|`(a:, b:)`|12.24x|0.86x|5.56x|20.89x|10.78x|
|`(a, b:)`|11.98x|0.85x|5.42x|16.25x|10.76x|
|`(a, *args)`|1.91x|0.66x|0.75x|2.96x|2.80x|
|`(a:, **kwargs)`|2.73x|0.71x|1.22x|4.69x|2.41x|
|`(a, *args, b:, **kwargs)`|1.76x|0.63x|0.84x|3.00x|1.52x|
|`()` (none)|12.20x|0.57x|3.31x|2.76x|18.45x|
|`(a)`|9.75x|0.98x|3.76x|14.54x|13.96x|
|`(a, b)`|7.59x|0.82x|2.92x|11.39x|10.87x|
|`(a:)`|14.89x|0.97x|6.39x|19.76x|12.60x|
|`(a:, b:)`|12.64x|0.86x|5.43x|21.05x|10.70x|
|`(a, b:)`|12.25x|0.84x|5.22x|16.12x|10.31x|
|`(a, *args)`|1.89x|0.65x|0.73x|2.84x|2.70x|
|`(a:, **kwargs)`|2.86x|0.71x|1.21x|4.79x|2.42x|
|`(a, *args, b:, **kwargs)`|1.81x|0.62x|0.83x|3.03x|1.52x|

\* `dry-core`
[may cause incorrect behavior caused by hash collisions](https://github.com/dry-rb/dry-core/issues/63).
Expand Down
1 change: 1 addition & 0 deletions lib/memo_wise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def inherited(subclass)
module CreateMemoWiseStateOnIncluded
def included(base)
base.prepend(MemoWise)
super
end
end
private_constant(:CreateMemoWiseStateOnIncluded)
Expand Down
37 changes: 37 additions & 0 deletions spec/memo_wise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,43 @@ def child_class_method
expect(instance.child_class_method_counter).to eq(1)
end
end

context "when a module prepends MemoWise and defines `.included`" do
let(:module_to_include) do
Module.new do
prepend MemoWise

def self.included(base)
base.class_eval do
@internal_state = true
end
end

def method1
self.class.internal_state
end
memo_wise :method1
end
end

let(:klass) do
Class.new do
include ModuleToInclude

def self.internal_state
@internal_state ||= false
end
end
end

let(:instance) { klass.new }

before(:each) { stub_const("ModuleToInclude", module_to_include) }

it "calls module `.included` method" do
expect(instance.send(:method1)).to be true
end
end
end

context "with class methods" do
Expand Down