memoist 0.15.0 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +11 -6
- data/README.md +2 -2
- data/Rakefile +6 -5
- data/lib/memoist.rb +21 -21
- data/lib/memoist/core_ext/singleton_class.rb +7 -4
- data/lib/memoist/version.rb +2 -1
- data/memoist.gemspec +29 -24
- data/script/benchmark.rb +4 -4
- data/test/memoist_test.rb +136 -49
- data/test/test_helper.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6eafbb59c1568063bab08d879276b6f9dd8a08f4
|
4
|
+
data.tar.gz: 6dc2a93ab06eb367aa4431534b7ed4d768824366
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d450d07e8d37029a54b5383095acfb7d0e2a772cff9deee85d0f050930e566124443a9a6b7e8c42c508ad21eb14c8046963eb220fbf8d60f6ff5ad523349fd1
|
7
|
+
data.tar.gz: d7090d4fec3b79ef89b3ced3ca136a866d1f4528b7819c7902a292d11d205d5c2018154dc2c5586a6c38d6674755527075d32515420293ad68b59d486e332118
|
data/.travis.yml
CHANGED
@@ -6,12 +6,17 @@ rvm:
|
|
6
6
|
- 1.9.2
|
7
7
|
- 1.9.3
|
8
8
|
- 2.0.0
|
9
|
-
- 2.1.
|
10
|
-
- 2.2.
|
11
|
-
- 2.3.
|
9
|
+
- 2.1.10
|
10
|
+
- 2.2.7
|
11
|
+
- 2.3.4
|
12
|
+
- 2.4.1
|
13
|
+
- ruby-head
|
12
14
|
- jruby-18mode
|
13
15
|
- jruby-19mode
|
14
|
-
-
|
15
|
-
- ruby-head
|
16
|
+
- jruby-9.1.5.0
|
16
17
|
- jruby-head
|
17
|
-
|
18
|
+
|
19
|
+
before_install:
|
20
|
+
- gem install bundler --no-document -v '~> 1.13'
|
21
|
+
before_script:
|
22
|
+
- unset JRUBY_OPTS
|
data/README.md
CHANGED
@@ -113,7 +113,7 @@ person.taxes_due(100_000, true) # bypasses the memoized value and rememoizes it
|
|
113
113
|
If you want to flush the entire memoization cache for an object
|
114
114
|
|
115
115
|
```ruby
|
116
|
-
person.flush_cache
|
116
|
+
person.flush_cache # returns an array of flushed memoized methods, e.g. ["social_security", "some_method"]
|
117
117
|
```
|
118
118
|
|
119
119
|
Authors
|
@@ -137,7 +137,7 @@ Everyone who contributed to it in the rails repository.
|
|
137
137
|
Contributing
|
138
138
|
============
|
139
139
|
|
140
|
-
1. Fork it (
|
140
|
+
1. Fork it ( https://github.com/matthewrudy/memoist/fork )
|
141
141
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
142
142
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
143
143
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require "bundler/gem_tasks"
|
3
2
|
|
4
|
-
require
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
require 'rake/testtask'
|
5
6
|
Rake::TestTask.new do |t|
|
6
|
-
t.libs <<
|
7
|
-
t.test_files = FileList[
|
7
|
+
t.libs << 'test'
|
8
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
9
|
t.verbose = true
|
9
10
|
end
|
10
11
|
|
11
|
-
task :
|
12
|
+
task default: ['test']
|
data/lib/memoist.rb
CHANGED
@@ -1,30 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'memoist/version'
|
3
4
|
require 'memoist/core_ext/singleton_class'
|
4
5
|
|
5
6
|
module Memoist
|
7
|
+
def self.extended(extender)
|
8
|
+
Memoist.memoist_eval(extender) do
|
9
|
+
unless singleton_class.method_defined?(:memoized_methods)
|
10
|
+
def self.memoized_methods
|
11
|
+
@_memoized_methods ||= []
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
6
16
|
|
7
|
-
def self.memoized_ivar_for(method_name, identifier=nil)
|
17
|
+
def self.memoized_ivar_for(method_name, identifier = nil)
|
8
18
|
"@#{memoized_prefix(identifier)}_#{escape_punctuation(method_name)}"
|
9
19
|
end
|
10
20
|
|
11
|
-
def self.unmemoized_method_for(method_name, identifier=nil)
|
21
|
+
def self.unmemoized_method_for(method_name, identifier = nil)
|
12
22
|
"#{unmemoized_prefix(identifier)}_#{method_name}".to_sym
|
13
23
|
end
|
14
24
|
|
15
|
-
def self.memoized_prefix(identifier=nil)
|
25
|
+
def self.memoized_prefix(identifier = nil)
|
16
26
|
if identifier
|
17
27
|
"_memoized_#{identifier}"
|
18
28
|
else
|
19
|
-
|
29
|
+
'_memoized'.freeze
|
20
30
|
end
|
21
31
|
end
|
22
32
|
|
23
|
-
def self.unmemoized_prefix(identifier=nil)
|
33
|
+
def self.unmemoized_prefix(identifier = nil)
|
24
34
|
if identifier
|
25
35
|
"_unmemoized_#{identifier}"
|
26
36
|
else
|
27
|
-
|
37
|
+
'_unmemoized'.freeze
|
28
38
|
end
|
29
39
|
end
|
30
40
|
|
@@ -66,7 +76,8 @@ module Memoist
|
|
66
76
|
end
|
67
77
|
|
68
78
|
def memoized_structs(names)
|
69
|
-
|
79
|
+
ref_obj = self.class.respond_to?(:class_eval) ? singleton_class : self
|
80
|
+
structs = ref_obj.all_memoized_structs
|
70
81
|
return structs if names.empty?
|
71
82
|
|
72
83
|
structs.select { |s| names.include?(s.memoized_method) }
|
@@ -100,7 +111,7 @@ module Memoist
|
|
100
111
|
# an ancestor method.
|
101
112
|
ancestors.grep(Memoist).each do |ancestor|
|
102
113
|
ancestor.memoized_methods.each do |m|
|
103
|
-
structs << m unless structs.any? {|am| am.memoized_method == m.memoized_method }
|
114
|
+
structs << m unless structs.any? { |am| am.memoized_method == m.memoized_method }
|
104
115
|
end
|
105
116
|
end
|
106
117
|
structs
|
@@ -112,17 +123,7 @@ module Memoist
|
|
112
123
|
end
|
113
124
|
|
114
125
|
def memoize(*method_names)
|
115
|
-
if method_names.last.is_a?(Hash)
|
116
|
-
identifier = method_names.pop[:identifier]
|
117
|
-
end
|
118
|
-
|
119
|
-
Memoist.memoist_eval(self) do
|
120
|
-
unless singleton_class.method_defined?(:memoized_methods)
|
121
|
-
def self.memoized_methods
|
122
|
-
@_memoized_methods ||= []
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
+
identifier = method_names.pop[:identifier] if method_names.last.is_a?(Hash)
|
126
127
|
|
127
128
|
method_names.each do |method_name|
|
128
129
|
unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier)
|
@@ -138,7 +139,7 @@ module Memoist
|
|
138
139
|
alias_method unmemoized_method, method_name
|
139
140
|
|
140
141
|
mm = MemoizedMethod.new(method_name, memoized_ivar, instance_method(method_name).arity)
|
141
|
-
|
142
|
+
memoized_methods << mm
|
142
143
|
if mm.arity == 0
|
143
144
|
|
144
145
|
# define a method like this;
|
@@ -235,5 +236,4 @@ module Memoist
|
|
235
236
|
# return a chainable method_name symbol if we can
|
236
237
|
method_names.length == 1 ? method_names.first : method_names
|
237
238
|
end
|
238
|
-
|
239
239
|
end
|
@@ -1,9 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Kernel
|
3
4
|
# Returns the object's singleton class.
|
4
|
-
|
5
|
-
|
6
|
-
self
|
5
|
+
unless respond_to?(:singleton_class)
|
6
|
+
def singleton_class
|
7
|
+
class << self
|
8
|
+
self
|
9
|
+
end
|
7
10
|
end
|
8
|
-
end
|
11
|
+
end # exists in 1.9.2
|
9
12
|
end
|
data/lib/memoist/version.rb
CHANGED
data/memoist.gemspec
CHANGED
@@ -1,39 +1,44 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'memoist/version'
|
5
6
|
|
6
7
|
AUTHORS = [
|
7
|
-
[
|
8
|
-
[
|
9
|
-
[
|
10
|
-
[
|
11
|
-
[
|
12
|
-
[
|
13
|
-
[
|
14
|
-
[
|
15
|
-
[
|
16
|
-
[
|
17
|
-
[
|
18
|
-
[
|
19
|
-
]
|
8
|
+
['Joshua Peek', 'josh@joshpeek.com'],
|
9
|
+
['Tarmo Tänav', 'tarmo@itech.ee'],
|
10
|
+
['Jeremy Kemper', 'jeremy@bitsweat.net'],
|
11
|
+
['Eugene Pimenov', 'libc@mac.com'],
|
12
|
+
['Xavier Noria', 'fxn@hashref.com'],
|
13
|
+
['Niels Ganser', 'niels@herimedia.co'],
|
14
|
+
['Carl Lerche & Yehuda Katz', 'wycats@gmail.com'],
|
15
|
+
['jeem', 'jeem@hughesorama.com'],
|
16
|
+
['Jay Pignata', 'john.pignata@gmail.com'],
|
17
|
+
['Damien Mathieu', '42@dmathieu.com'],
|
18
|
+
['José Valim', 'jose.valim@gmail.com'],
|
19
|
+
['Matthew Rudy Jacobs', 'matthewrudyjacobs@gmail.com']
|
20
|
+
].freeze
|
20
21
|
|
21
22
|
Gem::Specification.new do |spec|
|
22
|
-
spec.name =
|
23
|
+
spec.name = 'memoist'
|
23
24
|
spec.version = Memoist::VERSION
|
24
|
-
spec.authors = AUTHORS.map{ |name,
|
25
|
-
spec.email = AUTHORS.map{ |
|
26
|
-
spec.summary =
|
27
|
-
spec.homepage =
|
28
|
-
spec.license =
|
25
|
+
spec.authors = AUTHORS.map { |name, _email| name }
|
26
|
+
spec.email = AUTHORS.map { |_name, email| email }
|
27
|
+
spec.summary = 'memoize methods invocation'
|
28
|
+
spec.homepage = 'https://github.com/matthewrudy/memoist'
|
29
|
+
spec.license = 'MIT'
|
29
30
|
|
30
31
|
spec.files = `git ls-files -z`.split("\x0")
|
31
32
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
32
33
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
33
|
-
spec.require_paths = [
|
34
|
+
spec.require_paths = ['lib']
|
34
35
|
|
35
|
-
spec.add_development_dependency
|
36
|
-
spec.add_development_dependency
|
37
|
-
|
38
|
-
|
36
|
+
spec.add_development_dependency 'benchmark-ips'
|
37
|
+
spec.add_development_dependency 'bundler'
|
38
|
+
if RUBY_VERSION < '1.9.3'
|
39
|
+
spec.add_development_dependency 'rake', '~> 10.4'
|
40
|
+
else
|
41
|
+
spec.add_development_dependency 'rake'
|
42
|
+
end
|
43
|
+
spec.add_development_dependency 'minitest', '~> 5.10'
|
39
44
|
end
|
data/script/benchmark.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
2
2
|
require 'benchmark/ips'
|
3
3
|
|
4
4
|
require 'memoist'
|
@@ -7,7 +7,7 @@ class Benchy
|
|
7
7
|
extend Memoist
|
8
8
|
|
9
9
|
def arity_0
|
10
|
-
|
10
|
+
'Hello World'
|
11
11
|
end
|
12
12
|
memoize :arity_0
|
13
13
|
|
@@ -22,7 +22,7 @@ OBJECT = Benchy.new
|
|
22
22
|
puts "Benchmarking: #{Memoist::VERSION}"
|
23
23
|
|
24
24
|
Benchmark.ips do |x|
|
25
|
-
x.report(
|
25
|
+
x.report('arity 0 - memoized') do |times|
|
26
26
|
times.times do
|
27
27
|
OBJECT.arity_0
|
28
28
|
end
|
@@ -34,7 +34,7 @@ Benchmark.ips do |x|
|
|
34
34
|
# end
|
35
35
|
# end
|
36
36
|
|
37
|
-
x.report(
|
37
|
+
x.report('arity 1 - memoized') do |times|
|
38
38
|
times.times do
|
39
39
|
OBJECT.arity_1(:World)
|
40
40
|
end
|
data/test/memoist_test.rb
CHANGED
@@ -2,9 +2,7 @@ require 'test_helper'
|
|
2
2
|
require 'memoist'
|
3
3
|
|
4
4
|
class MemoistTest < Minitest::Test
|
5
|
-
|
6
5
|
class CallCounter
|
7
|
-
|
8
6
|
def initialize
|
9
7
|
@calls = {}
|
10
8
|
end
|
@@ -17,7 +15,6 @@ class MemoistTest < Minitest::Test
|
|
17
15
|
def count(method_name)
|
18
16
|
@calls[method_name] ||= 0
|
19
17
|
end
|
20
|
-
|
21
18
|
end
|
22
19
|
|
23
20
|
class Person
|
@@ -49,7 +46,7 @@ class MemoistTest < Minitest::Test
|
|
49
46
|
|
50
47
|
def name
|
51
48
|
@counter.call(:name)
|
52
|
-
|
49
|
+
'Josh'
|
53
50
|
end
|
54
51
|
|
55
52
|
def name?
|
@@ -58,8 +55,8 @@ class MemoistTest < Minitest::Test
|
|
58
55
|
end
|
59
56
|
memoize :name?
|
60
57
|
|
61
|
-
def update(
|
62
|
-
|
58
|
+
def update(_name)
|
59
|
+
'Joshua'
|
63
60
|
end
|
64
61
|
memoize :update
|
65
62
|
|
@@ -80,7 +77,7 @@ class MemoistTest < Minitest::Test
|
|
80
77
|
@counter.count(:sleep)
|
81
78
|
end
|
82
79
|
|
83
|
-
def update_attributes(
|
80
|
+
def update_attributes(_options = {})
|
84
81
|
@counter.call(:update_attributes)
|
85
82
|
true
|
86
83
|
end
|
@@ -101,7 +98,7 @@ class MemoistTest < Minitest::Test
|
|
101
98
|
|
102
99
|
def is_developer?
|
103
100
|
@counter.call(:is_developer?)
|
104
|
-
|
101
|
+
'Yes'
|
105
102
|
end
|
106
103
|
memoize :is_developer?
|
107
104
|
end
|
@@ -111,12 +108,12 @@ class MemoistTest < Minitest::Test
|
|
111
108
|
@counter.call(:student_name)
|
112
109
|
"Student #{super}"
|
113
110
|
end
|
114
|
-
memoize :name, :
|
111
|
+
memoize :name, identifier: :student
|
115
112
|
end
|
116
113
|
|
117
114
|
class Teacher < Person
|
118
115
|
def seniority
|
119
|
-
|
116
|
+
'very_senior'
|
120
117
|
end
|
121
118
|
memoize :seniority
|
122
119
|
end
|
@@ -129,7 +126,7 @@ class MemoistTest < Minitest::Test
|
|
129
126
|
|
130
127
|
def name
|
131
128
|
@name_calls += 1
|
132
|
-
|
129
|
+
'37signals'
|
133
130
|
end
|
134
131
|
end
|
135
132
|
|
@@ -181,16 +178,68 @@ class MemoistTest < Minitest::Test
|
|
181
178
|
memoize :counter
|
182
179
|
end
|
183
180
|
|
181
|
+
class Book
|
182
|
+
extend Memoist
|
183
|
+
STATUSES = %w[new used].freeze
|
184
|
+
CLASSIFICATION = %w[fiction nonfiction].freeze
|
185
|
+
GENRES = %w[humor romance reference sci-fi classic philosophy].freeze
|
186
|
+
|
187
|
+
attr_reader :title, :author
|
188
|
+
def initialize(title, author)
|
189
|
+
@title = title
|
190
|
+
@author = author
|
191
|
+
end
|
192
|
+
|
193
|
+
def full_title
|
194
|
+
"#{@title} by #{@author}"
|
195
|
+
end
|
196
|
+
memoize :full_title
|
197
|
+
|
198
|
+
class << self
|
199
|
+
extend Memoist
|
200
|
+
|
201
|
+
def all_types
|
202
|
+
STATUSES.product(CLASSIFICATION).product(GENRES).collect(&:flatten)
|
203
|
+
end
|
204
|
+
memoize :all_types
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
class Abb
|
209
|
+
extend Memoist
|
210
|
+
|
211
|
+
def run(*_args)
|
212
|
+
flush_cache if respond_to?(:flush_cache)
|
213
|
+
execute
|
214
|
+
end
|
215
|
+
|
216
|
+
def execute
|
217
|
+
some_method
|
218
|
+
end
|
219
|
+
|
220
|
+
def some_method
|
221
|
+
# Override this
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
class Bbb < Abb
|
226
|
+
def some_method
|
227
|
+
:foo
|
228
|
+
end
|
229
|
+
memoize :some_method
|
230
|
+
end
|
231
|
+
|
184
232
|
def setup
|
185
233
|
@person = Person.new
|
186
234
|
@calculator = Calculator.new
|
235
|
+
@book = Book.new('My Life', "Brian 'Fudge' Turmuck")
|
187
236
|
end
|
188
237
|
|
189
238
|
def test_memoization
|
190
|
-
assert_equal
|
239
|
+
assert_equal 'Josh', @person.name
|
191
240
|
assert_equal 1, @person.name_calls
|
192
241
|
|
193
|
-
3.times { assert_equal
|
242
|
+
3.times { assert_equal 'Josh', @person.name }
|
194
243
|
assert_equal 1, @person.name_calls
|
195
244
|
end
|
196
245
|
|
@@ -206,13 +255,13 @@ class MemoistTest < Minitest::Test
|
|
206
255
|
end
|
207
256
|
|
208
257
|
def test_memoize_with_options_hash
|
209
|
-
assert_equal true, @person.update_attributes(:
|
258
|
+
assert_equal true, @person.update_attributes(age: 21, name: 'James')
|
210
259
|
assert_equal 1, @person.update_attributes_calls
|
211
260
|
|
212
|
-
3.times { assert_equal true, @person.update_attributes(:
|
261
|
+
3.times { assert_equal true, @person.update_attributes(age: 21, name: 'James') }
|
213
262
|
assert_equal 1, @person.update_attributes_calls
|
214
263
|
|
215
|
-
3.times { assert_equal true, @person.update_attributes({:
|
264
|
+
3.times { assert_equal true, @person.update_attributes({ age: 21, name: 'James' }, :reload) }
|
216
265
|
assert_equal 4, @person.update_attributes_calls
|
217
266
|
end
|
218
267
|
|
@@ -231,10 +280,10 @@ class MemoistTest < Minitest::Test
|
|
231
280
|
end
|
232
281
|
|
233
282
|
def test_memoization_with_nil_value
|
234
|
-
|
283
|
+
assert_nil @person.age
|
235
284
|
assert_equal 1, @person.age_calls
|
236
285
|
|
237
|
-
3.times {
|
286
|
+
3.times { assert_nil @person.age }
|
238
287
|
assert_equal 1, @person.age_calls
|
239
288
|
end
|
240
289
|
|
@@ -256,6 +305,33 @@ class MemoistTest < Minitest::Test
|
|
256
305
|
assert_equal 2, @calculator.counter
|
257
306
|
end
|
258
307
|
|
308
|
+
def test_class_flush_cache
|
309
|
+
@book.memoize_all
|
310
|
+
assert_equal "My Life by Brian 'Fudge' Turmuck", @book.full_title
|
311
|
+
|
312
|
+
Book.memoize_all
|
313
|
+
assert_instance_of Array, Book.instance_variable_get(:@_memoized_all_types)
|
314
|
+
Book.flush_cache
|
315
|
+
assert_equal false, Book.instance_variable_defined?(:@_memoized_all_types)
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_class_flush_cache_preserves_instances
|
319
|
+
@book.memoize_all
|
320
|
+
Book.memoize_all
|
321
|
+
assert_equal "My Life by Brian 'Fudge' Turmuck", @book.full_title
|
322
|
+
|
323
|
+
Book.flush_cache
|
324
|
+
assert_equal false, Book.instance_variable_defined?(:@_memoized_all_types)
|
325
|
+
assert_equal "My Life by Brian 'Fudge' Turmuck", @book.full_title
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_flush_cache_in_child_class
|
329
|
+
x = Bbb.new
|
330
|
+
|
331
|
+
# This should not throw error
|
332
|
+
x.run
|
333
|
+
end
|
334
|
+
|
259
335
|
def test_unmemoize_all
|
260
336
|
assert_equal 1, @calculator.counter
|
261
337
|
|
@@ -272,20 +348,20 @@ class MemoistTest < Minitest::Test
|
|
272
348
|
# Student < Person memoize :name, :identifier => :student
|
273
349
|
# Teacher < Person memoize :seniority
|
274
350
|
|
275
|
-
expected = %w
|
351
|
+
expected = %w[age is_developer? memoize_protected_test name name? sleep update update_attributes]
|
276
352
|
structs = Person.all_memoized_structs
|
277
353
|
assert_equal expected, structs.collect(&:memoized_method).collect(&:to_s).sort
|
278
|
-
assert_equal
|
354
|
+
assert_equal '@_memoized_name', structs.detect { |s| s.memoized_method == :name }.ivar
|
279
355
|
|
280
356
|
# Same expected methods
|
281
357
|
structs = Student.all_memoized_structs
|
282
358
|
assert_equal expected, structs.collect(&:memoized_method).collect(&:to_s).sort
|
283
|
-
assert_equal
|
359
|
+
assert_equal '@_memoized_student_name', structs.detect { |s| s.memoized_method == :name }.ivar
|
284
360
|
|
285
|
-
expected = (expected <<
|
361
|
+
expected = (expected << 'seniority').sort
|
286
362
|
structs = Teacher.all_memoized_structs
|
287
363
|
assert_equal expected, structs.collect(&:memoized_method).collect(&:to_s).sort
|
288
|
-
assert_equal
|
364
|
+
assert_equal '@_memoized_name', structs.detect { |s| s.memoized_method == :name }.ivar
|
289
365
|
end
|
290
366
|
|
291
367
|
def test_unmemoize_all_subclasses
|
@@ -294,18 +370,18 @@ class MemoistTest < Minitest::Test
|
|
294
370
|
# Teacher < Person memoize :seniority
|
295
371
|
|
296
372
|
teacher = Teacher.new
|
297
|
-
assert_equal
|
298
|
-
assert_equal
|
299
|
-
assert_equal
|
300
|
-
assert_equal
|
373
|
+
assert_equal 'Josh', teacher.name
|
374
|
+
assert_equal 'Josh', teacher.instance_variable_get(:@_memoized_name)
|
375
|
+
assert_equal 'very_senior', teacher.seniority
|
376
|
+
assert_equal 'very_senior', teacher.instance_variable_get(:@_memoized_seniority)
|
301
377
|
|
302
378
|
teacher.unmemoize_all
|
303
379
|
assert_equal false, teacher.instance_variable_defined?(:@_memoized_name)
|
304
380
|
assert_equal false, teacher.instance_variable_defined?(:@_memoized_seniority)
|
305
381
|
|
306
382
|
student = Student.new
|
307
|
-
assert_equal
|
308
|
-
assert_equal
|
383
|
+
assert_equal 'Student Josh', student.name
|
384
|
+
assert_equal 'Student Josh', student.instance_variable_get(:@_memoized_student_name)
|
309
385
|
assert_equal false, student.instance_variable_defined?(:@_memoized_seniority)
|
310
386
|
|
311
387
|
student.unmemoize_all
|
@@ -325,14 +401,14 @@ class MemoistTest < Minitest::Test
|
|
325
401
|
teacher = Teacher.new
|
326
402
|
teacher.memoize_all
|
327
403
|
|
328
|
-
assert_equal
|
329
|
-
assert_equal
|
404
|
+
assert_equal 'very_senior', teacher.instance_variable_get(:@_memoized_seniority)
|
405
|
+
assert_equal 'Josh', teacher.instance_variable_get(:@_memoized_name)
|
330
406
|
|
331
407
|
student = Student.new
|
332
408
|
student.memoize_all
|
333
409
|
|
334
|
-
assert_equal
|
335
|
-
assert_equal
|
410
|
+
assert_equal 'Student Josh', student.instance_variable_get(:@_memoized_student_name)
|
411
|
+
assert_equal 'Student Josh', student.name
|
336
412
|
assert_equal false, student.instance_variable_defined?(:@_memoized_seniority)
|
337
413
|
end
|
338
414
|
|
@@ -342,10 +418,20 @@ class MemoistTest < Minitest::Test
|
|
342
418
|
assert_equal 1, Calculator.new.counter
|
343
419
|
end
|
344
420
|
|
421
|
+
def test_memoization_class_variables
|
422
|
+
@book.memoize_all
|
423
|
+
assert_equal "My Life by Brian 'Fudge' Turmuck", @book.instance_variable_get(:@_memoized_full_title)
|
424
|
+
assert_equal "My Life by Brian 'Fudge' Turmuck", @book.full_title
|
425
|
+
|
426
|
+
Book.memoize_all
|
427
|
+
assert_instance_of Array, Book.instance_variable_get(:@_memoized_all_types)
|
428
|
+
assert_equal 24, Book.all_types.count
|
429
|
+
end
|
430
|
+
|
345
431
|
def test_memoized_is_not_affected_by_freeze
|
346
432
|
@person.freeze
|
347
|
-
assert_equal
|
348
|
-
assert_equal
|
433
|
+
assert_equal 'Josh', @person.name
|
434
|
+
assert_equal 'Joshua', @person.update('Joshua')
|
349
435
|
end
|
350
436
|
|
351
437
|
def test_memoization_with_args
|
@@ -372,9 +458,9 @@ class MemoistTest < Minitest::Test
|
|
372
458
|
company.extend Memoist
|
373
459
|
company.memoize :name
|
374
460
|
|
375
|
-
assert_equal
|
461
|
+
assert_equal '37signals', company.name
|
376
462
|
assert_equal 1, company.name_calls
|
377
|
-
assert_equal
|
463
|
+
assert_equal '37signals', company.name
|
378
464
|
assert_equal 1, company.name_calls
|
379
465
|
end
|
380
466
|
end
|
@@ -405,9 +491,9 @@ class MemoistTest < Minitest::Test
|
|
405
491
|
# Student < Person memoize :name, :identifier => :student
|
406
492
|
# Teacher < Person memoize :seniority
|
407
493
|
|
408
|
-
Person.memoize :name, :
|
494
|
+
Person.memoize :name, identifier: :again
|
409
495
|
p = Person.new
|
410
|
-
assert_equal
|
496
|
+
assert_equal 'Josh', p.name
|
411
497
|
assert p.instance_variable_get(:@_memoized_again_name)
|
412
498
|
|
413
499
|
# HACK: tl;dr: Don't memoize classes in test that are used elsewhere.
|
@@ -418,12 +504,12 @@ class MemoistTest < Minitest::Test
|
|
418
504
|
Student.all_memoized_structs
|
419
505
|
Person.all_memoized_structs
|
420
506
|
Teacher.all_memoized_structs
|
421
|
-
assert Person.memoized_methods.any? { |m| m.ivar ==
|
507
|
+
assert Person.memoized_methods.any? { |m| m.ivar == '@_memoized_again_name' }
|
422
508
|
|
423
|
-
[Student, Teacher, Person].each
|
424
|
-
assert Person.memoized_methods.reject! { |m| m.ivar ==
|
425
|
-
assert_nil Student.memoized_methods.reject! { |m| m.ivar ==
|
426
|
-
assert_nil Teacher.memoized_methods.reject! { |m| m.ivar ==
|
509
|
+
[Student, Teacher, Person].each(&:clear_structs)
|
510
|
+
assert Person.memoized_methods.reject! { |m| m.ivar == '@_memoized_again_name' }
|
511
|
+
assert_nil Student.memoized_methods.reject! { |m| m.ivar == '@_memoized_again_name' }
|
512
|
+
assert_nil Teacher.memoized_methods.reject! { |m| m.ivar == '@_memoized_again_name' }
|
427
513
|
end
|
428
514
|
|
429
515
|
def test_memoization_with_a_subclass
|
@@ -436,7 +522,9 @@ class MemoistTest < Minitest::Test
|
|
436
522
|
|
437
523
|
def test_memoization_is_chainable
|
438
524
|
klass = Class.new do
|
439
|
-
def foo
|
525
|
+
def foo
|
526
|
+
'bar'
|
527
|
+
end
|
440
528
|
end
|
441
529
|
klass.extend Memoist
|
442
530
|
chainable = klass.memoize :foo
|
@@ -447,17 +535,16 @@ class MemoistTest < Minitest::Test
|
|
447
535
|
person = Person.new
|
448
536
|
|
449
537
|
assert_raises(NoMethodError) { person.memoize_protected_test }
|
450
|
-
assert_equal
|
538
|
+
assert_equal 'protected', person.send(:memoize_protected_test)
|
451
539
|
end
|
452
540
|
|
453
541
|
def test_private_method_memoization
|
454
542
|
person = Person.new
|
455
543
|
|
456
544
|
assert_raises(NoMethodError) { person.is_developer? }
|
457
|
-
assert_equal
|
545
|
+
assert_equal 'Yes', person.send(:is_developer?)
|
458
546
|
assert_equal 1, person.is_developer_calls
|
459
|
-
assert_equal
|
547
|
+
assert_equal 'Yes', person.send(:is_developer?)
|
460
548
|
assert_equal 1, person.is_developer_calls
|
461
549
|
end
|
462
|
-
|
463
550
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memoist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Peek
|
@@ -19,7 +19,7 @@ authors:
|
|
19
19
|
autorequire:
|
20
20
|
bindir: bin
|
21
21
|
cert_chain: []
|
22
|
-
date:
|
22
|
+
date: 2017-06-20 00:00:00.000000000 Z
|
23
23
|
dependencies:
|
24
24
|
- !ruby/object:Gem::Dependency
|
25
25
|
name: benchmark-ips
|
@@ -69,14 +69,14 @@ dependencies:
|
|
69
69
|
requirements:
|
70
70
|
- - "~>"
|
71
71
|
- !ruby/object:Gem::Version
|
72
|
-
version: 5.
|
72
|
+
version: '5.10'
|
73
73
|
type: :development
|
74
74
|
prerelease: false
|
75
75
|
version_requirements: !ruby/object:Gem::Requirement
|
76
76
|
requirements:
|
77
77
|
- - "~>"
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: 5.
|
79
|
+
version: '5.10'
|
80
80
|
description:
|
81
81
|
email:
|
82
82
|
- josh@joshpeek.com
|
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
128
|
version: '0'
|
129
129
|
requirements: []
|
130
130
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
131
|
+
rubygems_version: 2.6.10
|
132
132
|
signing_key:
|
133
133
|
specification_version: 4
|
134
134
|
summary: memoize methods invocation
|