memery 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '0854715beed165845002189a1574152c3f79024e'
4
- data.tar.gz: 8fa7be482b75e8c37caadd35d339dbeefbb670fc
3
+ metadata.gz: 7bea94e1b42ea3945a0d149eb6842d0c8cd5fb9b
4
+ data.tar.gz: af0473e8921f36a362a37e5b4b610489b1106f75
5
5
  SHA512:
6
- metadata.gz: cb9fd46d872ba116c7d69583292ae1f2a36eee6d2095d699a5790fd5aa9dde9d663f1e80b248549ff2d70b8e9e28a0c9a5f071e4b5288f68e1e84e5d3eced1bf
7
- data.tar.gz: 671d518ef8430157f8f30e3ea544ef45f9e472d54a8600345d30c8a4029c2782c79c36d93985f48f17718099a9aa9c91e2a59df677319e372f9a4eae2dab4361
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. Example:
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
- The other key difference is that it doesn't change method's signature (no extra `reload` param). If you need unmemoized result of method, just create an unmemoized version like this:
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).
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Memery
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov