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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +47 -3
- data/lib/memo/it.rb +4 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5d763007910553014d30928bc189551cf02b5d9
|
4
|
+
data.tar.gz: 2f5a1da6d1f6761703d2d05a74d309778d10761e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fa8dae350b101a3990e2c322251424dd37c292ccf6b474013a319c93d5f08bf0f9a71274d7891e31b36ef5c7bf6f25ae090726050b9cef0e8c00ad275cf2434
|
7
|
+
data.tar.gz: ccf969487455241a7ea4623ad261852f744325ebf5a4f9047ba9f1ac13b37669508cf130972abd100253eed0d151baa64fc94e175bad986403427a93f1a2c16a
|
data/CHANGELOG.md
CHANGED
@@ -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. [](https://travis-ci.org/phoet/memo-it)
|
4
4
|
|
5
|
-
##
|
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
|
-
|
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
|
-
|
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
|
data/lib/memo/it.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Memo
|
2
|
-
VERSION = '0.
|
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
|
-
|
41
|
-
|
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.
|
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-
|
11
|
+
date: 2017-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|