mem 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +5 -2
- data/lib/mem/version.rb +1 -1
- data/lib/mem.rb +30 -8
- data/mem.gemspec +1 -1
- data/spec/mem_spec.rb +44 -15
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac41bdc5119cf45f7824c911ae16f5df3e2c808d
|
4
|
+
data.tar.gz: 1f63987a155340dacbffc0d19648bdc08b5c9970
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3166d66167e8b19d45e281f57c289cee57d34ecd0eca5e3f0608bac09381a45b8b596d675cacebbe1f5714fbc8b8feac9384905939049af65d470abe2810ec05
|
7
|
+
data.tar.gz: a9b281277509b6731eda20c6c65d8985de7a930d8f4fc4fa70b057df093b7ab92a68e71ab325a23497180845287a2025dcbcfec0d45045abaaf8a1901066c688
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -9,7 +9,7 @@ gem install mem
|
|
9
9
|
## Usage
|
10
10
|
```ruby
|
11
11
|
class Foo
|
12
|
-
|
12
|
+
include Mem
|
13
13
|
|
14
14
|
def initialize
|
15
15
|
@count = 0
|
@@ -20,7 +20,7 @@ class Foo
|
|
20
20
|
end
|
21
21
|
|
22
22
|
# `memoize` defines bar_with_memoize & bar_without_memoize,
|
23
|
-
# and the result of the 1st method call is stored into @
|
23
|
+
# and the result of the 1st method call is stored into @memoized_table.
|
24
24
|
memoize :bar
|
25
25
|
|
26
26
|
private
|
@@ -34,4 +34,7 @@ foo = Foo.new
|
|
34
34
|
foo.bar #=> 1
|
35
35
|
foo.bar #=> 1
|
36
36
|
foo.bar #=> 1
|
37
|
+
foo.has_memoized?(:bar) #=> true
|
38
|
+
foo.memoized(:bar) #=> 1
|
39
|
+
foo.memoized_table #=> { bar: 1 }
|
37
40
|
```
|
data/lib/mem/version.rb
CHANGED
data/lib/mem.rb
CHANGED
@@ -1,15 +1,37 @@
|
|
1
1
|
require "mem/version"
|
2
2
|
|
3
3
|
module Mem
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def has_memoized?(key)
|
9
|
+
memoized_table.key?(key)
|
10
|
+
end
|
11
|
+
|
12
|
+
def memoize(key, value)
|
13
|
+
memoized_table[key] = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def memoized(key)
|
17
|
+
memoized_table[key]
|
18
|
+
end
|
19
|
+
|
20
|
+
def memoized_table
|
21
|
+
@memoized_table ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def memoize(method_name)
|
26
|
+
define_method("#{method_name}_with_memoize") do |*args, &block|
|
27
|
+
if has_memoized?(method_name)
|
28
|
+
memoized(method_name)
|
29
|
+
else
|
30
|
+
memoize(method_name, send("#{method_name}_without_memoize", *args, &block))
|
31
|
+
end
|
10
32
|
end
|
33
|
+
alias_method "#{method_name}_without_memoize", method_name
|
34
|
+
alias_method method_name, "#{method_name}_with_memoize"
|
11
35
|
end
|
12
|
-
alias_method "#{method_name}_without_memoize", method_name
|
13
|
-
alias_method method_name, "#{method_name}_with_memoize"
|
14
36
|
end
|
15
37
|
end
|
data/mem.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
17
|
spec.require_paths = ["lib"]
|
18
18
|
|
19
|
-
spec.add_development_dependency "bundler"
|
19
|
+
spec.add_development_dependency "bundler"
|
20
20
|
spec.add_development_dependency "rake"
|
21
21
|
spec.add_development_dependency "rspec", "2.14.1"
|
22
22
|
end
|
data/spec/mem_spec.rb
CHANGED
@@ -2,28 +2,57 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Mem do
|
4
4
|
describe "#memoize" do
|
5
|
-
let(:
|
6
|
-
|
7
|
-
|
5
|
+
let(:object) do
|
6
|
+
klass.new
|
7
|
+
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
context "with normal method name" do
|
10
|
+
let(:klass) do
|
11
|
+
Class.new do
|
12
|
+
include Mem
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
def a(value, &block)
|
15
|
+
b(value, &block)
|
16
|
+
end
|
17
|
+
memoize :a
|
18
|
+
|
19
|
+
def b(value)
|
20
|
+
[value, yield]
|
21
|
+
end
|
16
22
|
end
|
17
23
|
end
|
18
|
-
end
|
19
24
|
|
20
|
-
|
21
|
-
|
25
|
+
it "memoizes the method call" do
|
26
|
+
expect(object).to receive(:b).once.and_call_original
|
27
|
+
expect(object.a(1) { 2 }).to eq [1, 2]
|
28
|
+
expect(object.a(3) { 4 }).to eq [1, 2]
|
29
|
+
end
|
22
30
|
end
|
23
31
|
|
24
|
-
|
25
|
-
|
26
|
-
|
32
|
+
context "with normal method name" do
|
33
|
+
let(:klass) do
|
34
|
+
Class.new do
|
35
|
+
include Mem
|
36
|
+
|
37
|
+
def a!(value, &block)
|
38
|
+
b(value, &block)
|
39
|
+
end
|
40
|
+
memoize :a!
|
41
|
+
|
42
|
+
def b(value)
|
43
|
+
[value, yield]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it "memoizes the method call" do
|
49
|
+
expect(object).to receive(:b).once.and_call_original
|
50
|
+
expect(object.a!(1) { 2 }).to eq [1, 2]
|
51
|
+
expect(object.a!(3) { 4 }).to eq [1, 2]
|
52
|
+
object.should have_memoized(:a!)
|
53
|
+
object.memoized(:a!).should == [1, 2]
|
54
|
+
object.memoized_table.should == { a!: [1, 2] }
|
55
|
+
end
|
27
56
|
end
|
28
57
|
end
|
29
58
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,6 +60,7 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- .gitignore
|
63
|
+
- CHANGELOG.md
|
63
64
|
- Gemfile
|
64
65
|
- LICENSE.txt
|
65
66
|
- README.md
|