memo-it 0.2.0 → 0.3.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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +32 -6
  3. data/lib/memo/it.rb +28 -8
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ba70b290050e4ac1069de24e58bbe33b242fcf8
4
- data.tar.gz: dd171e80509a3427abced4b6ab35f8d47fb168dc
3
+ metadata.gz: ae2ef7f59fcce47065e9e4c8b9f1c80453575b27
4
+ data.tar.gz: 1b75e93544b70146774b84051eb7dad5720bef00
5
5
  SHA512:
6
- metadata.gz: 0a670f8c4f7a572c4ab62a58ff372796e44823d660466234c407f34a91582ed11c3b7bfd21006d07813843c5a9fe5efead90cee29b484ee85f9f0cb75f963434
7
- data.tar.gz: 9a7bb9853224dc5414a29a5ba3bc688f7d6b23a0c160c4657d549b2282b88044c227419fca35acb71fe32c396687afecbdd896b925d9670105c116c5a3ca1c4c
6
+ metadata.gz: 50212743b82d81f81650f51f15e0808c9a889bd94f9c8d301fa5aa5a96a603f120d10ec1f657f49c4541e2799e4d7559d9dfd0ef57c850b981b3889df58f8344
7
+ data.tar.gz: 9a95c272ab56b6251cfba00d407887e890f0d0ec24898732a2d207913f57272074f3cb43de2c60144bd9eb9681beedcb407921d2992cfd6bc08a222ea37ba46f
data/README.md CHANGED
@@ -28,28 +28,54 @@ In case you want to memoize something that has parameters, memo-it will just use
28
28
  ```
29
29
 
30
30
  If, on the other hand, you want to memoize parameters but ignore one of them,
31
- you can do this by adding it to the `:ignore` list:
31
+ you can do this by adding it to the `:except` list:
32
32
 
33
33
  ```ruby
34
34
  def load_repo(name = 'memo-it', time = Time.now)
35
- memo(ignore: :time) do
35
+ memo(except: :time) do
36
36
  # in this case the result will be memoized per name
37
37
  HTTPClient.get("https://github.com/phoet/#{name}?time=#{time}")
38
38
  end
39
39
  end
40
40
  ```
41
41
 
42
- Or provide a list of parameters to ignore:
42
+ Or provide a list of parameters to except:
43
43
 
44
44
  ```ruby
45
45
  def load_repo(name = 'memo-it', time = Time.now, other = 'irrelevant')
46
- memo(ignore: [:time, :other]) do
46
+ memo(except: [:time, :other]) do
47
47
  # in this case the result will be memoized per name
48
48
  HTTPClient.get("https://github.com/phoet/#{name}?time=#{time}&other=#{other}")
49
49
  end
50
50
  end
51
51
  ```
52
52
 
53
+ To be symmetric, it's also possible to define one or more parameters through the `:only` key:
54
+
55
+ ```ruby
56
+ def load_repo(name = 'memo-it', time = Time.now, format = 'json')
57
+ memo(only: [:name, :format]) do
58
+ # in this case the result will be memoized per name & format
59
+ HTTPClient.get("https://github.com/phoet/#{name}?time=#{time}&format=#{format}")
60
+ end
61
+ end
62
+ ```
63
+
64
+ In case you would like to disable memoization (ie testing) you can disable Memo::It:
65
+
66
+ ```ruby
67
+ # enabled is default
68
+ Memo.enabled? # => true
69
+
70
+ # disable memoization globally
71
+ Memo.disable
72
+ Memo.enabled? # => false
73
+
74
+ # re-enable memoization
75
+ Memo.enable
76
+ Memo.enabled? # => true
77
+ ```
78
+
53
79
  ## Installation
54
80
 
55
81
  ### As a Gem
@@ -78,11 +104,11 @@ If you don't want to include yet another Gem, just run this in your shell:
78
104
 
79
105
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
80
106
 
81
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
107
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
82
108
 
83
109
  ## Contributing
84
110
 
85
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/memo-it. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
111
+ Bug reports and pull requests are welcome on GitHub at https://github.com/phoet/memo-it. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
86
112
 
87
113
 
88
114
  ## License
@@ -1,17 +1,37 @@
1
1
  module Memo
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
+
4
+ @enabled = true
5
+
6
+ def self.enabled?
7
+ @enabled
8
+ end
9
+
10
+ def self.enable
11
+ @enabled = true
12
+ end
13
+
14
+ def self.disable
15
+ @enabled = false
16
+ end
17
+
3
18
  module It
4
- def memo(ignore: [], &block)
5
- keys = block.source_location
6
- ignore = Array(ignore)
7
- keys << block.binding.local_variables.map do |name|
8
- next if ignore.include?(name)
9
- [name, block.binding.local_variable_get(name)]
19
+ def memo(only: [], except: [], &block)
20
+ only = Array(only)
21
+ if only.empty?
22
+ except = Array(except)
23
+ key_names = block.binding.local_variables
24
+ key_names -= except unless except.empty?
25
+ else
26
+ key_names = only
10
27
  end
28
+
29
+ keys = block.source_location
30
+ keys << key_names.map { |name| [name, block.binding.local_variable_get(name)] }
11
31
  keys = keys.flatten.map(&:to_s)
12
32
 
13
33
  @_memo_it ||= {}
14
- return @_memo_it[keys] if @_memo_it.key? keys
34
+ return @_memo_it[keys] if Memo.enabled? && @_memo_it.key?(keys)
15
35
  @_memo_it[keys] = yield
16
36
  end
17
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.2.0
4
+ version: 0.3.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-01-12 00:00:00.000000000 Z
11
+ date: 2017-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler