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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bcf13cc28b811ea862a086b245ba3afea86508a
4
- data.tar.gz: 7d7d3978c858911c49f4e12d43c530f2f11113f9
3
+ metadata.gz: f86a8254a0324977fdc2e8bbc3445c381dd2cd66
4
+ data.tar.gz: d4565c572fcf4cc4c56674cd2439dc07400a1378
5
5
  SHA512:
6
- metadata.gz: 9dc8905bb4337c2e32de615e9455a1bef0ce47956ca0797f5095a423c16d64366eaf60541d6393d1b9863588018b12786548280333bc78a8c9397ef116aa83e4
7
- data.tar.gz: 67168fe1cff5a1be817cdbb45054874c20abe4e71714c9b6c42703543c7bb6193cb66d754f1c27a0b08169bc84743c127c869e606700e58b863ab7636e7fec76
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.call # => 42
20
- A.new.call # => 42
21
- A.new.call # => 42
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
- ## 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.
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 version of method, just make it like this:
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).
@@ -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
- def memoize(method_name)
18
- prepend_mememaster_module!
19
- define_memoized_method!(method_name)
20
- end
22
+ module ClassMethods
23
+ def memoize(method_name)
24
+ prepend_mememaster_module!
25
+ define_memoized_method!(method_name)
26
+ end
21
27
 
22
- private
28
+ private
23
29
 
24
- def prepend_mememaster_module!
25
- return if defined?(@_mememaster_module)
26
- @_mememaster_module = Module.new
27
- prepend @_mememaster_module
28
- end
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
- def define_memoized_method!(method_name)
31
- mod_id = @_mememaster_module.object_id
32
- visibility = Mememaster.method_visibility(self, method_name)
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
- @_mememaster_module.module_eval do
35
- define_method(method_name) do |*args|
36
- @_mememaster_memoized_values ||= {}
40
+ @_mememaster_module.module_eval do
41
+ define_method(method_name) do |*args|
42
+ @_mememaster_memoized_values ||= {}
37
43
 
38
- key = [method_name, mod_id].join("_").to_sym
39
- store = @_mememaster_memoized_values[key] ||= {}
44
+ key = [method_name, mod_id].join("_").to_sym
45
+ store = @_mememaster_memoized_values[key] ||= {}
40
46
 
41
- if store.key?(args)
42
- store[args]
43
- else
44
- store[args] = super(*args)
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
- send(visibility, method_name)
59
+ module InstanceMethods
60
+ def clear_mememaster_cache!
61
+ @_mememaster_memoized_values = {}
49
62
  end
50
63
  end
51
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mememaster
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov