lite-memoize 1.0.1 → 1.1.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
  SHA256:
3
- metadata.gz: 2a2e51485362684ce1520b7eca9484106b5b83c9bc6336b2215dcffa6d56cfed
4
- data.tar.gz: 43eb2cd5cb4330f12a503f3b66594b6a0c076bb1a0a991a0735bef1c11fd3cb6
3
+ metadata.gz: 550437dcb088779e47db178fb36ae20e72d95ef8353699627dc28b453038e5f7
4
+ data.tar.gz: 64e12bf2ffd2557c8e533289b202b9b41b0516f4d2390252befbfe0d540354ed
5
5
  SHA512:
6
- metadata.gz: b082b4804dbcedb965b006b0077375195316720d7397b5ed0140b5ae0de8847706d3503e8ca9f33f7c6ce064a0d79c2ff6f25a57a06e3a458ee2506e0f623436
7
- data.tar.gz: 0dc03f302a930483a8ca51f114e80b6ed36506153bf1840f0ddcfb221404acd1f307dc77ae1dcf8f4a0a60970513d382954180cb70e49928d7727a41dcdcf48c
6
+ metadata.gz: bc2f90b8c883e926a3621a2df8d8134f47dc19f96c7cd8137f17f66b77961f0ee0f26c39b029c2c99d5228c55e0baf1e5527ade2db5298aac8a85011b28ca970
7
+ data.tar.gz: 507083fde386b02a97a509d74a955308f688420d873450c79d55f0f7932bd40fc7202de8d117e5dde333d909455d250f110d6d8fc3e3e9633abb888c2b91a40f
data/.rubocop.yml CHANGED
@@ -2,11 +2,14 @@ require:
2
2
  - rubocop-performance
3
3
  - rubocop-rspec
4
4
  AllCops:
5
- TargetRubyVersion: 2.6
5
+ TargetRubyVersion: 3.0
6
+ NewCops: enable
6
7
  DisplayCopNames: true
7
8
  DisplayStyleGuide: true
8
- LineLength:
9
- Max: 100
9
+ Gemspec/RequiredRubyVersion:
10
+ Enabled: false
11
+ Layout/EmptyLinesAroundAttributeAccessor:
12
+ Enabled: true
10
13
  Layout/EmptyLinesAroundBlockBody:
11
14
  Exclude:
12
15
  - 'spec/**/**/*'
@@ -14,6 +17,14 @@ Layout/EmptyLinesAroundClassBody:
14
17
  EnforcedStyle: empty_lines_except_namespace
15
18
  Layout/EmptyLinesAroundModuleBody:
16
19
  EnforcedStyle: empty_lines_except_namespace
20
+ Layout/LineLength:
21
+ Max: 100
22
+ Layout/SpaceAroundMethodCallOperator:
23
+ Enabled: true
24
+ Lint/RaiseException:
25
+ Enabled: true
26
+ Lint/StructNewOverride:
27
+ Enabled: true
17
28
  Metrics/BlockLength:
18
29
  Exclude:
19
30
  - 'spec/**/**/*'
@@ -24,9 +35,15 @@ Naming/FileName:
24
35
  Enabled: false
25
36
  Naming/MemoizedInstanceVariableName:
26
37
  Enabled: false
27
- Naming/UncommunicativeMethodParamName:
38
+ Naming/MethodParameterName:
39
+ Enabled: false
40
+ Style/AccessModifierDeclarations:
41
+ Enabled: false
42
+ Style/ArgumentsForwarding:
28
43
  Enabled: false
29
44
  Style/Documentation:
30
45
  Enabled: false
46
+ Style/DocumentDynamicEvalDefinition:
47
+ Enabled: false
31
48
  Style/ExpandPathArguments:
32
49
  Enabled: false
data/.travis.yml CHANGED
@@ -4,6 +4,7 @@ cache: bundler
4
4
  rvm:
5
5
  - 2.5
6
6
  - 2.6
7
+ - 2.7
7
8
  - ruby-head
8
9
  matrix:
9
10
  fast_finish: true
