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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9c78fdf779defabcfac8c1f22986ed304dca27f
4
- data.tar.gz: f2f2c12536a72bebf60ffc455c3c24fdb93bf6e1
3
+ metadata.gz: 6eafbb59c1568063bab08d879276b6f9dd8a08f4
4
+ data.tar.gz: 6dc2a93ab06eb367aa4431534b7ed4d768824366
5
5
  SHA512:
6
- metadata.gz: fda92d74b0dab9e6c9ac5490cc2ad452df815260811069e9ac3d0d1fe0fee074525a665dc46f7ac51ea6a74c83fac495b1f4a4c3443b8e64e27a0bb920df65ac
7
- data.tar.gz: c3b9ea4fcecd032e6f0b78fcc5209df4297180fb75b6324b117d57cddf76cbde8d5edbda436c53d6398c173262a6594ed6b32ab8d9897cc31003fc68bf54d8c3
6
+ metadata.gz: 0d450d07e8d37029a54b5383095acfb7d0e2a772cff9deee85d0f050930e566124443a9a6b7e8c42c508ad21eb14c8046963eb220fbf8d60f6ff5ad523349fd1
7
+ data.tar.gz: d7090d4fec3b79ef89b3ced3ca136a866d1f4528b7819c7902a292d11d205d5c2018154dc2c5586a6c38d6674755527075d32515420293ad68b59d486e332118
@@ -6,12 +6,17 @@ rvm:
6
6
  - 1.9.2
7
7
  - 1.9.3
8
8
  - 2.0.0
9
- - 2.1.0
10
- - 2.2.0
11
- - 2.3.0
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
- - rbx-2
15
- - ruby-head
16
+ - jruby-9.1.5.0
16
17
  - jruby-head
17
- - ree
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 ( http://github.com/*my-github-username*/memoist/fork )
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 "rake/testtask"
3
+ require 'bundler/gem_tasks'
4
+
5
+ require 'rake/testtask'
5
6
  Rake::TestTask.new do |t|
6
- t.libs << "test"
7
- t.test_files = FileList["test/**/*_test.rb"]
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/**/*_test.rb']
8
9
  t.verbose = true
9
10
  end
10
11
 
11
- task :default => ["test"]
12
+ task default: ['test']
@@ -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
- "_memoized".freeze
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
- "_unmemoized".freeze
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
- structs = self.class.all_memoized_structs
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
- self.memoized_methods << mm
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
- def singleton_class
5
- class << self
6
- self
5
+ unless respond_to?(:singleton_class)
6
+ def singleton_class
7
+ class << self
8
+ self
9
+ end
7
10
  end
8
- end unless respond_to?(:singleton_class) # exists in 1.9.2
11
+ end # exists in 1.9.2
9
12
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Memoist
3
- VERSION = "0.15.0"
4
+ VERSION = '0.16.0'.freeze
4
5
  end
@@ -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
- ["Joshua Peek", "josh@joshpeek.com"],
8
- ["Tarmo Tänav", "tarmo@itech.ee"],
9
- ["Jeremy Kemper", "jeremy@bitsweat.net"],
10
- ["Eugene Pimenov", "libc@mac.com"],
11
- ["Xavier Noria", "fxn@hashref.com"],
12
- ["Niels Ganser", "niels@herimedia.co"],
13
- ["Carl Lerche & Yehuda Katz", "wycats@gmail.com"],
14
- ["jeem", "jeem@hughesorama.com"],
15
- ["Jay Pignata", "john.pignata@gmail.com"],
16
- ["Damien Mathieu", "42@dmathieu.com"],
17
- ["José Valim", "jose.valim@gmail.com"],
18
- ["Matthew Rudy Jacobs", "matthewrudyjacobs@gmail.com"],
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 = "memoist"
23
+ spec.name = 'memoist'
23
24
  spec.version = Memoist::VERSION
24
- spec.authors = AUTHORS.map{ |name, email| name }
25
- spec.email = AUTHORS.map{ |name, email| email }
26
- spec.summary = %q{memoize methods invocation}
27
- spec.homepage = "https://github.com/matthewrudy/memoist"
28
- spec.license = "MIT"
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 = ["lib"]
34
+ spec.require_paths = ['lib']
34
35
 
35
- spec.add_development_dependency "benchmark-ips"
36
- spec.add_development_dependency "bundler"
37
- spec.add_development_dependency "rake"
38
- spec.add_development_dependency "minitest", "~> 5.5.1"
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
@@ -1,4 +1,4 @@
1
- $:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib")
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
- "Hello World"
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("arity 0 - memoized") do |times|
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("arity 1 - memoized") do |times|
37
+ x.report('arity 1 - memoized') do |times|
38
38
  times.times do
39
39
  OBJECT.arity_1(:World)
40
40
  end
@@ -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
- "Josh"
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(name)
62
- "Joshua"
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(options = {})
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
- "Yes"
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, :identifier => :student
111
+ memoize :name, identifier: :student
115
112
  end
116
113
 
117
114
  class Teacher < Person
118
115
  def seniority
119
- "very_senior"
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
- "37signals"
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 "Josh", @person.name
239
+ assert_equal 'Josh', @person.name
191
240
  assert_equal 1, @person.name_calls
192
241
 
193
- 3.times { assert_equal "Josh", @person.name }
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(:age => 21, :name => 'James')
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(:age => 21, :name => 'James') }
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({:age => 21, :name => 'James'}, :reload) }
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
- assert_equal nil, @person.age
283
+ assert_nil @person.age
235
284
  assert_equal 1, @person.age_calls
