memoizer 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +48 -38
- data/lib/memoizer.rb +0 -3
- data/lib/memoizer/version.rb +1 -1
- data/memoizer.gemspec +5 -1
- data/spec/memoizer_spec.rb +44 -44
- data/spec/spec_helper.rb +1 -0
- metadata +56 -16
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b010462b283416d36bc6de92c071b5010676f917
|
4
|
+
data.tar.gz: 77daaef50543351dff8ca7ceb1e41c5e520495f6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e3ee325a2fb4709776b15d990af5c6b9c0bbf56238764a96265830bdb76dc0eb3de62798a31e2ed0bb8bdc662bbf7a5a32a9517cac8222f9dea2662bf1e0844
|
7
|
+
data.tar.gz: d33e9225fe71f6c4785f71e4fb06c01fdf19e54887a52900883c3467c72cf86f267674b2fea30f754e7f20b6687b75fdd5a7dbd1c88afe6ba68a9883bf68baec
|
data/README.md
CHANGED
@@ -1,63 +1,73 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
[](https://travis-ci.org/wegowise/memoizer)
|
2
|
+
|
3
|
+
# memoizer
|
3
4
|
|
4
5
|
Memoizer will memoize the results of your methods. It acts much like
|
5
|
-
ActiveSupport::Memoizable without all of that freezing business. The API for
|
6
|
-
is also a bit more
|
6
|
+
`ActiveSupport::Memoizable` without all of that freezing business. The API for
|
7
|
+
unmemoizing is also a bit more explicit.
|
7
8
|
|
8
|
-
Install
|
9
|
-
=======
|
9
|
+
## Install
|
10
10
|
|
11
|
-
|
11
|
+
```
|
12
|
+
$ gem install memoizer
|
13
|
+
```
|
12
14
|
|
13
|
-
Usage
|
14
|
-
=====
|
15
|
+
## Usage
|
15
16
|
|
16
17
|
To memoize an instance method:
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
```ruby
|
20
|
+
class A
|
21
|
+
include Memoizer
|
22
|
+
def hello() 'hello!'; end
|
23
|
+
memoize :hello
|
24
|
+
end
|
25
|
+
```
|
23
26
|
|
24
27
|
Or you can memoize many methods at once:
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
```ruby
|
30
|
+
class B
|
31
|
+
extend Memoizer
|
32
|
+
def hello() 'hello!'; end
|
33
|
+
def goodbye() 'goodbye :('; end
|
34
|
+
memoize :hello, :goodbye
|
35
|
+
end
|
36
|
+
```
|
32
37
|
|
33
38
|
Memoizing class methods works the same way:
|
34
39
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
```ruby
|
41
|
+
class C
|
42
|
+
class << self
|
43
|
+
include Memoizer
|
44
|
+
def hello() 'hello!'; end
|
45
|
+
memoize :hello
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
42
49
|
|
43
50
|
|
44
51
|
To unmemoize a specific method:
|
45
52
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
53
|
+
```ruby
|
54
|
+
instance = A.new
|
55
|
+
instance.hello # the hello method is now memoized
|
56
|
+
instance.unmemoize(:hello) # the hello method is no longer memoized
|
57
|
+
instance.hello # the hello method is run again and re-memoized
|
58
|
+
```
|
50
59
|
|
51
60
|
|
52
61
|
To unmemoize all methods for an instance:
|
53
62
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
63
|
+
```ruby
|
64
|
+
instance = B.new
|
65
|
+
instance.hello # the hello method is now memoized
|
66
|
+
instance.goodbye # the goodbye method is now memoized
|
67
|
+
instance.unmemoize_all # neither hello nor goodbye are memoized anymore
|
68
|
+
```
|
58
69
|
|
59
70
|
|
60
|
-
License
|
61
|
-
=======
|
71
|
+
## License
|
62
72
|
|
63
|
-
See LICENSE.txt
|
73
|
+
See [LICENSE.txt](https://github.com/wegowise/memoizer/blob/master/LICENSE.txt)
|
data/lib/memoizer.rb
CHANGED
@@ -15,7 +15,6 @@ module Memoizer
|
|
15
15
|
module ClassMethods
|
16
16
|
def memoize(*method_names)
|
17
17
|
method_names.each do |method_name|
|
18
|
-
safe_method_name = Memoizer.safe_name(method_name)
|
19
18
|
memoized_ivar_name = Memoizer.ivar_name(method_name)
|
20
19
|
unmemoized_method = "_unmemoized_#{method_name}"
|
21
20
|
|
@@ -52,7 +51,6 @@ module Memoizer
|
|
52
51
|
elsif self.protected_method_defined?(unmemoized_method)
|
53
52
|
protected method_name
|
54
53
|
end
|
55
|
-
|
56
54
|
end
|
57
55
|
end
|
58
56
|
end
|
@@ -70,5 +68,4 @@ module Memoizer
|
|
70
68
|
end
|
71
69
|
end
|
72
70
|
end
|
73
|
-
|
74
71
|
end
|
data/lib/memoizer/version.rb
CHANGED
data/memoizer.gemspec
CHANGED
@@ -7,11 +7,15 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Memoizer::VERSION
|
8
8
|
s.authors = ["Barun Singh"]
|
9
9
|
s.email = "bsingh@wegowise.com"
|
10
|
-
s.homepage = "
|
10
|
+
s.homepage = "https://github.com/wegowise/memoizer"
|
11
11
|
s.summary = "Memoizes (caches the results of) your methods"
|
12
12
|
s.description = "Memoizer caches the results of your method calls, works well with methods that accept arguments or return nil. It's a simpler and more expicit alternative to ActiveSupport::Memoizable"
|
13
13
|
s.required_rubygems_version = ">= 1.3.6"
|
14
14
|
s.files = Dir.glob(%w[{lib,spec}/**/*.rb [A-Z]*.{txt,rdoc,md} *.gemspec]) + %w{Rakefile}
|
15
15
|
s.extra_rdoc_files = ["README.md", "LICENSE.txt"]
|
16
16
|
s.license = 'MIT'
|
17
|
+
|
18
|
+
s.add_development_dependency('rake', '~> 10.4.2')
|
19
|
+
s.add_development_dependency('rspec', '~> 3.5.0')
|
20
|
+
s.add_development_dependency('timecop', '~> 0.8.0')
|
17
21
|
end
|
data/spec/memoizer_spec.rb
CHANGED
@@ -21,34 +21,34 @@ describe Memoizer do
|
|
21
21
|
context "for a method with no params" do
|
22
22
|
it "stores memoized value" do
|
23
23
|
Timecop.freeze(today)
|
24
|
-
object.no_params.
|
24
|
+
expect(object.no_params).to eq(today)
|
25
25
|
Timecop.freeze(tomorrow)
|
26
|
-
object.no_params.
|
26
|
+
expect(object.no_params).to eq(today)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
context "for a method with params (and ending in ?)" do
|
31
31
|
it "stores memoized value" do
|
32
32
|
Timecop.freeze(today)
|
33
|
-
object.with_params?(1, [1,2]).
|
33
|
+
expect(object.with_params?(1, [1,2])).to eq(today + 3)
|
34
34
|
Timecop.freeze(tomorrow)
|
35
|
-
object.with_params?(1, [1,2]).
|
35
|
+
expect(object.with_params?(1, [1,2])).to eq(today + 3)
|
36
36
|
end
|
37
37
|
it "does not confuse one set of inputs for another" do
|
38
38
|
Timecop.freeze(today)
|
39
|
-
object.with_params?(1, [1,2]).
|
40
|
-
object.with_params?(2, [1,2]).
|
39
|
+
expect(object.with_params?(1, [1,2])).to eq(today + 3)
|
40
|
+
expect(object.with_params?(2, [1,2])).to eq(today + 4)
|
41
41
|
Timecop.freeze(tomorrow)
|
42
|
-
object.with_params?(1, [1,2]).
|
43
|
-
object.with_params?(1, [2,2]).
|
42
|
+
expect(object.with_params?(1, [1,2])).to eq(today + 3)
|
43
|
+
expect(object.with_params?(1, [2,2])).to eq(today + 4)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
context "for a method that returns nil (and ends in !)" do
|
48
48
|
it "stores the memoized value" do
|
49
49
|
object.returning_nil!
|
50
|
-
Date.
|
51
|
-
object.returning_nil
|
50
|
+
allow(Date).to receive(:today).and_raise(ArgumentError)
|
51
|
+
expect(object.returning_nil!).to be_nil
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
@@ -56,9 +56,9 @@ describe Memoizer do
|
|
56
56
|
let(:object) { Beepbop.new }
|
57
57
|
it "still memoizes things" do
|
58
58
|
Timecop.freeze(today)
|
59
|
-
object.no_params.
|
59
|
+
expect(object.no_params).to eq(today)
|
60
60
|
Timecop.freeze(tomorrow)
|
61
|
-
object.no_params.
|
61
|
+
expect(object.no_params).to eq(today)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -72,15 +72,15 @@ describe Memoizer do
|
|
72
72
|
let(:object) { Beirut.new }
|
73
73
|
|
74
74
|
it "respects the privacy of the memoized method" do
|
75
|
-
Beirut.private_method_defined?(:bar).
|
76
|
-
Beirut.private_method_defined?(:_unmemoized_bar).
|
75
|
+
expect(Beirut.private_method_defined?(:bar)).to be_truthy
|
76
|
+
expect(Beirut.private_method_defined?(:_unmemoized_bar)).to be_truthy
|
77
77
|
end
|
78
78
|
|
79
79
|
it "memoizes things" do
|
80
80
|
Timecop.freeze(today)
|
81
|
-
object.foo.
|
81
|
+
expect(object.foo).to eq(today)
|
82
82
|
Timecop.freeze(today + 1)
|
83
|
-
object.foo.
|
83
|
+
expect(object.foo).to eq(today)
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
@@ -94,15 +94,15 @@ describe Memoizer do
|
|
94
94
|
let(:object) { Wonka.new }
|
95
95
|
|
96
96
|
it "respects the privacy of the memoized method" do
|
97
|
-
Wonka.protected_method_defined?(:bar).
|
98
|
-
Wonka.protected_method_defined?(:_unmemoized_bar).
|
97
|
+
expect(Wonka.protected_method_defined?(:bar)).to be_truthy
|
98
|
+
expect(Wonka.protected_method_defined?(:_unmemoized_bar)).to be_truthy
|
99
99
|
end
|
100
100
|
|
101
101
|
it "memoizes things" do
|
102
102
|
Timecop.freeze(today)
|
103
|
-
object.foo.
|
103
|
+
expect(object.foo).to eq(today)
|
104
104
|
Timecop.freeze(today + 1)
|
105
|
-
object.foo.
|
105
|
+
expect(object.foo).to eq(today)
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -119,63 +119,63 @@ describe Memoizer do
|
|
119
119
|
let(:object) { MemoizerSpecClass.new }
|
120
120
|
before do
|
121
121
|
Timecop.freeze(today)
|
122
|
-
object.today.
|
123
|
-
object.plus_ndays(1).
|
124
|
-
object.plus_ndays(3).
|
122
|
+
expect(object.today).to eq(today)
|
123
|
+
expect(object.plus_ndays(1)).to eq(today + 1)
|
124
|
+
expect(object.plus_ndays(3)).to eq(today + 3)
|
125
125
|
end
|
126
126
|
|
127
127
|
describe '#unmemoize' do
|
128
128
|
context "for a method with no arguments" do
|
129
129
|
it "clears the memoized value so it can be rememoized" do
|
130
130
|
Timecop.freeze(today + 1)
|
131
|
-
object.today.
|
131
|
+
expect(object.today).to eq(today)
|
132
132
|
|
133
133
|
object.unmemoize(:today)
|
134
|
-
object.today.
|
134
|
+
expect(object.today).to eq(today + 1)
|
135
135
|
|
136
136
|
Timecop.freeze(today + 2)
|
137
|
-
object.today.
|
137
|
+
expect(object.today).to eq(today + 1)
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
141
141
|
context "for a method with arguments" do
|
142
142
|
it "unmemoizes for all inupts" do
|
143
143
|
Timecop.freeze(today + 1)
|
144
|
-
object.plus_ndays(1).
|
145
|
-
object.plus_ndays(3).
|
144
|
+
expect(object.plus_ndays(1)).to eq(today + 1)
|
145
|
+
expect(object.plus_ndays(3)).to eq(today + 3)
|
146
146
|
|
147
147
|
object.unmemoize(:plus_ndays)
|
148
|
-
object.plus_ndays(1).
|
149
|
-
object.plus_ndays(3).
|
148
|
+
expect(object.plus_ndays(1)).to eq(today + 2)
|
149
|
+
expect(object.plus_ndays(3)).to eq(today + 4)
|
150
150
|
|
151
151
|
Timecop.freeze(today + 2)
|
152
|
-
object.plus_ndays(1).
|
153
|
-
object.plus_ndays(3).
|
152
|
+
expect(object.plus_ndays(1)).to eq(today + 2)
|
153
|
+
expect(object.plus_ndays(3)).to eq(today + 4)
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
157
|
it "only affects the method specified" do
|
158
158
|
Timecop.freeze(today + 1)
|
159
|
-
object.today.
|
159
|
+
expect(object.today).to eq(today)
|
160
160
|
|
161
161
|
object.unmemoize(:plus_ndays)
|
162
|
-
object.today.
|
162
|
+
expect(object.today).to eq(today)
|
163
163
|
|
164
164
|
object.unmemoize(:today)
|
165
|
-
object.today.
|
165
|
+
expect(object.today).to eq(today + 1)
|
166
166
|
end
|
167
167
|
|
168
168
|
context "for subclasses" do
|
169
169
|
let(:object) { Beepbop.new }
|
170
170
|
it "clears the memoized value" do
|
171
171
|
Timecop.freeze(today + 1)
|
172
|
-
object.today.
|
172
|
+
expect(object.today).to eq(today)
|
173
173
|
|
174
174
|
object.unmemoize(:today)
|
175
|
-
object.today.
|
175
|
+
expect(object.today).to eq(today + 1)
|
176
176
|
|
177
177
|
Timecop.freeze(today + 2)
|
178
|
-
object.today.
|
178
|
+
expect(object.today).to eq(today + 1)
|
179
179
|
end
|
180
180
|
end
|
181
181
|
end
|
@@ -184,15 +184,15 @@ describe Memoizer do
|
|
184
184
|
shared_examples_for "unmemoizing methods" do
|
185
185
|
it "clears all memoized values" do
|
186
186
|
Timecop.freeze(today + 1)
|
187
|
-
object.today.
|
188
|
-
object.plus_ndays(1).
|
189
|
-
object.plus_ndays(3).
|
187
|
+
expect(object.today).to eq(today)
|
188
|
+
expect(object.plus_ndays(1)).to eq(today + 1)
|
189
|
+
expect(object.plus_ndays(3)).to eq(today + 3)
|
190
190
|
|
191
191
|
object.unmemoize_all
|
192
192
|
|
193
|
-
object.today.
|
194
|
-
object.plus_ndays(1).
|
195
|
-
object.plus_ndays(3).
|
193
|
+
expect(object.today).to eq(today + 1)
|
194
|
+
expect(object.plus_ndays(1)).to eq(today + 2)
|
195
|
+
expect(object.plus_ndays(3)).to eq(today + 4)
|
196
196
|
end
|
197
197
|
end
|
198
198
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memoizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Barun Singh
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
11
|
+
date: 2016-12-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 10.4.2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 10.4.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.5.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.5.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: timecop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.0
|
14
55
|
description: Memoizer caches the results of your method calls, works well with methods
|
15
56
|
that accept arguments or return nil. It's a simpler and more expicit alternative
|
16
57
|
to ActiveSupport::Memoizable
|
@@ -21,38 +62,37 @@ extra_rdoc_files:
|
|
21
62
|
- README.md
|
22
63
|
- LICENSE.txt
|
23
64
|
files:
|
24
|
-
- lib/memoizer/version.rb
|
25
|
-
- lib/memoizer.rb
|
26
|
-
- spec/memoizer_spec.rb
|
27
|
-
- spec/spec_helper.rb
|
28
65
|
- CHANGELOG.txt
|
29
66
|
- LICENSE.txt
|
30
67
|
- README.md
|
31
|
-
- memoizer.gemspec
|
32
68
|
- Rakefile
|
33
|
-
|
69
|
+
- lib/memoizer.rb
|
70
|
+
- lib/memoizer/version.rb
|
71
|
+
- memoizer.gemspec
|
72
|
+
- spec/memoizer_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/wegowise/memoizer
|
34
75
|
licenses:
|
35
76
|
- MIT
|
77
|
+
metadata: {}
|
36
78
|
post_install_message:
|
37
79
|
rdoc_options: []
|
38
80
|
require_paths:
|
39
81
|
- lib
|
40
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
83
|
requirements:
|
43
|
-
- -
|
84
|
+
- - ">="
|
44
85
|
- !ruby/object:Gem::Version
|
45
86
|
version: '0'
|
46
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
-
none: false
|
48
88
|
requirements:
|
49
|
-
- -
|
89
|
+
- - ">="
|
50
90
|
- !ruby/object:Gem::Version
|
51
91
|
version: 1.3.6
|
52
92
|
requirements: []
|
53
93
|
rubyforge_project:
|
54
|
-
rubygems_version:
|
94
|
+
rubygems_version: 2.5.1
|
55
95
|
signing_key:
|
56
|
-
specification_version:
|
96
|
+
specification_version: 4
|
57
97
|
summary: Memoizes (caches the results of) your methods
|
58
98
|
test_files: []
|