data/CHANGELOG.md CHANGED
@@ -6,6 +6,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.1.0] - 2021-07-19
10
+ ### Added
11
+ - Added Ruby 3.0 support
12
+
13
+ ## [1.0.4] - 2021-05-07
14
+ ### Changed
15
+ - Performance bind call
16
+
17
+ ## [1.0.3] - 2019-08-13
18
+ ### Added
19
+ - Added ruby 2.7 support
20
+ - Added clear alias from clear_cache on Alias
21
+
22
+ ## [1.0.2] - 2019-08-07
23
+ ### Added
24
+ - Added variable base memoization
25
+ ### Changed
26
+ - Renamed mixin to table
27
+ - Changed caller_key method to private
28
+
9
29
  ## [1.0.1] - 2019-07-18
10
30
  ### Added
11
31
  - Added alias memoization
@@ -13,7 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
13
33
  ### Changed
14
34
  - Improved speed of key generation and lookup for instance
15
35
  - Changed refresh arg to reload
16
- - Renamed shared to mixin
36
+ - Renamed shared to table
17
37
 
18
38
  ## [1.0.0] - 2019-06-24
19
39
  ### Added
data/Gemfile.lock CHANGED
@@ -1,52 +1,61 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-memoize (1.0.1)
4
+ lite-memoize (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.0)
9
+ ast (2.4.2)
10
10
  colorize (0.8.1)
11
- diff-lcs (1.3)
12
- fasterer (0.5.1)
11
+ diff-lcs (1.4.4)
12
+ fasterer (0.9.0)
13
13
  colorize (~> 0.7)
14
- ruby_parser (>= 3.13.0)
15
- jaro_winkler (1.5.3)
16
- parallel (1.17.0)
17
- parser (2.6.3.0)
18
- ast (~> 2.4.0)
14
+ ruby_parser (>= 3.14.1)
15
+ parallel (1.20.1)
16
+ parser (3.0.2.0)
17
+ ast (~> 2.4.1)
19
18
  rainbow (3.0.0)
20
- rake (12.3.2)
21
- rspec (3.8.0)
22
- rspec-core (~> 3.8.0)
23
- rspec-expectations (~> 3.8.0)
24
- rspec-mocks (~> 3.8.0)
25
- rspec-core (3.8.1)
26
- rspec-support (~> 3.8.0)
27
- rspec-expectations (3.8.4)
19
+ rake (13.0.6)
20
+ regexp_parser (2.1.1)
21
+ rexml (3.2.5)
22
+ rspec (3.10.0)
23
+ rspec-core (~> 3.10.0)
24
+ rspec-expectations (~> 3.10.0)
25
+ rspec-mocks (~> 3.10.0)
26
+ rspec-core (3.10.1)
27
+ rspec-support (~> 3.10.0)
28
+ rspec-expectations (3.10.1)
28
29
  diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.8.0)
30
- rspec-mocks (3.8.1)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-mocks (3.10.2)
31
32
  diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.8.0)
33
- rspec-support (3.8.2)
34
- rubocop (0.71.0)
35
- jaro_winkler (~> 1.5.1)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-support (3.10.2)
35
+ rubocop (1.18.3)
36
36
  parallel (~> 1.10)
37
- parser (>= 2.6)
37
+ parser (>= 3.0.0.0)
38
38
  rainbow (>= 2.2.2, < 4.0)
39
+ regexp_parser (>= 1.8, < 3.0)
40
+ rexml
41
+ rubocop-ast (>= 1.7.0, < 2.0)
39
42
  ruby-progressbar (~> 1.7)
