memoist2 0.1.2 → 0.2.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/README.md +7 -7
- data/Rakefile +1 -1
- data/benchmark.rb +1 -1
- data/lib/memoist2.rb +25 -11
- data/memoist2.gemspec +3 -2
- data/spec/memoist2_spec.rb +111 -34
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e42697bf2e5d3807d6075bf5498bf849b86353c
|
4
|
+
data.tar.gz: 80b78cee05bd57d58ccfd7afd5e46b4a6e4b79c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66d8dd9039e1f2ac5c724bbc01946cafda39c3d1d5201564f42a2cbe33f37e2ce087f1836b8ee130d408403862245f25bf68e614f1e50b25ce8fc424aa4c2786
|
7
|
+
data.tar.gz: 2ad308d69a84799e565257ec2e4dedcfac853fc2039346b22abcaa38045859c2aea3d8347435f0d2444d2dace75124e2db7f3112fad440664f6afb734d1f9eec
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
memoist2
|
2
2
|
========
|
3
3
|
|
4
|
+
[](https://travis-ci.org/matthewrudy/memoist2)
|
5
|
+
|
4
6
|
Simple Memoization for Ruby 2.0
|
5
7
|
|
6
8
|
Example
|
@@ -9,7 +11,7 @@ Example
|
|
9
11
|
Memoize an instance method
|
10
12
|
|
11
13
|
class Foo
|
12
|
-
|
14
|
+
include Memoist2
|
13
15
|
|
14
16
|
def bar
|
15
17
|
sleep 1 && 2**10
|
@@ -20,14 +22,12 @@ Memoize an instance method
|
|
20
22
|
Memoize a class method
|
21
23
|
|
22
24
|
class Foo
|
23
|
-
|
24
|
-
extend Memoist2
|
25
|
+
include Memoist2
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
memoize :bar
|
27
|
+
def self.bar
|
28
|
+
# something expensive
|
30
29
|
end
|
30
|
+
memoize_class_method :bar
|
31
31
|
end
|
32
32
|
|
33
33
|
Licence
|
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ spec = Gem::Specification.new do |s|
|
|
15
15
|
|
16
16
|
# Basic details
|
17
17
|
s.name = "memoist2"
|
18
|
-
s.version = "0.
|
18
|
+
s.version = "0.2.0"
|
19
19
|
s.summary = "Really simple memoization for ruby 2.0"
|
20
20
|
s.author = "Matthew Rudy Jacobs"
|
21
21
|
s.email = "MatthewRudyJacobs@gmail.com"
|
data/benchmark.rb
CHANGED
data/lib/memoist2.rb
CHANGED
@@ -4,19 +4,33 @@ module Memoist2
|
|
4
4
|
"@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
|
5
5
|
end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def memoize(method_name)
|
10
|
+
memoized_ivar = Memoist2.memoized_ivar_for(method_name)
|
11
|
+
memoized_module = Module.new do
|
12
|
+
module_eval <<-EVAL
|
13
|
+
def #{method_name}
|
14
|
+
unless #{memoized_ivar}
|
15
|
+
#{memoized_ivar} = [super]
|
16
|
+
end
|
17
|
+
#{memoized_ivar}[0]
|
14
18
|
end
|
15
|
-
|
16
|
-
|
17
|
-
|
19
|
+
EVAL
|
20
|
+
end
|
21
|
+
prepend memoized_module
|
22
|
+
end
|
23
|
+
|
24
|
+
def memoize_class_method(method_name)
|
25
|
+
singleton_class.class_eval do
|
26
|
+
include Memoist2 unless ancestors.include?(Memoist2)
|
27
|
+
memoize method_name
|
28
|
+
end
|
18
29
|
end
|
19
|
-
|
30
|
+
|
20
31
|
end
|
21
32
|
|
33
|
+
def self.included(klass)
|
34
|
+
klass.extend(ClassMethods)
|
35
|
+
end
|
22
36
|
end
|
data/memoist2.gemspec
CHANGED
@@ -2,15 +2,16 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "memoist2"
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Matthew Rudy Jacobs"]
|
9
9
|
s.date = "2013-07-19"
|
10
10
|
s.email = "MatthewRudyJacobs@gmail.com"
|
11
11
|
s.extra_rdoc_files = ["README.md"]
|
12
|
-
s.files = [".gitignore", ".rspec", ".ruby-version", ".travis.yml", "Gemfile", "README.md", "Rakefile", "example/foo.rb", "lib/memoist2.rb", "memoist2.gemspec", "spec/memoist2_spec.rb", "spec/spec_helper.rb"]
|
12
|
+
s.files = [".gitignore", ".rspec", ".ruby-version", ".travis.yml", "Gemfile", "LICENCE", "README.md", "Rakefile", "benchmark.rb", "example/foo.rb", "lib/memoist2.rb", "memoist2.gemspec", "spec/memoist2_spec.rb", "spec/spec_helper.rb"]
|
13
13
|
s.homepage = "https://github.com/matthewrudy/memoist2"
|
14
|
+
s.licenses = ["MIT"]
|
14
15
|
s.rdoc_options = ["--main", "README.md"]
|
15
16
|
s.require_paths = ["lib"]
|
16
17
|
s.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
|
data/spec/memoist2_spec.rb
CHANGED
@@ -2,67 +2,144 @@ require 'memoist2'
|
|
2
2
|
|
3
3
|
describe Memoist2 do
|
4
4
|
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
count!
|
5
|
+
# used as base class for examples
|
6
|
+
class Counter
|
7
|
+
def initialize
|
8
|
+
@counter = 0
|
10
9
|
end
|
11
|
-
|
10
|
+
attr_reader :counter
|
12
11
|
|
13
|
-
def
|
14
|
-
|
12
|
+
def count!
|
13
|
+
@counter += 1
|
15
14
|
end
|
16
|
-
|
15
|
+
end
|
17
16
|
|
18
|
-
|
19
|
-
|
17
|
+
describe "nil values" do
|
18
|
+
subject do
|
19
|
+
Class.new(Counter) do
|
20
|
+
include Memoist2
|
21
|
+
|
22
|
+
def nilly
|
23
|
+
count!
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
memoize :nilly
|
27
|
+
end.new
|
20
28
|
end
|
21
|
-
memoize :bang!
|
22
29
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@count += 1
|
30
|
+
it "returns the expected value" do
|
31
|
+
5.times do
|
32
|
+
subject.nilly.should == nil
|
33
|
+
end
|
28
34
|
end
|
29
35
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
@bar_count ||= 0
|
35
|
-
@bar_count += 1
|
36
|
-
end
|
37
|
-
memoize :bar
|
36
|
+
it "memoizes correctly" do
|
37
|
+
expect do
|
38
|
+
5.times{ subject.nilly }
|
39
|
+
end.to change{ subject.counter }.by(1)
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
43
|
describe "instance methods" do
|
42
|
-
subject
|
44
|
+
subject do
|
45
|
+
Class.new(Counter) do
|
46
|
+
include Memoist2
|
47
|
+
|
48
|
+
def foo
|
49
|
+
count!
|
50
|
+
:bar
|
51
|
+
end
|
52
|
+
memoize :foo
|
53
|
+
end.new
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns the expected value" do
|
57
|
+
5.times do
|
58
|
+
subject.foo.should == :bar
|
59
|
+
end
|
60
|
+
end
|
43
61
|
|
44
|
-
it "
|
45
|
-
|
62
|
+
it "memoizes correctly" do
|
63
|
+
expect do
|
64
|
+
5.times{ subject.foo }
|
65
|
+
end.to change{ subject.counter }.by(1)
|
46
66
|
end
|
47
67
|
end
|
48
68
|
|
49
69
|
describe "class methods" do
|
50
|
-
|
70
|
+
describe "using metaclass" do
|
71
|
+
subject do
|
72
|
+
Class.new do
|
73
|
+
class << self
|
74
|
+
include Memoist2
|
75
|
+
|
76
|
+
def foo
|
77
|
+
@counter ||= 0
|
78
|
+
@counter += 1
|
79
|
+
end
|
80
|
+
memoize :foo
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
51
84
|
|
52
|
-
|
53
|
-
|
85
|
+
it "works" do
|
86
|
+
5.times do
|
87
|
+
subject.foo.should == 1
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "using memoize_class_method" do
|
93
|
+
subject do
|
94
|
+
Class.new do
|
95
|
+
include Memoist2
|
96
|
+
|
97
|
+
def self.foo
|
98
|
+
@counter ||= 0
|
99
|
+
@counter += 1
|
100
|
+
end
|
101
|
+
memoize_class_method :foo
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
it "works" do
|
106
|
+
5.times do
|
107
|
+
subject.foo.should == 1
|
108
|
+
end
|
109
|
+
end
|
54
110
|
end
|
55
111
|
end
|
56
112
|
|
57
113
|
describe "punctuated methods" do
|
58
|
-
subject
|
114
|
+
subject do
|
115
|
+
Class.new do
|
116
|
+
include Memoist2
|
117
|
+
|
118
|
+
def question?
|
119
|
+
@question_calls ||= 0
|
120
|
+
@question_calls += 1
|
121
|
+
end
|
122
|
+
memoize :question?
|
123
|
+
|
124
|
+
def bang!
|
125
|
+
@bang_calls ||= 0
|
126
|
+
@bang_calls += 1
|
127
|
+
end
|
128
|
+
memoize :bang!
|
129
|
+
|
130
|
+
end.new
|
131
|
+
end
|
59
132
|
|
60
133
|
it "can memoize question methods" do
|
61
|
-
|
134
|
+
5.times do
|
135
|
+
subject.question?.should == 1
|
136
|
+
end
|
62
137
|
end
|
63
138
|
|
64
139
|
it "can memoize bang methods" do
|
65
|
-
|
140
|
+
5.times do
|
141
|
+
subject.bang!.should == 1
|
142
|
+
end
|
66
143
|
end
|
67
144
|
end
|
68
145
|
end
|