236
285
 
237
- 3.times { assert_equal nil, @person.age }
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(age is_developer? memoize_protected_test name name? sleep update update_attributes)
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 "@_memoized_name", structs.detect {|s| s.memoized_method == :name }.ivar
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 "@_memoized_student_name", structs.detect {|s| s.memoized_method == :name }.ivar
359
+ assert_equal '@_memoized_student_name', structs.detect { |s| s.memoized_method == :name }.ivar
284
360
 
285
- expected = (expected << "seniority").sort
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 "@_memoized_name", structs.detect {|s| s.memoized_method == :name }.ivar
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 "Josh", teacher.name
298
- assert_equal "Josh", teacher.instance_variable_get(:@_memoized_name)
299
- assert_equal "very_senior", teacher.seniority
300
- assert_equal "very_senior", teacher.instance_variable_get(:@_memoized_seniority)
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 "Student Josh", student.name
308
- assert_equal "Student Josh", student.instance_variable_get(:@_memoized_student_name)
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 "very_senior", teacher.instance_variable_get(:@_memoized_seniority)
329
- assert_equal "Josh", teacher.instance_variable_get(:@_memoized_name)
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 "Student Josh", student.instance_variable_get(:@_memoized_student_name)
335
- assert_equal "Student Josh", student.name
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 "Josh", @person.name
348
- assert_equal "Joshua", @person.update("Joshua")
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 "37signals", company.name
461
+ assert_equal '37signals', company.name
376
462
  assert_equal 1, company.name_calls
377
- assert_equal "37signals", company.name
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, :identifier => :again
494
+ Person.memoize :name, identifier: :again
409
495
  p = Person.new
410
- assert_equal "Josh", p.name
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 == "@_memoized_again_name" }
507
+ assert Person.memoized_methods.any? { |m| m.ivar == '@_memoized_again_name' }
422
508
 
423
- [Student, Teacher, Person].each { |obj| obj.clear_structs }
424
- assert Person.memoized_methods.reject! { |m| m.ivar == "@_memoized_again_name" }
425
- assert_nil Student.memoized_methods.reject! { |m| m.ivar == "@_memoized_again_name" }
426
- assert_nil Teacher.memoized_methods.reject! { |m| m.ivar == "@_memoized_again_name" }
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; "bar"; end
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 "protected", person.send(:memoize_protected_test)
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 "Yes", person.send(:is_developer?)
545
+ assert_equal 'Yes', person.send(:is_developer?)
458
546
  assert_equal 1, person.is_developer_calls
459
- assert_equal "Yes", person.send(:is_developer?)
547
+ assert_equal 'Yes', person.send(:is_developer?)
460
548
  assert_equal 1, person.is_developer_calls
461
549
  end
462
-
463
550
  end
@@ -1,3 +1,3 @@
1
1
  require 'minitest/autorun'
2
2
 
3
- $:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib")
3
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
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.15.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: 2016-08-23 00:00:00.000000000 Z
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.5.1
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.5.1
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.5.1
131
+ rubygems_version: 2.6.10
132
132
  signing_key:
133
133
  specification_version: 4
134
134
  summary: memoize methods invocation