mememaster 0.2.0 → 0.3.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 +17 -6
- data/lib/mememaster.rb +36 -23
- data/lib/mememaster/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: f86a8254a0324977fdc2e8bbc3445c381dd2cd66
|
4
|
+
data.tar.gz: d4565c572fcf4cc4c56674cd2439dc07400a1378
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef80f3eda31417cb2434d19931b2791c2fc88b1ff9edf928aa43431b11494b70469f2a4d27bb0bfe8368d0e3676dda32aacd9d03277c561d4259c04967dbeee7
|
7
|
+
data.tar.gz: 30756beeec5adcd58b712d8cae08fd9f28f6963b5100b251684322bfb85b7e5e6d579454fb1624c74464d8ceed8106520b605516e6e3003d528d383739ca4bd6
|
data/README.md
CHANGED
@@ -2,8 +2,12 @@
|
|
2
2
|
|
3
3
|
Mememaster is a gem for memoization in Ruby. Example:
|
4
4
|
|
5
|
+
## Usage
|
6
|
+
|
5
7
|
```ruby
|
6
8
|
class A
|
9
|
+
include Mememaster
|
10
|
+
|
7
11
|
memoize def call
|
8
12
|
puts "calculating"
|
9
13
|
42
|
@@ -16,17 +20,18 @@ class A
|
|
16
20
|
# memoize :call
|
17
21
|
end
|
18
22
|
|
19
|
-
A.new
|
20
|
-
|
21
|
-
|
23
|
+
a = A.new
|
24
|
+
a.call # => 42
|
25
|
+
a.call # => 42
|
26
|
+
a.call # => 42
|
22
27
|
|
23
28
|
# Text will be printed only once.
|
24
29
|
```
|
25
30
|
|
26
|
-
##
|
27
|
-
Mememaster 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:
|
31
|
+
## Difference with other gems
|
32
|
+
Mememaster 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.
|
28
33
|
|
29
|
-
The other key difference is that it doesn't change method's signature (no extra `reload` param). If you need unmemoized
|
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:
|
30
35
|
|
31
36
|
```ruby
|
32
37
|
memoize def users
|
@@ -38,6 +43,12 @@ def get_users
|
|
38
43
|
end
|
39
44
|
```
|
40
45
|
|
46
|
+
Alternatively, you can clear the whole instance's cache:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
a.clear_mememaster_cache!
|
50
|
+
```
|
51
|
+
|
41
52
|
## License
|
42
53
|
|
43
54
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/mememaster.rb
CHANGED
@@ -3,6 +3,11 @@
|
|
3
3
|
require "mememaster/version"
|
4
4
|
|
5
5
|
module Mememaster
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
base.include(InstanceMethods)
|
9
|
+
end
|
10
|
+
|
6
11
|
def self.method_visibility(klass, method_name)
|
7
12
|
case
|
8
13
|
when klass.private_method_defined?(method_name)
|
@@ -14,38 +19,46 @@ module Mememaster
|
|
14
19
|
end
|
15
20
|
end
|
16
21
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
22
|
+
module ClassMethods
|
23
|
+
def memoize(method_name)
|
24
|
+
prepend_mememaster_module!
|
25
|
+
define_memoized_method!(method_name)
|
26
|
+
end
|
21
27
|
|
22
|
-
|
28
|
+
private
|
23
29
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
def prepend_mememaster_module!
|
31
|
+
return if defined?(@_mememaster_module)
|
32
|
+
@_mememaster_module = Module.new
|
33
|
+
prepend @_mememaster_module
|
34
|
+
end
|
29
35
|
|
30
|
-
|
31
|
-
|
32
|
-
|
36
|
+
def define_memoized_method!(method_name)
|
37
|
+
mod_id = @_mememaster_module.object_id
|
38
|
+
visibility = Mememaster.method_visibility(self, method_name)
|
33
39
|
|
34
|
-
|
35
|
-
|
36
|
-
|
40
|
+
@_mememaster_module.module_eval do
|
41
|
+
define_method(method_name) do |*args|
|
42
|
+
@_mememaster_memoized_values ||= {}
|
37
43
|
|
38
|
-
|
39
|
-
|
44
|
+
key = [method_name, mod_id].join("_").to_sym
|
45
|
+
store = @_mememaster_memoized_values[key] ||= {}
|
40
46
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
47
|
+
if store.key?(args)
|
48
|
+
store[args]
|
49
|
+
else
|
50
|
+
store[args] = super(*args)
|
51
|
+
end
|
45
52
|
end
|
53
|
+
|
54
|
+
send(visibility, method_name)
|
46
55
|
end
|
56
|
+
end
|
57
|
+
end
|
47
58
|
|
48
|
-
|
59
|
+
module InstanceMethods
|
60
|
+
def clear_mememaster_cache!
|
61
|
+
@_mememaster_memoized_values = {}
|
49
62
|
end
|
50
63
|
end
|
51
64
|
end
|
data/lib/mememaster/version.rb
CHANGED