memoist 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +17 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE.md +20 -0
- data/README.md +30 -3
- data/Rakefile +11 -0
- data/lib/memoist.rb +3 -1
- data/lib/memoist/version.rb +3 -0
- data/memoist.gemspec +37 -0
- data/test/memoist_test.rb +3 -3
- metadata +51 -15
- data/test/benchmark/memoist_benchmark.rb +0 -143
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 630e2e80eea33c244c75ee3cab805415e627f357
|
4
|
+
data.tar.gz: 4e7760bdbe28b2e7aee579e722c80142cc775976
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99ea31ce66097c7350d6f1393bfdb322b9002d145674caa68e04545ec082a4db8da671553f21198463125685275194843e55a256ae9315c7cdccdcf344b4e548
|
7
|
+
data.tar.gz: 24b7a97927feb6a684e1c1dcc31b827510ccccb194b176de04ab852cdc1d5d8a88be845a79c6f1d3c0317341b154f4c44253d0133d8d65623cdc4281f152bafe
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012-2013 Matthew Rudy Jacobs
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
Memoist
|
2
2
|
=============
|
3
3
|
|
4
|
+
[![Build Status](https://travis-ci.org/matthewrudy/memoist.png?branch=master)](https://travis-ci.org/matthewrudy/memoist)
|
5
|
+
|
4
6
|
Memoist is an extraction of ActiveSupport::Memoizable.
|
5
7
|
|
6
8
|
Since June 2011 ActiveSupport::Memoizable has been deprecated.
|
@@ -15,7 +17,7 @@ Just extend with the Memoist module
|
|
15
17
|
require 'memoist'
|
16
18
|
class Person
|
17
19
|
extend Memoist
|
18
|
-
|
20
|
+
|
19
21
|
def social_security
|
20
22
|
decrypt_social_security
|
21
23
|
end
|
@@ -24,7 +26,7 @@ Just extend with the Memoist module
|
|
24
26
|
|
25
27
|
And person.social_security will only be calculated once.
|
26
28
|
|
27
|
-
Every memoized function (which initially was not accepting any arguments) has a ```(reload)```
|
29
|
+
Every memoized function (which initially was not accepting any arguments) has a ```(reload)```
|
28
30
|
argument you can pass in to bypass and reset the memoization:
|
29
31
|
|
30
32
|
def some_method
|
@@ -46,6 +48,21 @@ You can even memoize method that takes arguments.
|
|
46
48
|
|
47
49
|
This will only be calculated once per value of income.
|
48
50
|
|
51
|
+
You can also memoize class methods.
|
52
|
+
|
53
|
+
class Person
|
54
|
+
|
55
|
+
class << self
|
56
|
+
extend Memoist
|
57
|
+
def with_overdue_taxes
|
58
|
+
# ...
|
59
|
+
end
|
60
|
+
memoize :with_overdue_taxes
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
|
49
66
|
Reload
|
50
67
|
------
|
51
68
|
|
@@ -53,7 +70,7 @@ Each memoized function comes with a way to flush the existing value.
|
|
53
70
|
|
54
71
|
person.social_security # returns the memoized value
|
55
72
|
person.social_security(true) # bypasses the memoized value and rememoizes it
|
56
|
-
|
73
|
+
|
57
74
|
This also works with a memoized method with arguments
|
58
75
|
|
59
76
|
person.taxes_due(100_000) # returns the memoized value
|
@@ -79,6 +96,16 @@ Everyone who contributed to it in the rails repository.
|
|
79
96
|
* Jay Pignata
|
80
97
|
* Damien Mathieu
|
81
98
|
* José Valim
|
99
|
+
* Matthew Rudy Jacobs
|
100
|
+
|
101
|
+
Contributing
|
102
|
+
============
|
103
|
+
|
104
|
+
1. Fork it ( http://github.com/<my-github-username>/memoist/fork )
|
105
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
106
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
107
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
108
|
+
5. Create new Pull Request
|
82
109
|
|
83
110
|
License
|
84
111
|
=======
|
data/Rakefile
ADDED
data/lib/memoist.rb
CHANGED
@@ -96,7 +96,7 @@ module Memoist
|
|
96
96
|
include InstanceMethods
|
97
97
|
|
98
98
|
if method_defined?(unmemoized_method)
|
99
|
-
raise "Already memoized #{method_name}"
|
99
|
+
raise AlreadyMemoizedError.new("Already memoized #{method_name}")
|
100
100
|
end
|
101
101
|
alias_method unmemoized_method, method_name
|
102
102
|
|
@@ -194,4 +194,6 @@ module Memoist
|
|
194
194
|
end
|
195
195
|
end
|
196
196
|
end
|
197
|
+
|
198
|
+
class AlreadyMemoizedError < RuntimeError; end
|
197
199
|
end
|
data/memoist.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'memoist/version'
|
5
|
+
|
6
|
+
AUTHORS = [
|
7
|
+
["Joshua Peek", "josh@joshpeek.com"],
|
8
|
+
["Tarmo Tänav", "tarmo@itech.ee"],
|
9
|
+
["Jeremy Kemper", "jeremy@bitsweat.net"],
|
10
|
+
["Eugene Pimenov", "libc@mac.com"],
|
11
|
+
["Xavier Noria", "fxn@hashref.com"],
|
12
|
+
["Niels Ganser", "niels@herimedia.co"],
|
13
|
+
["Carl Lerche & Yehuda Katz", "wycats@gmail.com"],
|
14
|
+
["jeem", "jeem@hughesorama.com"],
|
15
|
+
["Jay Pignata", "john.pignata@gmail.com"],
|
16
|
+
["Damien Mathieu", "42@dmathieu.com"],
|
17
|
+
["José Valim", "jose.valim@gmail.com"],
|
18
|
+
["Matthew Rudy Jacobs", "matthewrudyjacobs@gmail.com"],
|
19
|
+
]
|
20
|
+
|
21
|
+
Gem::Specification.new do |spec|
|
22
|
+
spec.name = "memoist"
|
23
|
+
spec.version = Memoist::VERSION
|
24
|
+
spec.authors = AUTHORS.map{ |name, email| name }
|
25
|
+
spec.email = AUTHORS.map{ |name, email| email }
|
26
|
+
spec.summary = %q{memoize methods invocation}
|
27
|
+
spec.homepage = "https://github.com/matthewrudy/memoist"
|
28
|
+
spec.license = "MIT"
|
29
|
+
|
30
|
+
spec.files = `git ls-files -z`.split("\x0")
|
31
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
32
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
36
|
+
spec.add_development_dependency "rake"
|
37
|
+
end
|
data/test/memoist_test.rb
CHANGED
@@ -289,15 +289,15 @@ class MemoistTest < Test::Unit::TestCase
|
|
289
289
|
end
|
290
290
|
|
291
291
|
def test_double_memoization
|
292
|
-
assert_raise(
|
292
|
+
assert_raise(Memoist::AlreadyMemoizedError) { Person.memoize :name }
|
293
293
|
person = Person.new
|
294
294
|
person.extend Memoist
|
295
|
-
assert_raise(
|
295
|
+
assert_raise(Memoist::AlreadyMemoizedError) { person.memoize :name }
|
296
296
|
|
297
297
|
company = Company.new
|
298
298
|
company.extend Memoist
|
299
299
|
company.memoize :name
|
300
|
-
assert_raise(
|
300
|
+
assert_raise(Memoist::AlreadyMemoizedError) { company.memoize :name }
|
301
301
|
end
|
302
302
|
|
303
303
|
def test_double_memoization_with_identifier
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memoist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Peek
|
@@ -15,11 +15,40 @@ authors:
|
|
15
15
|
- Jay Pignata
|
16
16
|
- Damien Mathieu
|
17
17
|
- José Valim
|
18
|
+
- Matthew Rudy Jacobs
|
18
19
|
autorequire:
|
19
20
|
bindir: bin
|
20
21
|
cert_chain: []
|
21
|
-
date:
|
22
|
-
dependencies:
|
22
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
23
|
+
dependencies:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: bundler
|
26
|
+
requirement: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.5'
|
31
|
+
type: :development
|
32
|
+
prerelease: false
|
33
|
+
version_requirements: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - "~>"
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.5'
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rake
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
23
52
|
description:
|
24
53
|
email:
|
25
54
|
- josh@joshpeek.com
|
@@ -33,41 +62,48 @@ email:
|
|
33
62
|
- john.pignata@gmail.com
|
34
63
|
- 42@dmathieu.com
|
35
64
|
- jose.valim@gmail.com
|
65
|
+
- matthewrudyjacobs@gmail.com
|
36
66
|
executables: []
|
37
67
|
extensions: []
|
38
|
-
extra_rdoc_files:
|
39
|
-
- README.md
|
68
|
+
extra_rdoc_files: []
|
40
69
|
files:
|
70
|
+
- ".gitignore"
|
71
|
+
- ".travis.yml"
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.md
|
41
74
|
- README.md
|
42
|
-
-
|
75
|
+
- Rakefile
|
76
|
+
- lib/memoist.rb
|
77
|
+
- lib/memoist/core_ext/singleton_class.rb
|
78
|
+
- lib/memoist/version.rb
|
79
|
+
- memoist.gemspec
|
43
80
|
- test/memoist_test.rb
|
44
81
|
- test/test_helper.rb
|
45
|
-
- lib/memoist/core_ext/singleton_class.rb
|
46
|
-
- lib/memoist.rb
|
47
82
|
homepage: https://github.com/matthewrudy/memoist
|
48
83
|
licenses:
|
49
84
|
- MIT
|
50
85
|
metadata: {}
|
51
86
|
post_install_message:
|
52
|
-
rdoc_options:
|
53
|
-
- --main
|
54
|
-
- README.md
|
87
|
+
rdoc_options: []
|
55
88
|
require_paths:
|
56
89
|
- lib
|
57
90
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
91
|
requirements:
|
59
|
-
- -
|
92
|
+
- - ">="
|
60
93
|
- !ruby/object:Gem::Version
|
61
94
|
version: '0'
|
62
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
96
|
requirements:
|
64
|
-
- -
|
97
|
+
- - ">="
|
65
98
|
- !ruby/object:Gem::Version
|
66
99
|
version: '0'
|
67
100
|
requirements: []
|
68
101
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
102
|
+
rubygems_version: 2.2.2
|
70
103
|
signing_key:
|
71
104
|
specification_version: 4
|
72
105
|
summary: memoize methods invocation
|
73
|
-
test_files:
|
106
|
+
test_files:
|
107
|
+
- test/memoist_test.rb
|
108
|
+
- test/test_helper.rb
|
109
|
+
has_rdoc:
|
@@ -1,143 +0,0 @@
|
|
1
|
-
<<-INTRO
|
2
|
-
|
3
|
-
Question:
|
4
|
-
How fast are these simple use case of memoisation
|
5
|
-
|
6
|
-
hash = {}
|
7
|
-
hash[:name] = "Peter"
|
8
|
-
hash[:age] = 23
|
9
|
-
hash[:hair_colour] = "black"
|
10
|
-
hash
|
11
|
-
|
12
|
-
{}.tap do |hash|
|
13
|
-
hash[:name] = "Peter"
|
14
|
-
hash[:age] = 23
|
15
|
-
hash[:hair_colour] = "black"
|
16
|
-
end
|
17
|
-
|
18
|
-
Answer:
|
19
|
-
Not really
|
20
|
-
|
21
|
-
user system total real
|
22
|
-
local variable 14.170000 0.060000 14.230000 ( 14.223995)
|
23
|
-
tap 15.440000 0.040000 15.480000 ( 15.492474)
|
24
|
-
|
25
|
-
INTRO
|
26
|
-
|
27
|
-
require 'memoist'
|
28
|
-
|
29
|
-
class Foo
|
30
|
-
extend Memoist
|
31
|
-
|
32
|
-
def memoized
|
33
|
-
42
|
34
|
-
end
|
35
|
-
memoize :memoized
|
36
|
-
|
37
|
-
def memoized_manually
|
38
|
-
@memoized_manually ||= 42
|
39
|
-
end
|
40
|
-
|
41
|
-
def memoized_manually_with_defined
|
42
|
-
unless defined?(@memoized_manually_with_defined)
|
43
|
-
@memoized_manually_with_defined = 42
|
44
|
-
end
|
45
|
-
@memoized_manually_with_defined
|
46
|
-
end
|
47
|
-
|
48
|
-
def raw
|
49
|
-
42
|
50
|
-
end
|
51
|
-
|
52
|
-
def memoized_with_arg(arg)
|
53
|
-
42
|
54
|
-
end
|
55
|
-
memoize :memoized_with_arg
|
56
|
-
end
|
57
|
-
|
58
|
-
require 'benchmark'
|
59
|
-
|
60
|
-
TIMES = 10_000_000
|
61
|
-
|
62
|
-
Benchmark.bm(40) do |x|
|
63
|
-
x.report "calling memoized method" do
|
64
|
-
foo = Foo.new
|
65
|
-
TIMES.times do
|
66
|
-
foo.memoized
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
x.report "calling manually memoized method" do
|
71
|
-
foo = Foo.new
|
72
|
-
TIMES.times do
|
73
|
-
foo.memoized_manually
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
x.report "calling manually memoized (with defined?) method" do
|
78
|
-
foo = Foo.new
|
79
|
-
TIMES.times do
|
80
|
-
foo.memoized_manually_with_defined
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
x.report "calling non-memoized method" do
|
85
|
-
foo = Foo.new
|
86
|
-
TIMES.times do
|
87
|
-
foo.raw
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
x.report "calling memoized method - one time" do
|
92
|
-
TIMES.times do
|
93
|
-
foo = Foo.new
|
94
|
-
foo.memoized
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
x.report "calling manually memoized method - one time" do
|
99
|
-
TIMES.times do
|
100
|
-
foo = Foo.new
|
101
|
-
foo.memoized_manually
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
x.report "calling manually memoized (with defined?) method - one time" do
|
106
|
-
TIMES.times do
|
107
|
-
foo = Foo.new
|
108
|
-
foo.memoized_manually_with_defined
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
x.report "calling non-memoized method - one time" do
|
113
|
-
TIMES.times do
|
114
|
-
foo = Foo.new
|
115
|
-
foo.raw
|
116
|
-
end
|
117
|
-
end
|
118
|
-
x.report "re-loading memoized method" do
|
119
|
-
foo = Foo.new
|
120
|
-
TIMES.times do
|
121
|
-
foo.memoized(true)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
comment = <<-COMMENT
|
126
|
-
|
127
|
-
x.report "calling memoized method with args" do
|
128
|
-
foo = Foo.new
|
129
|
-
TIMES.times do
|
130
|
-
foo.memoized_with_arg(1337)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
x.report "calling memoized method with args - no cache" do
|
135
|
-
foo = Foo.new
|
136
|
-
TIMES.times do |i|
|
137
|
-
foo.memoized_with_arg(i)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
COMMENT
|
142
|
-
|
143
|
-
end
|