memoit 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7fedfe089098dabf4550e6372c2427bae7c074a8
4
+ data.tar.gz: e232ef9aa3f0e4382e6a13d0955967c5c0406548
5
+ SHA512:
6
+ metadata.gz: a3b2f1e7899f21c4411dd0e82b5e87dea805b8c7184cff93e8231e343253013b6d30d63dc217376965afdf3ddb5071351318e9cfc6ff26c66ce7df497e4f0ee2
7
+ data.tar.gz: 8ef1f624dbbb4bfbc3b5797615706251c5d3486954e936c3f49dedbf53bd2bbf5eadd86dbbb43affff778af4186d182fde6ad2f59b5cafe1a857997bf67a71f6
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in memoit.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jonas Nicklas
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.
@@ -0,0 +1,25 @@
1
+ # Memoit
2
+
3
+ Memoizes methods.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'memoit'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ``` ruby
16
+ class Foo
17
+ memoize def bar(value)
18
+ expensive_calculation(value)
19
+ end
20
+ end
21
+ ```
22
+
23
+ ## License
24
+
25
+ [MIT](License.txt)
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ require "memoit/version"
2
+
3
+ module Memoit
4
+
5
+ # Memoize the method with the given name.
6
+ #
7
+ # @example
8
+ # class Foo
9
+ # memoize def bar(value)
10
+ # expensive_calculation(value)
11
+ # end
12
+ # end
13
+ def memoize(name)
14
+ ivar_name = "@_memo_#{name}".to_sym
15
+ mod = Module.new do
16
+ define_method(name) do |*args, &block|
17
+ return super(*args, &block) if block
18
+ cache = instance_variable_get(ivar_name) || instance_variable_set(ivar_name, {})
19
+ cache[args.hash] ||= super(*args)
20
+ end
21
+ end
22
+ prepend mod
23
+ end
24
+ end
25
+
26
+ Module.send(:include, Memoit)
@@ -0,0 +1,3 @@
1
+ module Memoit
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'memoit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "memoit"
8
+ spec.version = Memoit::VERSION
9
+ spec.authors = ["Jonas Nicklas"]
10
+ spec.email = ["jonas.nicklas@gmail.com"]
11
+ spec.summary = %q{Memoize is back!}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = ">= 2.1.0"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,35 @@
1
+ require "memoit"
2
+
3
+ describe Memoit do
4
+ let(:klass) do
5
+ Class.new do
6
+ memoize def foo
7
+ rand
8
+ end
9
+
10
+ memoize def bar(*values)
11
+ rand
12
+ end
13
+ end
14
+ end
15
+ let(:instance) { klass.new }
16
+
17
+ describe ".memoize" do
18
+ it "caches result" do
19
+ expect(instance.foo).to eq(instance.foo)
20
+ end
21
+
22
+ it "caches results for different parameters" do
23
+ a = Object.new
24
+ expect(instance.bar(1)).to eq(instance.bar(1))
25
+ expect(instance.bar(2)).to eq(instance.bar(2))
26
+ expect(instance.bar(a, 1, :foo, "bar")).to eq(instance.bar(a, 1, :foo, "bar"))
27
+ expect(instance.bar(2)).not_to eq(instance.bar(1))
28
+ expect(instance.bar(a, 1, :foo, "bar")).not_to eq(instance.bar(Object.new, 1, :foo, "bar"))
29
+ end
30
+
31
+ it "ignores cache when block given" do
32
+ expect(instance.foo { }).not_to eq(instance.foo { })
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memoit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Nicklas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-14 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - jonas.nicklas@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/memoit.rb
68
+ - lib/memoit/version.rb
69
+ - memoit.gemspec
70
+ - spec/memoit_spec.rb
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.1.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.4.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Memoize is back!
95
+ test_files:
96
+ - spec/memoit_spec.rb