memoit 0.1.2 → 0.2.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: 7d5440f6ce3b976230858dcb48b83657621987ec
4
- data.tar.gz: 1fe48fce65be49c139a83c3a0fe8c636af0b23ed
3
+ metadata.gz: f8ece57c9bea6440805f8ccd28b6bae60c977d40
4
+ data.tar.gz: cd5fd4c30fd3c6a9fa040c6a85510a88b8fbd00d
5
5
  SHA512:
6
- metadata.gz: 537902c0edacd41f6026d270fdc8ea85c151006a5200d57588d19bd701a0d55b9b853def246caa9b9a82ad2257b20f44b7db3d0a52644fce8a7b8ec9c7626d03
7
- data.tar.gz: 701854fb5eca663e2ce7aee4071c437ed4b339c4cbd18eb1a21a656423bd4e84075a54741454e16fdf360efd1773a48ec33436bb51abb117541db8020b83fc75
6
+ metadata.gz: 4091ce16342522f9321735939e83d78f17f401c77480fb572c2d5b20de87f94ee58db4f6b0d1ca87a9c207b0df4a1eed1f32a738596a9e43c895dada06b05b99
7
+ data.tar.gz: cbe78faf3d28a6d100f14df8c90994170ded87f95991e7c8a607f4593d36ff13cd9ac8e5ce9063589d8d654847e015be3540bc5a20dad0d7caac996351517b77
data/README.md CHANGED
@@ -17,9 +17,17 @@ class Foo
17
17
  memoize def bar(value)
18
18
  expensive_calculation(value)
19
19
  end
20
+
21
+ memoize_class_method def self.baz(value)
22
+ expensive_calculation(value)
23
+ end
20
24
  end
21
25
  ```
22
26
 
27
+ ## Is it any good?
28
+
29
+ [Yes](https://news.ycombinator.com/item?id=3067434).
30
+
23
31
  ## License
24
32
 
25
33
  [MIT](License.txt)
@@ -16,15 +16,23 @@ module Memoit
16
16
  define_method(name) do |*args, &block|
17
17
  return super(*args, &block) if block
18
18
  cache = instance_variable_get(ivar_name) || instance_variable_set(ivar_name, {})
19
- hash = args.hash
20
- if cache.has_key?(hash)
21
- cache[hash]
22
- else
23
- cache[hash] = super(*args)
24
- end
19
+ cache.fetch(args.hash) { |hash| cache[hash] = super(*args) }
25
20
  end
26
21
  end
27
22
  prepend mod
23
+ name
24
+ end
25
+
26
+ # Memoize the class method with the given name.
27
+ #
28
+ # @example
29
+ # class Foo
30
+ # memoize_class_method def self.bar(value)
31
+ # expensive_calculation(value)
32
+ # end
33
+ # end
34
+ def memoize_class_method(name)
35
+ singleton_class.memoize(name)
28
36
  end
29
37
  end
30
38
 
@@ -1,3 +1,3 @@
1
1
  module Memoit
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,6 +3,10 @@ require "memoit"
3
3
  describe Memoit do
4
4
  let(:klass) do
5
5
  Class.new do
6
+ memoize_class_method def self.foo
7
+ rand
8
+ end
9
+
6
10
  memoize def foo
7
11
  rand
8
12
  end
@@ -41,5 +45,20 @@ describe Memoit do
41
45
  expect(instance).to receive(:foo).once
42
46
  expect(instance.falsy).to eq(instance.falsy)
43
47
  end
48
+
49
+ it "returns the name of the method" do
50
+ name = nil
51
+ Class.new do
52
+ name = memoize def blah
53
+ end
54
+ end
55
+ expect(name).to eq(:blah)
56
+ end
57
+ end
58
+
59
+ describe ".memoize_class_method" do
60
+ it "caches result" do
61
+ expect(klass.foo).to eq(klass.foo)
62
+ end
44
63
  end
45
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memoit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-14 00:00:00.000000000 Z
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,9 +88,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project:
91
- rubygems_version: 2.4.3
91
+ rubygems_version: 2.4.5
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: Memoize is back!
95
95
  test_files:
96
96
  - spec/memoit_spec.rb
97
+ has_rdoc: