mem 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/lib/mem.rb +16 -0
- data/lib/mem/version.rb +3 -0
- data/mem.gemspec +22 -0
- data/spec/mem_spec.rb +29 -0
- data/spec/spec_helper.rb +8 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ecaf373ebeba1d6dae7301eb5e57f7b55d38de3c
|
4
|
+
data.tar.gz: 0ac99374afe9cc191003701f7a28c6a85d0a7928
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a04c41775361140df941c50a418fd861877f873ba6cd64200ddef7349330444ffac29239e3470c4c99c04893d9d5d6c664408005fa108f35b4ffc883cffaaf7
|
7
|
+
data.tar.gz: 234a6e162ad9dea70843a8eb3d1a090f444fa1a9fba14203084cb90bb18bd950cd9ff602416d93334e52d56434090b93e944142851d799e6be1968bc010886ed
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ryo Nakamura
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Mem
|
2
|
+
Memoize any method call.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
```
|
6
|
+
gem install mem
|
7
|
+
```
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
```
|
11
|
+
class Foo
|
12
|
+
extend Mem
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@count = 0
|
16
|
+
end
|
17
|
+
|
18
|
+
def bar
|
19
|
+
baz
|
20
|
+
end
|
21
|
+
|
22
|
+
# `memoize` defines bar_with_memoize & bar_without_memoize,
|
23
|
+
# and the result of 1st method call is stored into @bar.
|
24
|
+
memoize :bar
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def baz
|
29
|
+
@count += 1
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
foo = Foo.new
|
34
|
+
foo.bar #=> 1
|
35
|
+
foo.bar #=> 1
|
36
|
+
foo.bar #=> 1
|
37
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mem.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "mem/version"
|
2
|
+
require "active_support/core_ext/module/aliasing"
|
3
|
+
|
4
|
+
module Mem
|
5
|
+
def memoize(method_name)
|
6
|
+
define_method("#{method_name}_with_memoize") do |*args, &block|
|
7
|
+
if instance_variable_defined?("@#{method_name}")
|
8
|
+
instance_variable_get("@#{method_name}")
|
9
|
+
else
|
10
|
+
instance_variable_set("@#{method_name}", send("#{method_name}_without_memoize", *args, &block))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
alias_method "#{method_name}_without_memoize", method_name
|
14
|
+
alias_method method_name, "#{method_name}_with_memoize"
|
15
|
+
end
|
16
|
+
end
|
data/lib/mem/version.rb
ADDED
data/mem.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "mem/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "mem"
|
7
|
+
spec.version = Mem::VERSION
|
8
|
+
spec.authors = ["Ryo Nakamura"]
|
9
|
+
spec.email = ["r7kamura@gmail.com"]
|
10
|
+
spec.summary = "Memoize any method call"
|
11
|
+
spec.homepage = "https://github.com/r7kamura/mem"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
spec.add_development_dependency "rspec", "2.14.1"
|
22
|
+
end
|
data/spec/mem_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mem do
|
4
|
+
describe "#memoize" do
|
5
|
+
let(:klass) do
|
6
|
+
Class.new do
|
7
|
+
extend Mem
|
8
|
+
|
9
|
+
def foo(&block)
|
10
|
+
bar(&block)
|
11
|
+
end
|
12
|
+
memoize :foo
|
13
|
+
|
14
|
+
def bar
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:object) do
|
21
|
+
klass.new
|
22
|
+
end
|
23
|
+
|
24
|
+
it "memoizes the result of specified method call" do
|
25
|
+
expect(object).to receive(:bar).once.and_call_original
|
26
|
+
expect(object.foo { "foo" }).to eq "foo"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryo Nakamura
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.14.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.1
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- r7kamura@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/mem.rb
|
68
|
+
- lib/mem/version.rb
|
69
|
+
- mem.gemspec
|
70
|
+
- spec/mem_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
homepage: https://github.com/r7kamura/mem
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Memoize any method call
|
96
|
+
test_files:
|
97
|
+
- spec/mem_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
has_rdoc:
|