Skip to content

Commit

Permalink
Add .memoized? method
Browse files Browse the repository at this point in the history
To check (externally) if method memoized or not.
  • Loading branch information
AlexWayfer committed Sep 19, 2019
1 parent fd3f84f commit 0bf6f70
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/memery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def memoize(method_name, condition: nil, ttl: nil)
define_memoized_method!(method_name, condition: condition, ttl: ttl)
end

def memoized?(method_name)
return faslse unless defined?(@_memery_module)

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

private

def prepend_memery_module!
Expand Down
36 changes: 36 additions & 0 deletions spec/memery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class A
m_private
end

def not_memoized; end

memoize def m_nil
m_protected
end
Expand Down Expand Up @@ -254,4 +256,38 @@ class E
end
end
end

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

context "public memoized method" do
let(:method_name) { :m }

it { is_expected.to be true }
end

context "private memoized method" do
let(:method_name) { :m_private }

it { is_expected.to be true }
end

context "non-memoized method" do
let(:method_name) { :not_memoized }

it { is_expected.to be false }
end

context "standard class method" do
let(:method_name) { :constants }

it { is_expected.to be false }
end

context "standard instance method" do
let(:method_name) { :to_s }

it { is_expected.to be false }
end
end
end

0 comments on commit 0bf6f70

Please sign in to comment.