memery 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +40 -4
- data/lib/memery.rb +3 -1
- data/lib/memery/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bea94e1b42ea3945a0d149eb6842d0c8cd5fb9b
|
4
|
+
data.tar.gz: af0473e8921f36a362a37e5b4b610489b1106f75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d87994d56fcaa1b6cde160140b262f96e873ea4663e53e322f1de79661b8ca1c63b5cee5e52a3328245272e9b982d94ac447a01f54ce3dc0681904660ef78a69
|
7
|
+
data.tar.gz: f227cea7e655a3568c046d01d31e56d11dc9dc176261bf906c2b4cdf97d65f9195eb21922ef31da69e6912d59d4cb2012f7d798c44650fec725c1a2c9a7787c5
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Memery
|
2
2
|
|
3
|
-
Memery is a gem for memoization in Ruby.
|
3
|
+
Memery is a gem for memoization in Ruby.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
@@ -24,14 +24,42 @@ a = A.new
|
|
24
24
|
a.call # => 42
|
25
25
|
a.call # => 42
|
26
26
|
a.call # => 42
|
27
|
-
|
28
27
|
# Text will be printed only once.
|
28
|
+
|
29
|
+
a.call { 1 } # => 42
|
30
|
+
# Will print because passing a block disables memoization
|
29
31
|
```
|
30
32
|
|
31
33
|
## Difference with other gems
|
32
|
-
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
|
34
|
+
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:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class A
|
38
|
+
include Memery
|
33
39
|
|
34
|
-
|
40
|
+
memoize def x(param)
|
41
|
+
param
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class B < A
|
46
|
+
memoize def x(param)
|
47
|
+
super(2) * param
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
b = B.new
|
52
|
+
b.x(1) # => 2
|
53
|
+
b.x(2) # => 4
|
54
|
+
b.x(3) # => 6
|
55
|
+
|
56
|
+
b.instance_variable_get(:@_memery_memoized_values)
|
57
|
+
# => {:x_70318201388120=>{[1]=>2, [2]=>4, [3]=>6}, :x_70318184636620=>{[2]=>2}}
|
58
|
+
```
|
59
|
+
|
60
|
+
Note how both method's return values are cached separately and don't interfere with each other.
|
61
|
+
|
62
|
+
The other key difference is that it doesn't change method's signature (no extra `reload` param). If you need to get unmemoized result of method, just create an unmemoized version like this:
|
35
63
|
|
36
64
|
```ruby
|
37
65
|
memoize def users
|
@@ -49,6 +77,14 @@ Alternatively, you can clear the whole instance's cache:
|
|
49
77
|
a.clear_memery_cache!
|
50
78
|
```
|
51
79
|
|
80
|
+
Finally, you can provide a block:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
a.users {}
|
84
|
+
```
|
85
|
+
|
86
|
+
However, this solution is kind of hacky.
|
87
|
+
|
52
88
|
## License
|
53
89
|
|
54
90
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/memery.rb
CHANGED
@@ -38,7 +38,9 @@ module Memery
|
|
38
38
|
visibility = Memery.method_visibility(self, method_name)
|
39
39
|
|
40
40
|
@_memery_module.module_eval do
|
41
|
-
define_method(method_name) do |*args|
|
41
|
+
define_method(method_name) do |*args, &block|
|
42
|
+
return super(*args, &block) if block
|
43
|
+
|
42
44
|
@_memery_memoized_values ||= {}
|
43
45
|
|
44
46
|
key = [method_name, mod_id].join("_").to_sym
|
data/lib/memery/version.rb
CHANGED