memery 0.6.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +9 -0
- data/.gitignore +1 -1
- data/.rspec +1 -0
- data/.rubocop.yml +6 -0
- data/.travis.yml +3 -11
- data/CHANGELOG.md +61 -0
- data/Gemfile.lock +131 -0
- data/README.md +118 -3
- data/Rakefile +7 -1
- data/benchmark.rb +46 -0
- data/lib/memery.rb +77 -29
- data/lib/memery/version.rb +1 -1
- data/memery.gemspec +8 -1
- metadata +67 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9deb1312add2edf331f0e4b5ff2b196c95f7de899d6ae15ee01e9d4e43fca950
|
4
|
+
data.tar.gz: f228b03ab8ed0a286075a13f045a53337780c75d4eb9b015b285dfc3c48c3913
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8da63cbd319881210877b295f5791132ae80848758954e4ea0615baaf82bcee5fa06c5b93b4dd9a524af7efabad749a65caf428680673e7f68dea625fc806bf7
|
7
|
+
data.tar.gz: 57806aadb6b7bd61520d068e1c3244836dc6c89c4ce550f8881adfee7eea139b6a07e5dddc4d5de072917693dd34596b49db5114716c9f0dc792023879d69c38
|
data/.editorconfig
ADDED
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -3,23 +3,15 @@ language: ruby
|
|
3
3
|
sudo: false
|
4
4
|
|
5
5
|
rvm:
|
6
|
-
- 2.2
|
7
|
-
- 2.3
|
8
|
-
- 2.4
|
9
6
|
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
9
|
+
- 3.0
|
10
10
|
- ruby-head
|
11
11
|
|
12
12
|
before_install: gem install bundler
|
13
13
|
|
14
|
-
env: SUITE="rspec"
|
15
|
-
|
16
|
-
script: bundle exec $SUITE
|
17
|
-
|
18
14
|
matrix:
|
19
15
|
fast_finish: true
|
20
|
-
# Only run RuboCop on the latest Ruby
|
21
|
-
include:
|
22
|
-
- rvm: 2.5
|
23
|
-
env: SUITE="rubocop"
|
24
16
|
allow_failures:
|
25
17
|
- rvm: ruby-head
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
## [Unreleased]
|
2
|
+
|
3
|
+
## [1.4.0] - 2021-03-15
|
4
|
+
### Fixed
|
5
|
+
- Fix compatibility with `ActiveSupport::Concern`. ([@tycooon] and [@AlexWayfer]) [#26]
|
6
|
+
|
7
|
+
## [1.3.0] - 2020-02-10
|
8
|
+
### Added
|
9
|
+
- Allow memoization after including module with Memery. ([@AlexWayfer]) [#23]
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
- Make `memoize` return the method name to allow chaining. ([@JelF]) [#22]
|
13
|
+
|
14
|
+
### Fixed
|
15
|
+
- Fix warnings in Ruby 2.7. ([@AlexWayfer]) [#19], [#25]
|
16
|
+
|
17
|
+
## [1.2.0] - 2019-10-19
|
18
|
+
### Added
|
19
|
+
- Add `:ttl` option for `memoize` method ([@AlexWayfer]) [#11]
|
20
|
+
- Add benchmark script ([@AlexWayfer]) [#14]
|
21
|
+
- Add `.memoized?` method ([@AlexWayfer]) [#17]
|
22
|
+
|
23
|
+
## [1.1.0] - 2019-08-05
|
24
|
+
### Fixed
|
25
|
+
- Optimize speed and memory for cached values returns. ([@AlexWayfer]) [#10]
|
26
|
+
|
27
|
+
## [1.0.0] - 2018-08-31
|
28
|
+
### Added
|
29
|
+
- Add `:condition` option for `.memoize` method. ([@AlexWayfer]) [#7]
|
30
|
+
|
31
|
+
## [0.6.0] - 2018-04-20
|
32
|
+
### Added
|
33
|
+
- Readme example for memoizing class methods. ([@AlexWayfer]) [#3]
|
34
|
+
- Memery raises `ArgumentError` if method is not defined when you call `memoize`.
|
35
|
+
|
36
|
+
## [0.5.0] - 2017-06-12
|
37
|
+
- Initial public version.
|
38
|
+
|
39
|
+
[0.5.0]: https://github.com/tycooon/memery/tree/v0.5.0
|
40
|
+
[0.6.0]: https://github.com/tycooon/memery/compare/v0.5.0...v0.6.0
|
41
|
+
[1.0.0]: https://github.com/tycooon/memery/compare/v0.6.0...v1.0.0
|
42
|
+
[1.1.0]: https://github.com/tycooon/memery/compare/v1.0.0...v1.1.0
|
43
|
+
[1.2.0]: https://github.com/tycooon/memery/compare/v1.1.0...v1.2.0
|
44
|
+
[1.3.0]: https://github.com/tycooon/memery/compare/v1.2.0...v1.3.0
|
45
|
+
[Unreleased]: https://github.com/tycooon/memery/compare/v1.3.0...HEAD
|
46
|
+
|
47
|
+
[@tycooon]: https://github.com/tycooon
|
48
|
+
[@AlexWayfer]: https://github.com/AlexWayfer
|
49
|
+
[@JelF]: https://github.com/JelF
|
50
|
+
|
51
|
+
[#3]: https://github.com/tycooon/memery/pull/3
|
52
|
+
[#7]: https://github.com/tycooon/memery/pull/7
|
53
|
+
[#10]: https://github.com/tycooon/memery/pull/10
|
54
|
+
[#11]: https://github.com/tycooon/memery/pull/11
|
55
|
+
[#14]: https://github.com/tycooon/memery/pull/14
|
56
|
+
[#17]: https://github.com/tycooon/memery/pull/17
|
57
|
+
[#19]: https://github.com/tycooon/memery/pull/19
|
58
|
+
[#22]: https://github.com/tycooon/memery/pull/22
|
59
|
+
[#23]: https://github.com/tycooon/memery/pull/23
|
60
|
+
[#25]: https://github.com/tycooon/memery/pull/25
|
61
|
+
[#26]: https://github.com/tycooon/memery/pull/26
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
memery (1.4.0)
|
5
|
+
ruby2_keywords (~> 0.0.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (6.1.3)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 1.6, < 2)
|
13
|
+
minitest (>= 5.1)
|
14
|
+
tzinfo (~> 2.0)
|
15
|
+
zeitwerk (~> 2.3)
|
16
|
+
ast (2.4.2)
|
17
|
+
benchmark-ips (2.8.4)
|
18
|
+
benchmark-memory (0.1.2)
|
19
|
+
memory_profiler (~> 0.9)
|
20
|
+
coderay (1.1.3)
|
21
|
+
concurrent-ruby (1.1.8)
|
22
|
+
coveralls (0.7.2)
|
23
|
+
multi_json (~> 1.3)
|
24
|
+
rest-client (= 1.6.7)
|
25
|
+
simplecov (>= 0.7)
|
26
|
+
term-ansicolor (= 1.2.2)
|
27
|
+
thor (= 0.18.1)
|
28
|
+
diff-lcs (1.4.4)
|
29
|
+
docile (1.3.5)
|
30
|
+
i18n (1.8.9)
|
31
|
+
concurrent-ruby (~> 1.0)
|
32
|
+
memory_profiler (0.9.14)
|
33
|
+
method_source (1.0.0)
|
34
|
+
mime-types (3.3.1)
|
35
|
+
mime-types-data (~> 3.2015)
|
36
|
+
mime-types-data (3.2021.0225)
|
37
|
+
minitest (5.14.4)
|
38
|
+
multi_json (1.15.0)
|
39
|
+
parallel (1.20.1)
|
40
|
+
parser (3.0.0.0)
|
41
|
+
ast (~> 2.4.1)
|
42
|
+
pry (0.14.0)
|
43
|
+
coderay (~> 1.1)
|
44
|
+
method_source (~> 1.0)
|
45
|
+
rack (2.2.3)
|
46
|
+
rainbow (3.0.0)
|
47
|
+
rake (13.0.3)
|
48
|
+
regexp_parser (2.1.1)
|
49
|
+
rest-client (1.6.7)
|
50
|
+
mime-types (>= 1.16)
|
51
|
+
rexml (3.2.4)
|
52
|
+
rspec (3.10.0)
|
53
|
+
rspec-core (~> 3.10.0)
|
54
|
+
rspec-expectations (~> 3.10.0)
|
55
|
+
rspec-mocks (~> 3.10.0)
|
56
|
+
rspec-core (3.10.1)
|
57
|
+
rspec-support (~> 3.10.0)
|
58
|
+
rspec-expectations (3.10.1)
|
59
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
+
rspec-support (~> 3.10.0)
|
61
|
+
rspec-mocks (3.10.2)
|
62
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
63
|
+
rspec-support (~> 3.10.0)
|
64
|
+
rspec-support (3.10.2)
|
65
|
+
rubocop (1.11.0)
|
66
|
+
parallel (~> 1.10)
|
67
|
+
parser (>= 3.0.0.0)
|
68
|
+
rainbow (>= 2.2.2, < 4.0)
|
69
|
+
regexp_parser (>= 1.8, < 3.0)
|
70
|
+
rexml
|
71
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
72
|
+
ruby-progressbar (~> 1.7)
|
73
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
74
|
+
rubocop-ast (1.4.1)
|
75
|
+
parser (>= 2.7.1.5)
|
76
|
+
rubocop-config-umbrellio (1.11.0.40)
|
77
|
+
rubocop (= 1.11.0)
|
78
|
+
rubocop-performance (= 1.10.0)
|
79
|
+
rubocop-rails (= 2.9.1)
|
80
|
+
rubocop-rake (= 0.5.1)
|
81
|
+
rubocop-rspec (= 2.2.0)
|
82
|
+
rubocop-sequel (= 0.2.0)
|
83
|
+
rubocop-performance (1.10.0)
|
84
|
+
rubocop (>= 0.90.0, < 2.0)
|
85
|
+
rubocop-ast (>= 0.4.0)
|
86
|
+
rubocop-rails (2.9.1)
|
87
|
+
activesupport (>= 4.2.0)
|
88
|
+
rack (>= 1.1)
|
89
|
+
rubocop (>= 0.90.0, < 2.0)
|
90
|
+
rubocop-rake (0.5.1)
|
91
|
+
rubocop
|
92
|
+
rubocop-rspec (2.2.0)
|
93
|
+
rubocop (~> 1.0)
|
94
|
+
rubocop-ast (>= 1.1.0)
|
95
|
+
rubocop-sequel (0.2.0)
|
96
|
+
rubocop (~> 1.0)
|
97
|
+
ruby-progressbar (1.11.0)
|
98
|
+
ruby2_keywords (0.0.4)
|
99
|
+
simplecov (0.21.2)
|
100
|
+
docile (~> 1.1)
|
101
|
+
simplecov-html (~> 0.11)
|
102
|
+
simplecov_json_formatter (~> 0.1)
|
103
|
+
simplecov-html (0.12.3)
|
104
|
+
simplecov_json_formatter (0.1.2)
|
105
|
+
term-ansicolor (1.2.2)
|
106
|
+
tins (~> 0.8)
|
107
|
+
thor (0.18.1)
|
108
|
+
tins (0.13.2)
|
109
|
+
tzinfo (2.0.4)
|
110
|
+
concurrent-ruby (~> 1.0)
|
111
|
+
unicode-display_width (2.0.0)
|
112
|
+
zeitwerk (2.4.2)
|
113
|
+
|
114
|
+
PLATFORMS
|
115
|
+
ruby
|
116
|
+
|
117
|
+
DEPENDENCIES
|
118
|
+
activesupport
|
119
|
+
benchmark-ips
|
120
|
+
benchmark-memory
|
121
|
+
bundler
|
122
|
+
coveralls
|
123
|
+
memery!
|
124
|
+
pry
|
125
|
+
rake
|
126
|
+
rspec
|
127
|
+
rubocop-config-umbrellio
|
128
|
+
simplecov
|
129
|
+
|
130
|
+
BUNDLED WITH
|
131
|
+
2.2.11
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ def user
|
|
8
8
|
end
|
9
9
|
```
|
10
10
|
|
11
|
-
However, this approach
|
11
|
+
However, this approach doesn't work if calculated result can be `nil` or `false` or in case the method is using arguments. You will also require extra `begin`/`end` lines in case your method requires multiple lines:
|
12
12
|
|
13
13
|
```ruby
|
14
14
|
def user
|
@@ -31,7 +31,8 @@ end
|
|
31
31
|
```
|
32
32
|
|
33
33
|
## Installation
|
34
|
-
|
34
|
+
|
35
|
+
Simply add `gem "memery"` to your Gemfile.
|
35
36
|
|
36
37
|
## Usage
|
37
38
|
|
@@ -61,6 +62,25 @@ a.call { 1 } # => 42
|
|
61
62
|
# Will print because passing a block disables memoization
|
62
63
|
```
|
63
64
|
|
65
|
+
Methods with arguments are supported and the memoization will be done based on arguments using an internal hash. So this will work as expected:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class A
|
69
|
+
include Memery
|
70
|
+
|
71
|
+
memoize def call(arg1, arg2)
|
72
|
+
puts "calculating"
|
73
|
+
arg1 + arg2
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
a = A.new
|
78
|
+
a.call(1, 5) # => 6
|
79
|
+
a.call(2, 15) # => 17
|
80
|
+
a.call(1, 5) # => 6
|
81
|
+
# Text will be printed only twice, once per unique argument list.
|
82
|
+
```
|
83
|
+
|
64
84
|
For class methods:
|
65
85
|
|
66
86
|
```ruby
|
@@ -81,8 +101,97 @@ B.call # => 42
|
|
81
101
|
# Text will be printed only once.
|
82
102
|
```
|
83
103
|
|
104
|
+
For conditional memoization:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
class A
|
108
|
+
include Memery
|
109
|
+
|
110
|
+
attr_accessor :environment
|
111
|
+
|
112
|
+
def call
|
113
|
+
puts "calculating"
|
114
|
+
42
|
115
|
+
end
|
116
|
+
|
117
|
+
memoize :call, condition: -> { environment == 'production' }
|
118
|
+
end
|
119
|
+
|
120
|
+
a = A.new
|
121
|
+
a.environment = 'development'
|
122
|
+
a.call # => 42
|
123
|
+
# calculating
|
124
|
+
a.call # => 42
|
125
|
+
# calculating
|
126
|
+
a.call # => 42
|
127
|
+
# calculating
|
128
|
+
# Text will be printed every time because result of condition block is `false`.
|
129
|
+
|
130
|
+
a.environment = 'production'
|
131
|
+
a.call # => 42
|
132
|
+
# calculating
|
133
|
+
a.call # => 42
|
134
|
+
a.call # => 42
|
135
|
+
# Text will be printed only once because there is memoization
|
136
|
+
# with `true` result of condition block.
|
137
|
+
```
|
138
|
+
|
139
|
+
For memoization with time-to-live:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
class A
|
143
|
+
include Memery
|
144
|
+
|
145
|
+
def call
|
146
|
+
puts "calculating"
|
147
|
+
42
|
148
|
+
end
|
149
|
+
|
150
|
+
memoize :call, ttl: 3 # seconds
|
151
|
+
end
|
152
|
+
|
153
|
+
a = A.new
|
154
|
+
a.call # => 42
|
155
|
+
# calculating
|
156
|
+
a.call # => 42
|
157
|
+
a.call # => 42
|
158
|
+
# Text will be printed again only after 3 seconds of time-to-live.
|
159
|
+
# 3 seconds later...
|
160
|
+
a.call # => 42
|
161
|
+
# calculating
|
162
|
+
a.call # => 42
|
163
|
+
a.call # => 42
|
164
|
+
# another 3 seconds later...
|
165
|
+
a.call # => 42
|
166
|
+
# calculating
|
167
|
+
a.call # => 42
|
168
|
+
a.call # => 42
|
169
|
+
```
|
170
|
+
|
171
|
+
Check if method is memoized:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
class A
|
175
|
+
include Memery
|
176
|
+
|
177
|
+
memoize def call
|
178
|
+
puts "calculating"
|
179
|
+
42
|
180
|
+
end
|
181
|
+
|
182
|
+
def execute
|
183
|
+
puts "non-memoized"
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
a = A.new
|
188
|
+
|
189
|
+
a.memoized?(:call) # => true
|
190
|
+
a.memoized?(:execute) # => false
|
191
|
+
```
|
192
|
+
|
84
193
|
## Difference with other gems
|
85
|
-
Memery is very similar to [Memoist](https://github.com/matthewrudy/memoist). The difference is that it doesn't override methods, instead it uses Ruby 2 `Module.prepend` feature. This approach is cleaner and it allows subclasses' methods to work properly: if you redefine a memoized method in a subclass, it's not memoized by default, but you can memoize it normally (without using awkward `identifier: ` argument) and it will just work:
|
194
|
+
Memery is very similar to [Memoist](https://github.com/matthewrudy/memoist). The difference is that it doesn't override methods, instead it uses Ruby 2 `Module.prepend` feature. This approach is cleaner (for example you are able to inspect the original method body using `method(:x).super_method.source`) and it allows subclasses' methods to work properly: if you redefine a memoized method in a subclass, it's not memoized by default, but you can memoize it normally (without using awkward `identifier: ` argument) and it will just work:
|
86
195
|
|
87
196
|
```ruby
|
88
197
|
class A
|
@@ -136,8 +245,14 @@ a.users {}
|
|
136
245
|
|
137
246
|
However, this solution is kind of hacky.
|
138
247
|
|
248
|
+
## Contributing
|
249
|
+
|
250
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tycooon/memery.
|
251
|
+
|
139
252
|
## License
|
253
|
+
|
140
254
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
141
255
|
|
142
256
|
## Author
|
257
|
+
|
143
258
|
Created by Yuri Smirnov.
|
data/Rakefile
CHANGED
@@ -2,7 +2,13 @@
|
|
2
2
|
|
3
3
|
require "bundler/gem_tasks"
|
4
4
|
require "rspec/core/rake_task"
|
5
|
+
require "rubocop/rake_task"
|
5
6
|
|
6
7
|
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
RuboCop::RakeTask.new(:lint)
|
7
9
|
|
8
|
-
task default:
|
10
|
+
task default: %i[lint spec]
|
11
|
+
|
12
|
+
task :benchmark do
|
13
|
+
require_relative "./benchmark"
|
14
|
+
end
|
data/benchmark.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
Bundler.setup
|
5
|
+
|
6
|
+
require "benchmark"
|
7
|
+
require "benchmark/ips"
|
8
|
+
require "benchmark/memory"
|
9
|
+
|
10
|
+
puts "```ruby"
|
11
|
+
puts File.read(__FILE__)
|
12
|
+
puts "```"
|
13
|
+
puts
|
14
|
+
puts "### Output"
|
15
|
+
puts
|
16
|
+
puts "```"
|
17
|
+
|
18
|
+
require_relative "lib/memery"
|
19
|
+
|
20
|
+
class Foo
|
21
|
+
class << self
|
22
|
+
include Memery
|
23
|
+
|
24
|
+
def base_find(char)
|
25
|
+
("a".."k").find { |letter| letter == char }
|
26
|
+
end
|
27
|
+
|
28
|
+
memoize def find_new(char)
|
29
|
+
base_find(char)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_memery
|
35
|
+
Foo.find_new("d")
|
36
|
+
end
|
37
|
+
|
38
|
+
Benchmark.ips do |x|
|
39
|
+
x.report("test_memery") { test_memery }
|
40
|
+
end
|
41
|
+
|
42
|
+
Benchmark.memory do |x|
|
43
|
+
x.report("test_memery") { 100.times { test_memery } }
|
44
|
+
end
|
45
|
+
|
46
|
+
puts "```"
|
data/lib/memery.rb
CHANGED
@@ -1,62 +1,110 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "ruby2_keywords"
|
4
|
+
|
3
5
|
require "memery/version"
|
4
6
|
|
5
7
|
module Memery
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
class << self
|
9
|
+
def monotonic_clock
|
10
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
OUR_BLOCK = lambda do
|
15
|
+
extend(ClassMethods)
|
16
|
+
include(InstanceMethods)
|
17
|
+
extend ModuleMethods if instance_of?(Module)
|
9
18
|
end
|
10
19
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
20
|
+
private_constant :OUR_BLOCK
|
21
|
+
|
22
|
+
module ModuleMethods
|
23
|
+
def included(base = nil, &block)
|
24
|
+
if base.nil? && block
|
25
|
+
super do
|
26
|
+
instance_exec(&block)
|
27
|
+
instance_exec(&OUR_BLOCK)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
base.instance_exec(&OUR_BLOCK)
|
31
|
+
end
|
19
32
|
end
|
20
33
|
end
|
21
34
|
|
35
|
+
extend ModuleMethods
|
36
|
+
|
22
37
|
module ClassMethods
|
23
|
-
def memoize(method_name)
|
38
|
+
def memoize(method_name, condition: nil, ttl: nil)
|
24
39
|
prepend_memery_module!
|
25
|
-
define_memoized_method!(method_name)
|
40
|
+
define_memoized_method!(method_name, condition: condition, ttl: ttl)
|
41
|
+
method_name
|
42
|
+
end
|
43
|
+
|
44
|
+
def memoized?(method_name)
|
45
|
+
return false unless defined?(@_memery_module)
|
46
|
+
|
47
|
+
@_memery_module.method_defined?(method_name) ||
|
48
|
+
@_memery_module.private_method_defined?(method_name)
|
26
49
|
end
|
27
50
|
|
28
51
|
private
|
29
52
|
|
30
53
|
def prepend_memery_module!
|
31
54
|
return if defined?(@_memery_module)
|
32
|
-
@_memery_module = Module.new
|
55
|
+
@_memery_module = Module.new { extend MemoizationModule }
|
33
56
|
prepend @_memery_module
|
34
57
|
end
|
35
58
|
|
36
|
-
def define_memoized_method!(
|
37
|
-
|
38
|
-
|
39
|
-
|
59
|
+
def define_memoized_method!(*args, **kwargs)
|
60
|
+
@_memery_module.public_send __method__, self, *args, **kwargs
|
61
|
+
end
|
62
|
+
|
63
|
+
module MemoizationModule
|
64
|
+
def define_memoized_method!(klass, method_name, condition: nil, ttl: nil)
|
65
|
+
method_key = "#{method_name}_#{object_id}"
|
40
66
|
|
41
|
-
|
42
|
-
define_method(method_name) do |*args, &block|
|
43
|
-
return super(*args, &block) if block
|
67
|
+
original_visibility = method_visibility(klass, method_name)
|
44
68
|
|
45
|
-
|
69
|
+
define_method method_name do |*args, &block|
|
70
|
+
if block || (condition && !instance_exec(&condition))
|
71
|
+
return super(*args, &block)
|
72
|
+
end
|
46
73
|
|
47
|
-
|
48
|
-
store = @_memery_memoized_values[key] ||= {}
|
74
|
+
store = (@_memery_memoized_values ||= {})[method_key] ||= {}
|
49
75
|
|
50
|
-
if store.key?(args)
|
51
|
-
store[args]
|
52
|
-
|
53
|
-
store[args] = super(*args)
|
76
|
+
if store.key?(args) &&
|
77
|
+
(ttl.nil? || Memery.monotonic_clock <= store[args][:time] + ttl)
|
78
|
+
return store[args][:result]
|
54
79
|
end
|
80
|
+
|
81
|
+
result = super(*args)
|
82
|
+
@_memery_memoized_values[method_key][args] =
|
83
|
+
{ result: result, time: Memery.monotonic_clock }
|
84
|
+
result
|
55
85
|
end
|
56
86
|
|
57
|
-
|
87
|
+
ruby2_keywords method_name
|
88
|
+
|
89
|
+
send original_visibility, method_name
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def method_visibility(klass, method_name)
|
95
|
+
if klass.private_method_defined?(method_name)
|
96
|
+
:private
|
97
|
+
elsif klass.protected_method_defined?(method_name)
|
98
|
+
:protected
|
99
|
+
elsif klass.public_method_defined?(method_name)
|
100
|
+
:public
|
101
|
+
else
|
102
|
+
raise ArgumentError, "Method #{method_name} is not defined on #{klass}"
|
103
|
+
end
|
58
104
|
end
|
59
105
|
end
|
106
|
+
|
107
|
+
private_constant :MemoizationModule
|
60
108
|
end
|
61
109
|
|
62
110
|
module InstanceMethods
|
data/lib/memery/version.rb
CHANGED
data/memery.gemspec
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
lib = File.expand_path("
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
5
|
require "memery/version"
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
|
+
spec.required_ruby_version = ">= 2.5.0"
|
9
|
+
|
8
10
|
spec.name = "memery"
|
9
11
|
spec.version = Memery::VERSION
|
10
12
|
spec.authors = ["Yuri Smirnov"]
|
@@ -20,6 +22,11 @@ Gem::Specification.new do |spec|
|
|
20
22
|
end
|
21
23
|
spec.require_paths = ["lib"]
|
22
24
|
|
25
|
+
spec.add_runtime_dependency "ruby2_keywords", "~> 0.0.2"
|
26
|
+
|
27
|
+
spec.add_development_dependency "activesupport"
|
28
|
+
spec.add_development_dependency "benchmark-ips"
|
29
|
+
spec.add_development_dependency "benchmark-memory"
|
23
30
|
spec.add_development_dependency "bundler"
|
24
31
|
spec.add_development_dependency "coveralls"
|
25
32
|
spec.add_development_dependency "pry"
|
metadata
CHANGED
@@ -1,15 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuri Smirnov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ruby2_keywords
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: benchmark-ips
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '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'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: benchmark-memory
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
13
69
|
- !ruby/object:Gem::Dependency
|
14
70
|
name: bundler
|
15
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,14 +171,18 @@ executables: []
|
|
115
171
|
extensions: []
|
116
172
|
extra_rdoc_files: []
|
117
173
|
files:
|
174
|
+
- ".editorconfig"
|
118
175
|
- ".gitignore"
|
119
176
|
- ".rspec"
|
120
177
|
- ".rubocop.yml"
|
121
178
|
- ".travis.yml"
|
179
|
+
- CHANGELOG.md
|
122
180
|
- Gemfile
|
181
|
+
- Gemfile.lock
|
123
182
|
- LICENSE.txt
|
124
183
|
- README.md
|
125
184
|
- Rakefile
|
185
|
+
- benchmark.rb
|
126
186
|
- lib/memery.rb
|
127
187
|
- lib/memery/version.rb
|
128
188
|
- memery.gemspec
|
@@ -130,7 +190,7 @@ homepage: https://github.com/tycooon/memery
|
|
130
190
|
licenses:
|
131
191
|
- MIT
|
132
192
|
metadata: {}
|
133
|
-
post_install_message:
|
193
|
+
post_install_message:
|
134
194
|
rdoc_options: []
|
135
195
|
require_paths:
|
136
196
|
- lib
|
@@ -138,16 +198,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
198
|
requirements:
|
139
199
|
- - ">="
|
140
200
|
- !ruby/object:Gem::Version
|
141
|
-
version:
|
201
|
+
version: 2.5.0
|
142
202
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
203
|
requirements:
|
144
204
|
- - ">="
|
145
205
|
- !ruby/object:Gem::Version
|
146
206
|
version: '0'
|
147
207
|
requirements: []
|
148
|
-
|
149
|
-
|
150
|
-
signing_key:
|
208
|
+
rubygems_version: 3.2.14
|
209
|
+
signing_key:
|
151
210
|
specification_version: 4
|
152
211
|
summary: A gem for memoization.
|
153
212
|
test_files: []
|