memoizer 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/memoizer.rb +8 -6
- data/lib/memoizer/version.rb +1 -1
- data/spec/spec_helper.rb +6 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f15c5729b1a9844e6c5f4e75ce3994170d7544c
|
4
|
+
data.tar.gz: 707e892931dc2ebbef87efc266ee66347bba37e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c34f13ce47f80cd588e7acc06f38207fb2502689be3986b99af60ea8a20dc4e3ca353e81562a3392b3b96667f685ed51fec04d08a0f3345bf28cf2de38a9bc4a
|
7
|
+
data.tar.gz: e6cdf1930961c701e41c7ef7bd8705941aaeb60e49a883f7892a8a7d9e70151ec6f0ca0fe555c862cae1210982f694144547d91d9c85896976c95d691ffa51b7
|
data/lib/memoizer.rb
CHANGED
@@ -9,7 +9,7 @@ module Memoizer
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.ivar_name(method_name)
|
12
|
-
"_memoized_#{self.safe_name(method_name)}"
|
12
|
+
:"@_memoized_#{self.safe_name(method_name)}"
|
13
13
|
end
|
14
14
|
|
15
15
|
module ClassMethods
|
@@ -18,19 +18,21 @@ module Memoizer
|
|
18
18
|
memoized_ivar_name = Memoizer.ivar_name(method_name)
|
19
19
|
unmemoized_method = "_unmemoized_#{method_name}"
|
20
20
|
|
21
|
-
attr_accessor memoized_ivar_name
|
22
21
|
alias_method unmemoized_method, method_name
|
23
22
|
|
24
23
|
no_args = self.instance_method(unmemoized_method).arity == 0
|
25
24
|
|
26
25
|
define_method method_name do |*args|
|
27
|
-
|
26
|
+
|
27
|
+
if instance_variable_defined?(memoized_ivar_name)
|
28
|
+
memoized_value = self.instance_variable_get(memoized_ivar_name)
|
29
|
+
end
|
28
30
|
|
29
31
|
# if the method takes no inputs, store the value in an array
|
30
32
|
if no_args
|
31
33
|
if !memoized_value.is_a?(Array)
|
32
34
|
memoized_value = [self.send(unmemoized_method)]
|
33
|
-
self.instance_variable_set(
|
35
|
+
self.instance_variable_set(memoized_ivar_name, memoized_value)
|
34
36
|
end
|
35
37
|
memoized_value.first
|
36
38
|
|
@@ -38,7 +40,7 @@ module Memoizer
|
|
38
40
|
else
|
39
41
|
if !memoized_value.is_a?(Hash)
|
40
42
|
memoized_value = {args => self.send(unmemoized_method, *args)}
|
41
|
-
self.instance_variable_set(
|
43
|
+
self.instance_variable_set(memoized_ivar_name, memoized_value)
|
42
44
|
elsif !memoized_value.has_key?(args)
|
43
45
|
memoized_value[args] = self.send(unmemoized_method, *args)
|
44
46
|
end
|
@@ -57,7 +59,7 @@ module Memoizer
|
|
57
59
|
|
58
60
|
module InstanceMethods
|
59
61
|
def unmemoize(method_name)
|
60
|
-
self.instance_variable_set(
|
62
|
+
self.instance_variable_set(Memoizer.ivar_name(method_name), nil)
|
61
63
|
end
|
62
64
|
|
63
65
|
def unmemoize_all
|
data/lib/memoizer/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memoizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Barun Singh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: 1.3.6
|
92
92
|
requirements: []
|
93
93
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
94
|
+
rubygems_version: 2.6.11
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Memoizes (caches the results of) your methods
|