mememaster 0.1.1 → 0.2.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 +15 -0
- data/lib/mememaster.rb +16 -2
- 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: 2bcf13cc28b811ea862a086b245ba3afea86508a
|
4
|
+
data.tar.gz: 7d7d3978c858911c49f4e12d43c530f2f11113f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dc8905bb4337c2e32de615e9455a1bef0ce47956ca0797f5095a423c16d64366eaf60541d6393d1b9863588018b12786548280333bc78a8c9397ef116aa83e4
|
7
|
+
data.tar.gz: 67168fe1cff5a1be817cdbb45054874c20abe4e71714c9b6c42703543c7bb6193cb66d754f1c27a0b08169bc84743c127c869e606700e58b863ab7636e7fec76
|
data/README.md
CHANGED
@@ -23,6 +23,21 @@ A.new.call # => 42
|
|
23
23
|
# Text will be printed only once.
|
24
24
|
```
|
25
25
|
|
26
|
+
## Features
|
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: by default, 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
|
+
|
29
|
+
The other key difference is that it doesn't change method's signature (no extra `reload` param). If you need unmemoized version of method, just make it like this:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
memoize def users
|
33
|
+
get_users
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_users
|
37
|
+
# ...
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
26
41
|
## License
|
27
42
|
|
28
43
|
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,17 @@
|
|
3
3
|
require "mememaster/version"
|
4
4
|
|
5
5
|
module Mememaster
|
6
|
+
def self.method_visibility(klass, method_name)
|
7
|
+
case
|
8
|
+
when klass.private_method_defined?(method_name)
|
9
|
+
:private
|
10
|
+
when klass.protected_method_defined?(method_name)
|
11
|
+
:protected
|
12
|
+
else
|
13
|
+
:public
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
6
17
|
def memoize(method_name)
|
7
18
|
prepend_mememaster_module!
|
8
19
|
define_memoized_method!(method_name)
|
@@ -17,13 +28,14 @@ module Mememaster
|
|
17
28
|
end
|
18
29
|
|
19
30
|
def define_memoized_method!(method_name)
|
20
|
-
|
31
|
+
mod_id = @_mememaster_module.object_id
|
32
|
+
visibility = Mememaster.method_visibility(self, method_name)
|
21
33
|
|
22
34
|
@_mememaster_module.module_eval do
|
23
35
|
define_method(method_name) do |*args|
|
24
36
|
@_mememaster_memoized_values ||= {}
|
25
37
|
|
26
|
-
key = [method_name,
|
38
|
+
key = [method_name, mod_id].join("_").to_sym
|
27
39
|
store = @_mememaster_memoized_values[key] ||= {}
|
28
40
|
|
29
41
|
if store.key?(args)
|
@@ -32,6 +44,8 @@ module Mememaster
|
|
32
44
|
store[args] = super(*args)
|
33
45
|
end
|
34
46
|
end
|
47
|
+
|
48
|
+
send(visibility, method_name)
|
35
49
|
end
|
36
50
|
end
|
37
51
|
end
|
data/lib/mememaster/version.rb
CHANGED