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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a20b9bf0a78cc66a775814caa716fc736da9501
4
- data.tar.gz: 2ec9a91b28d1b0e755b277ba9f180ee918e39d1f
3
+ metadata.gz: 2bcf13cc28b811ea862a086b245ba3afea86508a
4
+ data.tar.gz: 7d7d3978c858911c49f4e12d43c530f2f11113f9
5
5
  SHA512:
6
- metadata.gz: d00fcef90e24154d2bd8866dbe3c6cbe92223830b66b98206ac8ef1d1d41a0bf2b80c353805cf098156f7520bfa31d9e3ec5b7bc3dda4055117ffabcbae86c09
7
- data.tar.gz: aa75520401b9ecb110106268ad8eee777783cd18599a951e080c6f6372e188b98fce2fb03664bf7d63e0f2507efc95a4c612c221237a6bc1ceee475bd6b3a99b
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).
@@ -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
- mod = @_mememaster_module
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, mod.object_id].join("_").to_sym
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mememaster
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mememaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov