Skip to content

Commit

Permalink
Fix compatibility with AS Concern (#26)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tycooon and AlexWayfer authored Mar 26, 2020
1 parent 3fd5e54 commit d956418
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [Unreleased]

### Fixed
- Fix compatibility with `ActiveSupport::Concern`. ([@tycooon] and [@AlexWayfer]) [#26]

## [1.3.0] - 2020-02-10
### Added
- Allow memoization after including module with Memery. ([@AlexWayfer]) [#23]
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.4.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
ast (2.4.0)
benchmark-ips (2.7.2)
benchmark-memory (0.1.2)
memory_profiler (~> 0.9)
coderay (1.1.2)
concurrent-ruby (1.1.6)
coveralls (0.8.23)
json (>= 1.8, < 3)
simplecov (~> 0.16.1)
Expand All @@ -20,10 +26,13 @@ GEM
tins (~> 1.6)
diff-lcs (1.3)
docile (1.3.2)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
json (2.3.0)
memory_profiler (0.9.14)
method_source (0.9.2)
minitest (5.14.0)
parallel (1.19.1)
parser (2.7.0.2)
ast (~> 2.4.0)
Expand Down Expand Up @@ -76,14 +85,18 @@ GEM
term-ansicolor (1.7.1)
tins (~> 1.0)
thor (1.0.1)
thread_safe (0.3.6)
tins (1.24.0)
sync
tzinfo (1.2.6)
thread_safe (~> 0.1)
unicode-display_width (1.6.1)

PLATFORMS
ruby

DEPENDENCIES
activesupport (~> 5.0)
benchmark-ips
benchmark-memory
bundler
Expand Down
27 changes: 19 additions & 8 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ def monotonic_clock
end
end

OUR_BLOCK = lambda do
extend(ClassMethods)
include(InstanceMethods)
extend ModuleMethods if instance_of?(Module)
end

private_constant :OUR_BLOCK

module ModuleMethods
def included(base)
base.extend(ClassMethods)
base.include(InstanceMethods)
base.extend ModuleMethods if base.instance_of?(Module)
def included(base = nil, &block)
if base.nil? && block
super do
instance_exec(&block)
instance_exec(&OUR_BLOCK)
end
else
base.instance_exec(&OUR_BLOCK)
end
end
end

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

@_memery_module.method_defined?(method_name) ||
@_memery_module.private_method_defined?(method_name)
@_memery_module.private_method_defined?(method_name)
end

private

def prepend_memery_module!
return if defined?(@_memery_module)
@_memery_module = Module.new do
extend MemoizationModule
end
@_memery_module = Module.new { extend MemoizationModule }
prepend @_memery_module
end

Expand Down
1 change: 1 addition & 0 deletions memery.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|

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

spec.add_development_dependency "activesupport", "~> 5.0"
spec.add_development_dependency "benchmark-ips"
spec.add_development_dependency "benchmark-memory"
spec.add_development_dependency "bundler"
Expand Down
22 changes: 22 additions & 0 deletions spec/memery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,28 @@ def self.macro(name)
end
end

context "module with self.included method defined" do
subject(:c) { C.new }

before { C.include(some_mixin) }

let(:some_mixin) do
Module.new do
extend ActiveSupport::Concern
include Memery

included do
attr_accessor :a
end
end
end

it "doesn't override existing method" do
c.a = 15
expect(c.a).to eq(15)
end
end

context "class method with args" do
subject(:d) { D }

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
SimpleCov.start

require "memery"
require "active_support/concern"

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

0 comments on commit d956418

Please sign in to comment.