Skip to content

Commit ddf5ca7

Browse files
committed
Fix module's .included not run sometimes
Fixes #386
1 parent 16c7c4b commit ddf5ca7

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
77

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

10-
**Gem enhancements:** none
10+
**Gem enhancements:**
11+
12+
- Prevented overwriting of `included` in modules prepending `MemoWise` [[#387]](https://github.com/panorama-ed/memo_wise/pull/387))
1113

1214
_No breaking changes!_
1315

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ Results using Ruby 3.4.3:
118118

119119
|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)|
120120
|--|--|--|--|--|--|
121-
|`()` (none)|12.27x|0.58x|3.36x|2.79x|18.43x|
122-
|`(a)`|9.50x|0.99x|3.79x|14.93x|14.16x|
123-
|`(a, b)`|7.51x|0.83x|2.96x|11.84x|11.19x|
124-
|`(a:)`|14.32x|1.00x|6.46x|19.60x|12.81x|
125-
|`(a:, b:)`|12.24x|0.86x|5.56x|20.89x|10.78x|
126-
|`(a, b:)`|11.98x|0.85x|5.42x|16.25x|10.76x|
127-
|`(a, *args)`|1.91x|0.66x|0.75x|2.96x|2.80x|
128-
|`(a:, **kwargs)`|2.73x|0.71x|1.22x|4.69x|2.41x|
129-
|`(a, *args, b:, **kwargs)`|1.76x|0.63x|0.84x|3.00x|1.52x|
121+
|`()` (none)|12.20x|0.57x|3.31x|2.76x|18.45x|
122+
|`(a)`|9.75x|0.98x|3.76x|14.54x|13.96x|
123+
|`(a, b)`|7.59x|0.82x|2.92x|11.39x|10.87x|
124+
|`(a:)`|14.89x|0.97x|6.39x|19.76x|12.60x|
125+
|`(a:, b:)`|12.64x|0.86x|5.43x|21.05x|10.70x|
126+
|`(a, b:)`|12.25x|0.84x|5.22x|16.12x|10.31x|
127+
|`(a, *args)`|1.89x|0.65x|0.73x|2.84x|2.70x|
128+
|`(a:, **kwargs)`|2.86x|0.71x|1.21x|4.79x|2.42x|
129+
|`(a, *args, b:, **kwargs)`|1.81x|0.62x|0.83x|3.03x|1.52x|
130130

131131
\* `dry-core`
132132
[may cause incorrect behavior caused by hash collisions](https://github.com/dry-rb/dry-core/issues/63).

lib/memo_wise.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def inherited(subclass)
6767
module CreateMemoWiseStateOnIncluded
6868
def included(base)
6969
base.prepend(MemoWise)
70+
super
7071
end
7172
end
7273
private_constant(:CreateMemoWiseStateOnIncluded)

spec/memo_wise_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,43 @@ def child_class_method
483483
expect(instance.child_class_method_counter).to eq(1)
484484
end
485485
end
486+
487+
context "when a module prepends MemoWise and defines `.included`" do
488+
let(:module_to_include) do
489+
Module.new do
490+
prepend MemoWise
491+
492+
def self.included(base)
493+
base.class_eval do
494+
@internal_state = true
495+
end
496+
end
497+
498+
def method1
499+
self.class.internal_state
500+
end
501+
memo_wise :method1
502+
end
503+
end
504+
505+
let(:klass) do
506+
Class.new do
507+
include ModuleToInclude
508+
509+
def self.internal_state
510+
@internal_state ||= false
511+
end
512+
end
513+
end
514+
515+
let(:instance) { klass.new }
516+
517+
before(:each) { stub_const("ModuleToInclude", module_to_include) }
518+
519+
it "calls module `.included` method" do
520+
expect(instance.send(:method1)).to be true
521+
end
522+
end
486523
end
487524

488525
context "with class methods" do

0 commit comments

Comments
 (0)