40
- unicode-display_width (>= 1.4.0, < 1.7)
41
- rubocop-performance (1.4.0)
42
- rubocop (>= 0.71.0)
43
- rubocop-rspec (1.33.0)
44
- rubocop (>= 0.60.0)
45
- ruby-progressbar (1.10.1)
46
- ruby_parser (3.13.1)
47
- sexp_processor (~> 4.9)
48
- sexp_processor (4.12.1)
49
- unicode-display_width (1.6.0)
43
+ unicode-display_width (>= 1.4.0, < 3.0)
44
+ rubocop-ast (1.8.0)
45
+ parser (>= 3.0.1.1)
46
+ rubocop-performance (1.11.4)
47
+ rubocop (>= 1.7.0, < 2.0)
48
+ rubocop-ast (>= 0.4.0)
49
+ rubocop-rake (0.6.0)
50
+ rubocop (~> 1.0)
51
+ rubocop-rspec (2.4.0)
52
+ rubocop (~> 1.0)
53
+ rubocop-ast (>= 1.1.0)
54
+ ruby-progressbar (1.11.0)
55
+ ruby_parser (3.16.0)
56
+ sexp_processor (~> 4.15, >= 4.15.1)
57
+ sexp_processor (4.15.3)
58
+ unicode-display_width (2.0.0)
50
59
 
51
60
  PLATFORMS
52
61
  ruby
@@ -59,7 +68,8 @@ DEPENDENCIES
59
68
  rspec
60
69
  rubocop
61
70
  rubocop-performance
71
+ rubocop-rake
62
72
  rubocop-rspec
63
73
 
64
74
  BUNDLED WITH
