memo-it 0.3.2 → 0.4.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/README.md +47 -3
  4. data/lib/memo/it.rb +4 -11
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af982b8ebf877a0f128499d126d2b40bf407894a
4
- data.tar.gz: f4c35d3df4468bb778eb075b2fd8ff640313e676
3
+ metadata.gz: a5d763007910553014d30928bc189551cf02b5d9
4
+ data.tar.gz: 2f5a1da6d1f6761703d2d05a74d309778d10761e
5
5
  SHA512:
6
- metadata.gz: 807f598a93993f69e680e2b9b33f01881e6c378ca126cccdba0cef8f8f4e1d4fd14324b6a7c9369cf60a36706ead8859c47eb2fe32796ed37d685cb2682d894e
7
- data.tar.gz: c46ce4c40fb9d19da3ff5c34ed76cbce9ba51f40b83dc21dc650638feeb1789c7846a81bf7a186b5540551538aa8a29da7cef1b529a872d61e12985245527625
6
+ metadata.gz: 0fa8dae350b101a3990e2c322251424dd37c292ccf6b474013a319c93d5f08bf0f9a71274d7891e31b36ef5c7bf6f25ae090726050b9cef0e8c00ad275cf2434
7
+ data.tar.gz: ccf969487455241a7ea4623ad261852f744325ebf5a4f9047ba9f1ac13b37669508cf130972abd100253eed0d151baa64fc94e175bad986403427a93f1a2c16a
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.4.0 ([list of commits](https://github.com/phoet/memo-it/compare/v0.3.2...v0.4.0))
4
+
5
+ * separate instance and class level caching (see [issue #7](https://github.com/phoet/memo-it/pull/11) for details)
6
+
3
7
  ## 0.3.2 ([list of commits](https://github.com/phoet/memo-it/compare/v0.3.1...v0.3.2))
4
8
 
5
9
  * cache on the class level (see [issue #7](https://github.com/phoet/memo-it/pull/9) for details)
data/README.md CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  Clever memoization helper that uses Ruby internals instead of meta-programming. [![Build Status](https://travis-ci.org/phoet/memo-it.svg?branch=master)](https://travis-ci.org/phoet/memo-it)
4
4
 
5
- ## Usage
5
+ ## Using Memo::It
6
+
7
+ ### Basic Usage
6
8
 
7
9
  Requiring the gem will add a `memo` method to the `Object` class so that you can just use it like so:
8
10
 
@@ -16,7 +18,47 @@ Requiring the gem will add a `memo` method to the `Object` class so that you can
16
18
  end
17
19
  ```
18
20
 
19
- In case you want to memoize something that has parameters, memo-it will just use all local variables in scope to determine the memoization:
21
+ ### Instance vs Class memoization
22
+
23
+ Per default, the memoization will be done on an instance level,
24
+ so every instance of an object will have it's own memoization namespace:
25
+
26
+ ```ruby
27
+ class Loader
28
+ def initialize(url)
29
+ @url = url
30
+ end
31
+
32
+ def content
33
+ memo do
34
+ # fetch the url content from the web
35
+ end
36
+ end
37
+ end
38
+
39
+ issues_loader = Loader.new('https://github.com/phoet/memo-it/issues')
40
+ pulls_loader = Loader.new('https://github.com/phoet/memo-it/pulls')
41
+
42
+ issues_loader.content # load & memoize the issues
43
+ pulls_loader.content # load & memoize the pulls
44
+ ```
45
+
46
+ But you can also memoize on a global/class scope.
47
+ This is needed if you want to use Memo::It in dynamically instanciated objects like Rails helpers:
48
+
49
+ ```ruby
50
+ module WebHelper
51
+ def content(url)
52
+ Memo::It.memo do
53
+ # fetch the url content from the web
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ ### Parameters as scopes
60
+
61
+ In case you want to memoize something that has parameters, Memo::It will just use all local variables in scope to determine the memoization:
20
62
 
21
63
  ```ruby
22
64
  def load_repo(name = 'memo-it')
@@ -61,7 +103,9 @@ To be symmetric, it's also possible to define one or more parameters through the
61
103
  end
62
104
  ```
63
105
 
64
- In case you would like to disable memoization (ie testing) you can disable Memo::It:
106
+ ### Turning it on and off
107
+
108
+ In case you would like to disable memoization (ie. for testing) you can disable Memo::It:
65
109
 
66
110
  ```ruby
67
111
  # enabled is default
@@ -1,5 +1,5 @@
1
1
  module Memo
2
- VERSION = '0.3.2'
2
+ VERSION = '0.4.0'
3
3
 
4
4
  @enabled = true
5
5
 
@@ -15,14 +15,6 @@ module Memo
15
15
  @enabled = false
16
16
  end
17
17
 
18
- def self.cache
19
- @cache ||= {}
20
- end
21
-
22
- def self.clear
23
- cache.clear
24
- end
25
-
26
18
  module It
27
19
  def memo(only: [], except: [], &block)
28
20
  only = Array(only)
@@ -37,8 +29,9 @@ module Memo
37
29
  keys = block.source_location
38
30
  keys << key_names.flat_map { |name| [name, block.binding.local_variable_get(name)] }
39
31
 
40
- return Memo.cache[keys] if Memo.enabled? && Memo.cache.key?(keys)
41
- Memo.cache[keys] = yield
32
+ @_memo_it ||= {}
33
+ return @_memo_it[keys] if Memo.enabled? && @_memo_it.key?(keys)
34
+ @_memo_it[keys] = yield
42
35
  end
43
36
  end
44
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memo-it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - phoet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-22 00:00:00.000000000 Z
11
+ date: 2017-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler