Skip to content
Open
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
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
push:
branches: [ master ]
pull_request:

jobs:
tests:
name: Tests

runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['3.0', '3.2']

steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler: 2.3
bundler-cache: true
env:
BUNDLE_GITHUB__COM: ${{ secrets.BUNDLE_GITHUB__COM }}:x-oauth-basic
- name: Run tests
run: bundle exec rake
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
Expand Down
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PATH
remote: .
specs:
memoist (0.16.0)

GEM
remote: https://rubygems.org/
specs:
benchmark-ips (2.11.0)
minitest (5.18.0)
rake (13.0.6)

PLATFORMS
x86_64-linux

DEPENDENCIES
benchmark-ips
bundler
memoist!
minitest (~> 5.10)
rake

BUNDLED WITH
2.4.3
21 changes: 11 additions & 10 deletions lib/memoist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def self.escape_punctuation(string)
string
end

def self.memoist_eval(klass, *args, &block)
def self.memoist_eval(klass, *args, **kwargs, &block)
if klass.respond_to?(:class_eval)
klass.class_eval(*args, &block)
klass.class_eval(*args, **kwargs, &block)
else
klass.singleton_class.class_eval(*args, &block)
klass.singleton_class.class_eval(*args, **kwargs, &block)
end
end

Expand Down Expand Up @@ -132,8 +132,9 @@ def memoize(*method_names)
include InstanceMethods

if method_defined?(unmemoized_method)
warn "Already memoized #{method_name}"
return
default = "AnIdentifier"
suggestion = respond_to?(:name) ? name || default : default
raise "Already memoized :#{method_name}. Try `memoize :#{method_name}, identifier: '#{suggestion}'` or use `@#{method_name} ||= compute` pattern instead."
end
alias_method unmemoized_method, method_name

Expand Down Expand Up @@ -203,21 +204,21 @@ def #{method_name}(reload = false)
# end

module_eval <<-EOS, __FILE__, __LINE__ + 1
def #{method_name}(*args)
def #{method_name}(*args, **kwargs)
reload = Memoist.extract_reload!(method(#{unmemoized_method.inspect}), args)

skip_cache = reload || !(instance_variable_defined?(#{memoized_ivar.inspect}) && #{memoized_ivar} && #{memoized_ivar}.has_key?(args))
skip_cache = reload || !(instance_variable_defined?(#{memoized_ivar.inspect}) && #{memoized_ivar} && #{memoized_ivar}.has_key?(args+kwargs.to_a))
set_cache = skip_cache && !frozen?

if skip_cache
value = #{unmemoized_method}(*args)
value = #{unmemoized_method}(*args, **kwargs)
else
value = #{memoized_ivar}[args]
value = #{memoized_ivar}[args+kwargs.to_a]
end

if set_cache
#{memoized_ivar} ||= {}
#{memoized_ivar}[args] = value
#{memoized_ivar}[args+kwargs.to_a] = value
end

value
Expand Down
82 changes: 81 additions & 1 deletion test/memoist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ def update_attributes_calls
@counter.count(:update_attributes)
end

def do_with_special(_regular = 10, special_one: true, special_two: true)
@counter.call(:do_with_special)
true
end
memoize :do_with_special

def do_with_special_calls
@counter.count(:do_with_special)
end

def format_metadata(metadata)
@counter.call(:format_metadata)
"#{metadata[:grade]}: #{metadata[:comment]}"
end
memoize :format_metadata

def format_metadata_calls
@counter.count(:format_metadata)
end

protected

def memoize_protected_test
Expand Down Expand Up @@ -265,6 +285,35 @@ def test_memoize_with_options_hash
assert_equal 4, @person.update_attributes_calls
end

def test_memoize_with_kwargs
assert_equal true, @person.do_with_special(1, special_one: true)
assert_equal 1, @person.do_with_special_calls

3.times { assert_equal true, @person.do_with_special(1, special_one: true) }
assert_equal 1, @person.do_with_special_calls

assert_equal true, @person.do_with_special(2)
assert_equal 2, @person.do_with_special_calls

assert_equal true, @person.do_with_special(1, special_one: false)
assert_equal 3, @person.do_with_special_calls

assert_equal true, @person.do_with_special(1, special_two: false)
assert_equal 4, @person.do_with_special_calls
end

def test_memoize_with_hash_for_ruby3
metadata = { grade: 42, comment: 'The meaning.' }
assert_equal '42: The meaning.', @person.format_metadata(metadata)
assert_equal 1, @person.format_metadata_calls

assert_equal '42: The meaning.', @person.format_metadata(metadata)
assert_equal 1, @person.format_metadata_calls

assert_equal '44: The name.', @person.format_metadata({ grade: 44, comment: 'The name.'})
assert_equal 2, @person.format_metadata_calls
end

def test_memoization_with_punctuation
assert_equal true, @person.name?

Expand Down Expand Up @@ -348,7 +397,7 @@ def test_all_memoized_structs
# Student < Person memoize :name, :identifier => :student
# Teacher < Person memoize :seniority

expected = %w[age is_developer? memoize_protected_test name name? sleep update update_attributes]
expected = %w[age do_with_special format_metadata is_developer? memoize_protected_test name name? sleep update update_attributes]
structs = Person.all_memoized_structs
assert_equal expected, structs.collect(&:memoized_method).collect(&:to_s).sort
assert_equal '@_memoized_name', structs.detect { |s| s.memoized_method == :name }.ivar
Expand Down Expand Up @@ -547,4 +596,35 @@ def test_private_method_memoization
assert_equal 'Yes', person.send(:is_developer?)
assert_equal 1, person.is_developer_calls
end

def test_exception_when_subclass_without_identifier
err = assert_raises(StandardError) do
Class.new(Person) do
def self.name
"Staff"
end
def name
'Overwritten'
end
memoize :name
end
end
assert_equal(
"Already memoized :name. Try `memoize :name, identifier: 'Staff'` or use `@name ||= compute` pattern instead.",
err.message
)

err = assert_raises(StandardError) do
Class.new(Person) do
def name
'Overwritten'
end
memoize :name
end
end
assert_equal(
"Already memoized :name. Try `memoize :name, identifier: 'AnIdentifier'` or use `@name ||= compute` pattern instead.",
err.message
)
end
end