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

allow memoize to take multiple method names #37

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ def included(base = nil, &block)
extend ModuleMethods

module ClassMethods
def memoize(method_name, condition: nil, ttl: nil)
def memoize(*method_names, condition: nil, ttl: nil)
prepend_memery_module!
define_memoized_method!(method_name, condition: condition, ttl: ttl)
method_name
method_names.each do |method_name|
define_memoized_method!(method_name, condition: condition, ttl: ttl)
end
method_names.length > 1 ? method_names : method_names.first
nevinera marked this conversation as resolved.
Show resolved Hide resolved
end

def memoized?(method_name)
Expand Down
68 changes: 68 additions & 0 deletions spec/memery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,39 @@ def self.macro(name)
macro memoize def g; end
end

class H
include Memery

[:a, :b, :m, :n, :x, :y].each do |name|
define_method(name) do
CALLS << name
name
end
end

memoize :m, :n
memoize :x, :y, ttl: 3
end

RSpec.describe Memery do
subject(:a) { A.new }

before { CALLS.clear }
before { B_CALLS.clear }

let(:unmemoized_class) do
Class.new do
include Memery

[:a, :b, :m, :n, :x, :y].each do |name|
define_method(name) do
CALLS << name
name
end
end
end
end

context "methods without args" do
specify do
values = [ a.m, a.m_nil, a.m, a.m_nil ]
Expand Down Expand Up @@ -366,6 +393,47 @@ def self.macro(name)
end
end

describe "with multiple methods" do
let(:h) { H.new }

specify do
values = [h.m, h.n, h.m, h.n]
expect(values).to eq([:m, :n, :m, :n])
expect(CALLS).to eq([:m, :n])
end

specify do
values = [h.x, h.y, h.x, h.y]
expect(values).to eq([:x, :y, :x, :y])
expect(CALLS).to eq([:x, :y])

allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC)
tycooon marked this conversation as resolved.
Show resolved Hide resolved
.and_wrap_original { |m, *args| m.call(*args) + 5 }

later_values = [h.x, h.x, h.y, h.x, h.y, h.y]
expect(later_values).to eq([:x, :x, :y, :x, :y, :y])
expect(CALLS).to eq([:x, :y, :x, :y])
end

specify do
expect(unmemoized_class.memoize(:x, :y, ttl: 3)).to eq([:x, :y])
end
end

describe ".memoize return value" do
specify do
expect(unmemoized_class.memoize(:x)).to eq(:x)
expect(unmemoized_class.memoize(:m, ttl: 3)).to eq(:m)
expect(unmemoized_class.memoize(:a, condition: -> { 1 == 2 })).to eq(:a)
end

specify do
expect(unmemoized_class.memoize(:x, :y)).to eq([:x, :y])
expect(unmemoized_class.memoize(:m, :n, ttl: 3)).to eq([:m, :n])
expect(unmemoized_class.memoize(:a, :b, condition: -> { 1 == 2 })).to eq([:a, :b])
end
end

describe ".memoized?" do
subject { object.memoized?(method_name) }

Expand Down