65
- 2.0.1
75
+ 2.2.24
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/lite-memoize.svg)](http://badge.fury.io/rb/lite-memoize)
4
4
  [![Build Status](https://travis-ci.org/drexed/lite-memoize.svg?branch=master)](https://travis-ci.org/drexed/lite-memoize)
5
5
 
6
- Lite::Memoize provides an API for caching and memoizing locally expensive calculations including those with parameters. The flexible API allows you to memoize results using alias, class, instance, or mixin level cache.
6
+ Lite::Memoize provides an API for caching and memoizing locally expensive calculations including those with parameters. The flexible API allows you to memoize results using alias, class, instance, table, or variable based cache.
7
7
 
8
8
  **NOTE:** If you are coming from `ActiveMemoize`, please read the [port](#port) section.
9
9
 
@@ -28,13 +28,14 @@ Or install it yourself as:
28
28
  * [Alias](#alias)
29
29
  * [Klass](#klass)
30
30
  * [Instance](#instance)
31
- * [Mixin](#mixin)
31
+ * [Table](#table)
32
+ * [Variable](#variable)
32
33
  * [Benchmarks](#benchmarks)
33
34
  * [Port](#port)
34
35
 
35
36
  ## Alias
36
37
 
37
- Alias level memoization is the fastest of the available methods, and provides a decent level
38
+ Alias based memoization is the fastest of the available methods, and provides a decent level
38
39
  of control. It's the only one that can also be used to memoize class level methods. Method
39
40
  arguments are automatically watched to cache dynamic values.
40
41
 
@@ -58,7 +59,9 @@ class Movies
58
59
  HTTP.get('http://movies.com/any')
59
60
  end
60
61
 
62
+ # NOTE: memoize must be before alias
61
63
  memoize :random
64
+ alias rando random
62
65
 
63
66
  def search(title)
64
67
  HTTP.get("http://movies.com?title=#{title}")
@@ -78,7 +81,7 @@ Movies.clear_cache #=> New value
78
81
 
79
82
  ## Klass
80
83
 
81
- Class level memoization is the quickest way to get up without polluting your class with new methods.
84
+ Class based memoization is the quickest way to get up without polluting your class with new methods.
82
85
  It's perfect for short lived or non-altering items like `activerecord` objects.
83
86
 
84
87
  You can only cache results without access to any information about the `store`.
@@ -91,7 +94,9 @@ class Movies
91
94
  HTTP.get('http://movies.com/any')
92
95
  end
93
96
 
97
+ # NOTE: memoize must be before alias
94
98
  memoize :random
99
+ alias rando random
95
100
 
96
101
  def search(title)
97
102
  HTTP.get("http://movies.com?title=#{title}")
@@ -104,11 +109,11 @@ end
104
109
 
105
110
  ## Instance
106
111
 
107
- Instance level memoization is the slowest of the available methods, but it provides
112
+ Instance based memoization is the slowest of the available methods, but it provides
108
113
  the most amount of flexibility and control. It's very useful for creating services or things
109
- where control is paramount like clearing the cache or dumping it to JSON. Please read the spec
110
- suite to see all available actions. Method arguments are automatically watched to cache dynamic
111
- values.
114
+ where control is paramount like clearing the cache or dumping it to JSON. Method arguments
115
+ are automatically watched to cache dynamic values. Please read the spec suite to see all
116
+ available actions.
112
117
 
113
118
  You can access almost all methods in the `instance.rb` file.
114
119
 
@@ -128,6 +133,8 @@ class Movies
128
133
  cache['random'] ||= HTTP.get("http://movies.com/any?type=#{type}")
129
134
  end
130
135
 
136
+ alias rando random
137
+
131
138
  # NOTE: Arguments in the memoize method are optional
132
139
  def search(title)
133
140
  cache.memoize(as: :find, args: [title], reload: !cache.empty?) do
@@ -138,21 +145,47 @@ class Movies
138
145
  end
139
146
  ```
140
147
 
141
- ## Mixin
148
+ ## Variable
149
+
150
+ Variable based memoization is lean but pollute the class with variables.
151
+
152
+ ```ruby
153
+ class Movies
154
+ include Lite::Memoize::Variable
155
+
156
+ def all
157
+ memoize(:all) { HTTP.get("http://movies.com/all") }
158
+ end
159
+
160
+ alias full all
161
+
162
+ # NOTE: Arguments in the memoize method are optional with the exception of method name
163
+ def search(title)
164
+ memoize(:find, args: [title], reload: false) do
165
+ HTTP.get("http://movies.com?title=#{title}")
166
+ end
167
+ end
168
+
169
+ end
170
+ ```
171
+
172
+ ## Table
142
173
 
143
- Mixin level memoization is the leanest of the available methods, and provides a decent level
174
+ Table based memoization is the leanest of the available methods, and provides a decent level
144
175
  of control. Useful when you want to keep your class light weight.
145
176
 
146
177
  You can access all methods to the `Hash` class.
147
178
 
148
179
  ```ruby
149
180
  class Movies
150
- include Lite::Memoize::Mixin
181
+ include Lite::Memoize::Table
151
182
 
152
183
  def all
153
184
  memoize(:all) { HTTP.get("http://movies.com/all") }
154
185
  end
155
186
 
187
+ alias full all
188
+
156
189
  # NOTE: Arguments in the memoize method are optional with the exception of method name
157
190
  def search(title)
158
191
  memoize(:find, args: [title], reload: false) do
@@ -165,9 +198,9 @@ end
165
198
 
166
199
  ## Benchmarks
167
200
 
168
- The classes ranked from fastest to slowest are `Alias`, `Mixin`, `Klass`, and `Instance`.
201
+ The classes ranked from fastest to slowest are `Alias`, `Table`, `Klass`, `Variable`, and `Instance`.
169
202
 
170
- View all how it compares to other libs by running the [benchmarks](https://github.com/drexed/lite-statistics/tree/master/benchmarks).
203
+ View how each compares to other libs by running the [benchmarks](https://github.com/drexed/lite-statistics/tree/master/benchmarks).
171
204
 
172
205
  ## Port
173
206
 
data/benchmarks/base.rb CHANGED
@@ -41,9 +41,19 @@ class LiteMemoizeKlassCache
41
41
 
42
42
  end
43
43
 
44
- class LiteMemoizeMixinCache
44
+ class LiteMemoizeTableCache
45
45
 
46
- include Lite::Memoize::Mixin
46
+ include Lite::Memoize::Table
47
+
48
+ def randomize
49
+ memoize(:randomize) { rand(1..99) }
50
+ end
51
+
52
+ end
53
+
54
+ class LiteMemoizeVariableCache
55
+
56
+ include Lite::Memoize::Variable
47
57
 
48
58
  def randomize
49
59
  memoize(:randomize) { rand(1..99) }
data/benchmarks/memery.rb CHANGED
@@ -15,8 +15,9 @@ end
15
15
 
16
16
  klass_a = LiteMemoizeInstanceCache.new
17
17
  klass_b = LiteMemoizeKlassCache.new
18
- klass_c = LiteMemoizeMixinCache.new
18
+ klass_c = LiteMemoizeTableCache.new
19
19
  klass_d = LiteMemoizeAliasCache.new
20
+ klass_e = LiteMemoizeVariableCache.new
20
21
  klass_z = MemeryCache.new
21
22
 
22
23
  Benchmark.ips do |x|
@@ -28,7 +29,7 @@ Benchmark.ips do |x|
28
29
  klass_b.randomize
29
30
  end
30
31
 
31
- x.report('LM.mixin') do
32
+ x.report('LM.table') do
32
33
  klass_c.randomize
33
34
  end
34
35
 
@@ -36,6 +37,10 @@ Benchmark.ips do |x|
36
37
  klass_d.randomize
37
38
  end
38
39
 
40
+ x.report('LM.variable') do
41
+ klass_e.randomize
42
+ end
43
+
39
44
  x.report('Memery') do
40
45
  klass_z.randomize
41
46
  end
data/benchmarks/memist.rb CHANGED
@@ -15,8 +15,9 @@ end
15
15
 
16
16
  klass_a = LiteMemoizeInstanceCache.new
17
17
  klass_b = LiteMemoizeKlassCache.new
18
- klass_c = LiteMemoizeMixinCache.new
18
+ klass_c = LiteMemoizeTableCache.new
19
19
  klass_d = LiteMemoizeAliasCache.new
20
+ klass_e = LiteMemoizeVariableCache.new
20
21
  klass_z = MemistCache.new
21
22
 
22
23
  Benchmark.ips do |x|
@@ -28,7 +29,7 @@ Benchmark.ips do |x|
28
29
  klass_b.randomize
29
30
  end
30
31
 
31
- x.report('LM.mixin') do
32
+ x.report('LM.table') do
32
33
  klass_c.randomize
33
34
  end
34
35
 
@@ -36,6 +37,10 @@ Benchmark.ips do |x|
36
37
  klass_d.randomize
37
38
  end
38
39
 
40
+ x.report('LM.variable') do
41
+ klass_e.randomize
42
+ end
43
+
39
44
  x.report('Memist') do
40
45
  klass_z.randomize
41
46
  end
@@ -15,8 +15,9 @@ end
15
15
 
16
16
  klass_a = LiteMemoizeInstanceCache.new
17
17
  klass_b = LiteMemoizeKlassCache.new
18
- klass_c = LiteMemoizeMixinCache.new
18
+ klass_c = LiteMemoizeTableCache.new
19
19
  klass_d = LiteMemoizeAliasCache.new
20
+ klass_e = LiteMemoizeVariableCache.new
20
21
  klass_z = MemoitCache.new
21
22
 
22
23
  Benchmark.ips do |x|
@@ -28,7 +29,7 @@ Benchmark.ips do |x|
28
29
  klass_b.randomize
29
30
  end
30
31
 
31
- x.report('LM.mixin') do
32
+ x.report('LM.table') do
32
33
  klass_c.randomize
33
34
  end
34
35
 
@@ -40,5 +41,9 @@ Benchmark.ips do |x|
40
41
  klass_z.randomize
41
42
  end
42
43
 
44
+ x.report('LM.variable') do
45
+ klass_e.randomize
46
+ end
47
+
43
48
  x.compare!
44
49
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = MemoistCache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoist') do
42
47
  klass_z.randomize
43
48
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = Memoist2Cache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoist2') do
42
47
  klass_z.randomize
43
48
  end
data/benchmarks/memoit.rb CHANGED
@@ -13,8 +13,9 @@ end
13
13
 
14
14
  klass_a = LiteMemoizeInstanceCache.new
15
15
  klass_b = LiteMemoizeKlassCache.new
16
- klass_c = LiteMemoizeMixinCache.new
16
+ klass_c = LiteMemoizeTableCache.new
17
17
  klass_d = LiteMemoizeAliasCache.new
18
+ klass_e = LiteMemoizeVariableCache.new
18
19
  klass_z = MemoitCache.new
19
20
 
20
21
  Benchmark.ips do |x|
@@ -26,7 +27,7 @@ Benchmark.ips do |x|
26
27
  klass_b.randomize
27
28
  end
28
29
 
29
- x.report('LM.mixin') do
30
+ x.report('LM.table') do
30
31
  klass_c.randomize
31
32
  end
32
33
 
@@ -34,6 +35,10 @@ Benchmark.ips do |x|
34
35
  klass_d.randomize
35
36
  end
36
37
 
38
+ x.report('LM.variable') do
39
+ klass_e.randomize
40
+ end
41
+
37
42
  x.report('Memoit') do
38
43
  klass_z.randomize
39
44
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = MemoizableCache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoizable') do
42
47
  klass_z.randomize
43
48
  end
@@ -17,8 +17,9 @@ end
17
17
 
18
18
  klass_a = LiteMemoizeInstanceCache.new
19
19
  klass_b = LiteMemoizeKlassCache.new
20
- klass_c = LiteMemoizeMixinCache.new
20
+ klass_c = LiteMemoizeTableCache.new
21
21
  klass_d = LiteMemoizeAliasCache.new
22
+ klass_e = LiteMemoizeVariableCache.new
22
23
  klass_z = MemoizerCache.new
23
24
 
24
25
  Benchmark.ips do |x|
@@ -30,7 +31,7 @@ Benchmark.ips do |x|
30
31
  klass_b.randomize
31
32
  end
32
33
 
33
- x.report('LM.mixin') do
34
+ x.report('LM.table') do
34
35
  klass_c.randomize
35
36
  end
36
37
 
@@ -38,6 +39,10 @@ Benchmark.ips do |x|
38
39
  klass_d.randomize
39
40
  end
40
41
 
42
+ x.report('LM.variable') do
43
+ klass_e.randomize
44
+ end
45
+
41
46
  x.report('Memoizer') do
42
47
  klass_z.randomize
43
48
  end
data/lib/lite/memoize.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- %w[version alias mixin instance klass].each do |name|
3
+ %w[version alias table instance klass variable].each do |name|
4
4
  require "lite/memoize/#{name}"
5
5
  end
@@ -34,13 +34,13 @@ module Lite
34
34
  end
35
35
  end
36
36
 
37
+ alias clear clear_cache
37
38
  alias flush_cache clear_cache
38
39
 
39
40
  end
40
41
 
41
42
  class << self
42
43
 
43
- # rubocop:disable Lint/NestedMethodDefinition
44
44
  def extended(extender)
45
45
  Lite::Memoize::Alias.memoist_eval(extender) do
46
46
  return if singleton_class.method_defined?(:memoized_methods)
@@ -50,7 +50,6 @@ module Lite
50
50
  end
51
51
  end
52
52
  end
53
- # rubocop:enable Lint/NestedMethodDefinition
54
53
 
55
54
  def memoized_ivar_for(method_name, as = nil)
56
55
  "@#{memoized_prefix(as)}_#{escape_punctuation(method_name)}"
@@ -4,7 +4,7 @@ module Lite
4
4
  module Memoize
5
5
  class Instance
6
6
 
7
- include Lite::Memoize::Mixin
7
+ include Lite::Memoize::Table
8
8
 
9
9
  def initialize; end
10
10
 
@@ -26,11 +26,11 @@ module Lite
26
26
  store.delete(key)
27
27
  end
28
28
 
29
- # :nocov:
29
+ # rubocop:disable Style/ExplicitBlockArgument
30
30
  def each
31
31
  store.each { |key, val| yield(key, val) }
32
32
  end
33
- # :nocov:
33
+ # rubocop:enable Style/ExplicitBlockArgument
34
34
 
35
35
  def empty?
36
36
  store.empty?
@@ -4,7 +4,7 @@ module Lite
4
4
  module Memoize
5
5
  module Klass
6
6
 
7
- include Lite::Memoize::Mixin
7
+ include Lite::Memoize::Table
8
8
 
9
9
  def memoize(method_name, as: nil)
10
10
  inner_method = instance_method(method_name)
@@ -12,10 +12,12 @@ module Lite
12
12
  define_method(method_name) do |*args|
13
13
  key = self.class.caller_key(args, as: as || method_name)
14
14
 
15
- self.class.store[key] ||= inner_method.bind(self).call(*args)
15
+ self.class.store[key] ||= inner_method.bind_call(self, *args)
16
16
  end
17
17
  end
18
18
 
19
+ public :caller_key
20
+
19
21
  end
20
22
  end
21
23
  end
@@ -2,22 +2,12 @@
2
2
 
3
3
  module Lite
4
4
  module Memoize
5
- module Mixin
5
+ module Table
6
6
 
7
7
  def store
8
8
  @_memoized_methods ||= {}
9
9
  end
10
10
 
11
- def caller_key(block, as: nil)
12
- name = as ? [as] : block.source_location
13
- return name.concat(block) if block.is_a?(Array)
14
-
15
- block.binding.local_variables.each_with_object(name) do |local_name, array|
16
- array << local_name
17
- array << block.binding.local_variable_get(local_name)
18
- end
19
- end
20
-
21
11
  def memoize(method_name, args: nil, reload: false)
22
12
  key = "#{method_name}:#{args}"
23
13
 
@@ -28,6 +18,18 @@ module Lite
28
18
  end
29
19
  end
30
20
 
21
+ private
22
+
23
+ def caller_key(block, as: nil)
24
+ name = as ? [as] : block.source_location
25
+ return name.concat(block) if block.is_a?(Array)
26
+
27
+ block.binding.local_variables.each_with_object(name) do |local_name, array|
28
+ array << local_name
29
+ array << block.binding.local_variable_get(local_name)
30
+ end
31
+ end
32
+
31
33
  end
32
34
  end
33
35
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Memoize
5
+ module Variable
6
+
7
+ def memoize(method_name, args: nil, reload: false)
8
+ key = "#{method_name}#{args}"
9
+ var = "@#{key.gsub(/\W/, '') || key}"
10
+ return instance_variable_get(var) if !reload && instance_variable_defined?(var)
11
+
12
+ instance_variable_set(var, yield)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Memoize
5
5
 
6
- VERSION ||= '1.0.1'
6
+ VERSION = '1.1.0'
7
7
 
8
8
  end
9
9
  end
data/lite-memoize.gemspec CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  )
26
26
  else
27
27
  raise 'RubyGems 2.0 or newer is required to protect against ' \
28
- 'public gem pushes.'
28
+ 'public gem pushes.'
29
29
  end
30
30
 
31
31
  # Specify which files should be added to the gem when it is released.
@@ -43,5 +43,6 @@ Gem::Specification.new do |spec|
43
43
  spec.add_development_dependency 'rspec'
44
44
  spec.add_development_dependency 'rubocop'
45
45
  spec.add_development_dependency 'rubocop-performance'
46
+ spec.add_development_dependency 'rubocop-rake'
46
47
  spec.add_development_dependency 'rubocop-rspec'
47
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-memoize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-18 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rubocop-rspec
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +122,7 @@ dependencies:
108
122
  - - ">="
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
111
- description:
125
+ description:
112
126
  email:
113
127
  - j.gomez@drexed.com
114
128
  executables: []
@@ -143,14 +157,15 @@ files:
143
157
  - lib/lite/memoize/alias.rb
144
158
  - lib/lite/memoize/instance.rb
145
159
  - lib/lite/memoize/klass.rb
146
- - lib/lite/memoize/mixin.rb
160
+ - lib/lite/memoize/table.rb
161
+ - lib/lite/memoize/variable.rb
147
162
  - lib/lite/memoize/version.rb
148
163
  - lite-memoize.gemspec
149
164
  homepage: http://drexed.github.io/lite-memoize
150
165
  licenses:
151
166
  - MIT
152
167
  metadata: {}
153
- post_install_message:
168
+ post_install_message:
154
169
  rdoc_options: []
155
170
  require_paths:
156
171
  - lib
@@ -165,8 +180,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
180
  - !ruby/object:Gem::Version
166
181
  version: '0'
167
182
  requirements: []
168
- rubygems_version: 3.0.4
169
- signing_key:
183
+ rubygems_version: 3.2.24
184
+ signing_key:
170
185
  specification_version: 4
171
186
  summary: Cache and memoization helpers for ruby Ruby classes
172
187
  test_files: []