memo-it 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +7 -3
  3. data/README.md +24 -0
  4. data/lib/memo/it.rb +6 -3
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a5d763007910553014d30928bc189551cf02b5d9
4
- data.tar.gz: 2f5a1da6d1f6761703d2d05a74d309778d10761e
2
+ SHA256:
3
+ metadata.gz: cad87d82c561a1ab4b92282a41dbe43bdf4e4cca81c0896062afde0dd94a5e25
4
+ data.tar.gz: 14fd9a7779219ce974712639a0cb131a94ea9675f0eb7232488219d050270460
5
5
  SHA512:
6
- metadata.gz: 0fa8dae350b101a3990e2c322251424dd37c292ccf6b474013a319c93d5f08bf0f9a71274d7891e31b36ef5c7bf6f25ae090726050b9cef0e8c00ad275cf2434
7
- data.tar.gz: ccf969487455241a7ea4623ad261852f744325ebf5a4f9047ba9f1ac13b37669508cf130972abd100253eed0d151baa64fc94e175bad986403427a93f1a2c16a
6
+ metadata.gz: 6e3041c9707b52eaa229f02f17732695eaa5d46a563c09ed9f46ce6bb1b557cab9449682f67e492af049e864e41b77afe3039466f71e7e8b149c7945b2e04dec
7
+ data.tar.gz: 222825cee2c1a4e57a519ebf9af1886a28cee4f9a6ff6a0cb47cffdecf5d2a04e00637c762731081677caeb46131b1c31f77a10f5aa1c67db6fc1dc8a49011cc
@@ -1,16 +1,20 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.5.0 ([list of commits](https://github.com/phoet/memo-it/compare/v0.4.0...v0.5.0))
4
+
5
+ * separate instance and class level caching (see [PR](https://github.com/phoet/memo-it/pull/15) for details) <3 @serioushaircut
6
+
3
7
  ## 0.4.0 ([list of commits](https://github.com/phoet/memo-it/compare/v0.3.2...v0.4.0))
4
8
 
5
- * separate instance and class level caching (see [issue #7](https://github.com/phoet/memo-it/pull/11) for details)
9
+ * separate instance and class level caching (see [PR](https://github.com/phoet/memo-it/pull/11) for details) <3 @alto
6
10
 
7
11
  ## 0.3.2 ([list of commits](https://github.com/phoet/memo-it/compare/v0.3.1...v0.3.2))
8
12
 
9
- * cache on the class level (see [issue #7](https://github.com/phoet/memo-it/pull/9) for details)
13
+ * cache on the class level (see [PR](https://github.com/phoet/memo-it/pull/9) for details)
10
14
 
11
15
  ## 0.3.1 ([list of commits](https://github.com/phoet/memo-it/compare/v0.3.0...v0.3.1))
12
16
 
13
- * simplify key generation (see [issue #7](https://github.com/phoet/memo-it/pull/7) for details)
17
+ * simplify key generation (see [PR](https://github.com/phoet/memo-it/pull/7) for details)
14
18
 
15
19
  ## 0.3.0 ([list of commits](https://github.com/phoet/memo-it/compare/v0.2.0...v0.3.0))
16
20
 
data/README.md CHANGED
@@ -103,6 +103,17 @@ To be symmetric, it's also possible to define one or more parameters through the
103
103
  end
104
104
  ```
105
105
 
106
+ Provide your own memoization keys through the `:provided` key:
107
+
108
+ ```ruby
109
+ def load_repo(name = 'memo-it')
110
+ memo(provided: Date.today.day_of_week) do
111
+ # in this case the result will be memoized per name and day_of_week
112
+ HTTPClient.get("https://github.com/phoet/#{name}")
113
+ end
114
+ end
115
+ ```
116
+
106
117
  ### Turning it on and off
107
118
 
108
119
  In case you would like to disable memoization (ie. for testing) you can disable Memo::It:
@@ -120,6 +131,19 @@ In case you would like to disable memoization (ie. for testing) you can disable
120
131
  Memo.enabled? # => true
121
132
  ```
122
133
 
134
+ ## Caveats
135
+
136
+ ### Multiple calls to memo on the same line of code
137
+
138
+ If you want to call `memo` twice within the same line of code, you would need provide a custom key through the `:provided` argument.
139
+ This is not recommended through. A better alternative would be to put both calls into their own sub-methods and call those instead.
140
+
141
+ ### Runtime-Speed
142
+
143
+ Compared to other memoization frameworks, the `memo` method requires more computation and is slower by size of a magnitude: https://github.com/phoet/memo-it/issues/6
144
+ Do not use this library to optimize hot code-paths! Use it to cache really slow things such as network-requests or generation of large strings like JSON objects etc.
145
+ Whatever you do, benchmark your code in order to see if the memoization strategy you use is a good fit for your use-case.
146
+
123
147
  ## Installation
124
148
 
125
149
  ### As a Gem
@@ -1,5 +1,5 @@
1
1
  module Memo
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
 
4
4
  @enabled = true
5
5
 
@@ -16,7 +16,9 @@ module Memo
16
16
  end
17
17
 
18
18
  module It
19
- def memo(only: [], except: [], &block)
19
+ def memo(only: [], except: [], provided: [], &block)
20
+ return yield unless Memo.enabled?
21
+
20
22
  only = Array(only)
21
23
  if only.empty?
22
24
  except = Array(except)
@@ -28,9 +30,10 @@ module Memo
28
30
 
29
31
  keys = block.source_location
30
32
  keys << key_names.flat_map { |name| [name, block.binding.local_variable_get(name)] }
33
+ keys << Array(provided)
31
34
 
32
35
  @_memo_it ||= {}
33
- return @_memo_it[keys] if Memo.enabled? && @_memo_it.key?(keys)
36
+ return @_memo_it[keys] if @_memo_it.key?(keys)
34
37
  @_memo_it[keys] = yield
35
38
  end
36
39
  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.0
4
+ version: 0.5.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-03-04 00:00:00.000000000 Z
11
+ date: 2019-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.6.8
96
+ rubygems_version: 2.7.6
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: "\U0001F4E5 \U0001F4E4 simple yet clever memoization helper with parameter