Skip to content

Commit d956418

Browse files
tycooonAlexWayfer
andauthored
Fix compatibility with AS Concern (#26)
* remove auto inclusion feature * return the AS test * Fix compatibility with `AS::Concern` (#27) Don't revert #23 * blank commit Co-authored-by: Alexander Popov <alex.wayfer@gmail.com>
1 parent 3fd5e54 commit d956418

6 files changed

+59
-8
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## [Unreleased]
22

3+
### Fixed
4+
- Fix compatibility with `ActiveSupport::Concern`. ([@tycooon] and [@AlexWayfer]) [#26]
5+
36
## [1.3.0] - 2020-02-10
47
### Added
58
- Allow memoization after including module with Memery. ([@AlexWayfer]) [#23]

Gemfile.lock

+13
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ PATH
77
GEM
88
remote: https://rubygems.org/
99
specs:
10+
activesupport (5.2.4.2)
11+
concurrent-ruby (~> 1.0, >= 1.0.2)
12+
i18n (>= 0.7, < 2)
13+
minitest (~> 5.1)
14+
tzinfo (~> 1.1)
1015
ast (2.4.0)
1116
benchmark-ips (2.7.2)
1217
benchmark-memory (0.1.2)
1318
memory_profiler (~> 0.9)
1419
coderay (1.1.2)
20+
concurrent-ruby (1.1.6)
1521
coveralls (0.8.23)
1622
json (>= 1.8, < 3)
1723
simplecov (~> 0.16.1)
@@ -20,10 +26,13 @@ GEM
2026
tins (~> 1.6)
2127
diff-lcs (1.3)
2228
docile (1.3.2)
29+
i18n (1.8.2)
30+
concurrent-ruby (~> 1.0)
2331
jaro_winkler (1.5.4)
2432
json (2.3.0)
2533
memory_profiler (0.9.14)
2634
method_source (0.9.2)
35+
minitest (5.14.0)
2736
parallel (1.19.1)
2837
parser (2.7.0.2)
2938
ast (~> 2.4.0)
@@ -76,14 +85,18 @@ GEM
7685
term-ansicolor (1.7.1)
7786
tins (~> 1.0)
7887
thor (1.0.1)
88+
thread_safe (0.3.6)
7989
tins (1.24.0)
8090
sync
91+
tzinfo (1.2.6)
92+
thread_safe (~> 0.1)
8193
unicode-display_width (1.6.1)
8294

8395
PLATFORMS
8496
ruby
8597

8698
DEPENDENCIES
99+
activesupport (~> 5.0)
87100
benchmark-ips
88101
benchmark-memory
89102
bundler

lib/memery.rb

+19-8
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,24 @@ def monotonic_clock
1111
end
1212
end
1313

14+
OUR_BLOCK = lambda do
15+
extend(ClassMethods)
16+
include(InstanceMethods)
17+
extend ModuleMethods if instance_of?(Module)
18+
end
19+
20+
private_constant :OUR_BLOCK
21+
1422
module ModuleMethods
15-
def included(base)
16-
base.extend(ClassMethods)
17-
base.include(InstanceMethods)
18-
base.extend ModuleMethods if base.instance_of?(Module)
23+
def included(base = nil, &block)
24+
if base.nil? && block
25+
super do
26+
instance_exec(&block)
27+
instance_exec(&OUR_BLOCK)
28+
end
29+
else
30+
base.instance_exec(&OUR_BLOCK)
31+
end
1932
end
2033
end
2134

@@ -32,16 +45,14 @@ def memoized?(method_name)
3245
return false unless defined?(@_memery_module)
3346

3447
@_memery_module.method_defined?(method_name) ||
35-
@_memery_module.private_method_defined?(method_name)
48+
@_memery_module.private_method_defined?(method_name)
3649
end
3750

3851
private
3952

4053
def prepend_memery_module!
4154
return if defined?(@_memery_module)
42-
@_memery_module = Module.new do
43-
extend MemoizationModule
44-
end
55+
@_memery_module = Module.new { extend MemoizationModule }
4556
prepend @_memery_module
4657
end
4758

memery.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
2222

2323
spec.add_runtime_dependency "ruby2_keywords", "~> 0.0.2"
2424

25+
spec.add_development_dependency "activesupport", "~> 5.0"
2526
spec.add_development_dependency "benchmark-ips"
2627
spec.add_development_dependency "benchmark-memory"
2728
spec.add_development_dependency "bundler"

spec/memery_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,28 @@ def self.macro(name)
260260
end
261261
end
262262

263+
context "module with self.included method defined" do
264+
subject(:c) { C.new }
265+
266+
before { C.include(some_mixin) }
267+
268+
let(:some_mixin) do
269+
Module.new do
270+
extend ActiveSupport::Concern
271+
include Memery
272+
273+
included do
274+
attr_accessor :a
275+
end
276+
end
277+
end
278+
279+
it "doesn't override existing method" do
280+
c.a = 15
281+
expect(c.a).to eq(15)
282+
end
283+
end
284+
263285
context "class method with args" do
264286
subject(:d) { D }
265287

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SimpleCov.start
1313

1414
require "memery"
15+
require "active_support/concern"
1516

1617
RSpec.configure do |config|
1718
# Enable flags like --only-failures and --next-failure

0 commit comments

Comments
 (0)