Skip to content
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

Ruby 3 support #93

Closed
Closed
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- ruby: 2.4.1
- ruby: 2.5.1
- ruby: 2.6.0
- ruby: 2.7.5
- ruby: 3.0.3
- ruby: 3.1.0
- ruby: ruby-head
- ruby: jruby-d19
- ruby: jruby-9.1.9.0
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.bundle
.config
.yardoc
.ruby-version
Gemfile.lock
InstalledFiles
_yardoc
Expand Down
16 changes: 8 additions & 8 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 @@ -203,21 +203,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
29 changes: 28 additions & 1 deletion test/memoist_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ 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

protected

def memoize_protected_test
Expand Down Expand Up @@ -271,6 +281,23 @@ 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_memoization_with_punctuation
assert_equal true, @person.name?

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

expected = %w[age age? is_developer? memoize_protected_test name name? sleep update update_attributes]
expected = %w[age age? do_with_special 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