memery 0.6.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.editorconfig +9 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +6 -0
- data/CHANGELOG.md +24 -0
- data/Gemfile +2 -0
- data/README.md +38 -0
- data/lib/memery.rb +6 -4
- data/lib/memery/version.rb +1 -1
- data/memery.gemspec +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a2db1b80dc2da0ad38dea657f09a1f4a029017d5c67610e27a71af3aeb9aef4
|
4
|
+
data.tar.gz: b2e8bc1c499d2462096b0b4045c41de110ea8ea27b5750e23cbdf1a74c57d9f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 813057c9b895b2b789a75c48ea49366a9a951494e90d07a21a628b46ed2bda35e3f9a8407a5efcbf0bf73712ddaff6002d8685e083d6b767e8c52d3102b65743
|
7
|
+
data.tar.gz: 24a8b9ef7c10341336e301f6e9c09644101fd7c593f9535483cdbf8360ac8ac003d94869200303269fb8d3c6b7103675f7691a7c5d46b31acfd74f5f40b19a59
|
data/.editorconfig
ADDED
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
## [Unreleased]
|
2
|
+
|
3
|
+
## [1.0.0] - 2018-08-31
|
4
|
+
### Added
|
5
|
+
- Add `:condition` option for `.memoize` method ([@AlexWayfer]) [#7]
|
6
|
+
|
7
|
+
## [0.6.0] - 2018-04-20
|
8
|
+
### Added
|
9
|
+
- Readme example for memoizing class methods. ([@AlexWayfer]) [#3]
|
10
|
+
- Memery raises `ArgumentError` if method is not defined when you call `memoize`.
|
11
|
+
|
12
|
+
## [0.5.0] - 2017-16-12
|
13
|
+
- Initial public version.
|
14
|
+
|
15
|
+
[0.5.0]: https://github.com/tycooon/memery/tree/v0.5.0
|
16
|
+
[0.6.0]: https://github.com/tycooon/memery/compare/v0.5.0...v0.6.0
|
17
|
+
[1.0.0]: https://github.com/tycooon/memery/compare/v0.6.0...v1.0.0
|
18
|
+
[Unreleased]: https://github.com/tycooon/memery/compare/v0.6.0...HEAD
|
19
|
+
|
20
|
+
[@tycooon]: https://github.com/tycooon
|
21
|
+
[@AlexWayfer]: https://github.com/AlexWayfer
|
22
|
+
|
23
|
+
[#3]: https://github.com/tycooon/memery/pull/3
|
24
|
+
[#7]: https://github.com/tycooon/memery/pull/7
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -81,6 +81,41 @@ B.call # => 42
|
|
81
81
|
# Text will be printed only once.
|
82
82
|
```
|
83
83
|
|
84
|
+
For conditional memoization:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class A
|
88
|
+
include Memery
|
89
|
+
|
90
|
+
attr_accessor :environment
|
91
|
+
|
92
|
+
def call
|
93
|
+
puts "calculating"
|
94
|
+
42
|
95
|
+
end
|
96
|
+
|
97
|
+
memoize :call, condition: -> { environment == 'production' }
|
98
|
+
end
|
99
|
+
|
100
|
+
a = A.new
|
101
|
+
a.environment = 'development'
|
102
|
+
a.call # => 42
|
103
|
+
# calculating
|
104
|
+
a.call # => 42
|
105
|
+
# calculating
|
106
|
+
a.call # => 42
|
107
|
+
# calculating
|
108
|
+
# Text will be printed every time because result of condition block is `false`.
|
109
|
+
|
110
|
+
a.environment = 'production'
|
111
|
+
a.call # => 42
|
112
|
+
# calculating
|
113
|
+
a.call # => 42
|
114
|
+
a.call # => 42
|
115
|
+
# Text will be printed only once because there is memoization
|
116
|
+
# with `true` result of condition block.
|
117
|
+
```
|
118
|
+
|
84
119
|
## Difference with other gems
|
85
120
|
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:
|
86
121
|
|
@@ -136,6 +171,9 @@ a.users {}
|
|
136
171
|
|
137
172
|
However, this solution is kind of hacky.
|
138
173
|
|
174
|
+
## Contributing
|
175
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tycooon/memery.
|
176
|
+
|
139
177
|
## License
|
140
178
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
141
179
|
|
data/lib/memery.rb
CHANGED
@@ -20,9 +20,9 @@ module Memery
|
|
20
20
|
end
|
21
21
|
|
22
22
|
module ClassMethods
|
23
|
-
def memoize(method_name)
|
23
|
+
def memoize(method_name, condition: nil)
|
24
24
|
prepend_memery_module!
|
25
|
-
define_memoized_method!(method_name)
|
25
|
+
define_memoized_method!(method_name, condition: condition)
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
@@ -33,14 +33,16 @@ module Memery
|
|
33
33
|
prepend @_memery_module
|
34
34
|
end
|
35
35
|
|
36
|
-
def define_memoized_method!(method_name)
|
36
|
+
def define_memoized_method!(method_name, condition: nil)
|
37
37
|
mod_id = @_memery_module.object_id
|
38
38
|
visibility = Memery.method_visibility(self, method_name)
|
39
39
|
raise ArgumentError, "Method #{method_name} is not defined on #{self}" unless visibility
|
40
40
|
|
41
41
|
@_memery_module.module_eval do
|
42
42
|
define_method(method_name) do |*args, &block|
|
43
|
-
|
43
|
+
if block || (condition && !instance_exec(&condition))
|
44
|
+
return super(*args, &block)
|
45
|
+
end
|
44
46
|
|
45
47
|
@_memery_memoized_values ||= {}
|
46
48
|
|
data/lib/memery/version.rb
CHANGED
data/memery.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuri Smirnov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -115,10 +115,12 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
+
- ".editorconfig"
|
118
119
|
- ".gitignore"
|
119
120
|
- ".rspec"
|
120
121
|
- ".rubocop.yml"
|
121
122
|
- ".travis.yml"
|
123
|
+
- CHANGELOG.md
|
122
124
|
- Gemfile
|
123
125
|
- LICENSE.txt
|
124
126
|
- README.md
|
@@ -146,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
148
|
version: '0'
|
147
149
|
requirements: []
|
148
150
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.7.
|
151
|
+
rubygems_version: 2.7.7
|
150
152
|
signing_key:
|
151
153
|
specification_version: 4
|
152
154
|
summary: A gem for memoization.
|