memoist2 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +4 -0
- data/Rakefile +71 -0
- data/example/foo.rb +58 -0
- data/lib/memoist2.rb +22 -0
- data/spec/memoist2_spec.rb +68 -0
- data/spec/spec_helper.rb +17 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9469f4a69b93e831b13b1ffb25a5c1dde24d0b4f
|
4
|
+
data.tar.gz: 0f62199fc4a58d8011e9a497eabb4dca0bba474a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 240188ec5c43029bc30607098a97231761af2e3653608b8eec6b5c2bee1e95287a0be170186721e11c98a342ba18fd25d673e75e04c7490f520bdf3ebefb04be
|
7
|
+
data.tar.gz: edb58147414a0db8c312f21cee8fbcde1ecdb1013838efa3ff2f69f8a44e1b0f3e1563a7e3cae2db4853386aa3bb69011b3c49a4ca9b9c581d06b1fe6ee1e19f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
RSpec::Core::RakeTask.new(:spec)
|
3
|
+
task :default => :spec
|
4
|
+
|
5
|
+
require "rubygems"
|
6
|
+
require "rubygems/package_task"
|
7
|
+
require "rdoc/task"
|
8
|
+
|
9
|
+
# This builds the actual gem. For details of what all these options
|
10
|
+
# mean, and other ones you can add, check the documentation here:
|
11
|
+
#
|
12
|
+
# http://rubygems.org/read/chapter/20
|
13
|
+
#
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
|
16
|
+
# Basic details
|
17
|
+
s.name = "memoist2"
|
18
|
+
s.version = "0.1.0"
|
19
|
+
s.summary = "Really simple memoization for ruby 2.0"
|
20
|
+
s.author = "Matthew Rudy Jacobs"
|
21
|
+
s.email = "MatthewRudyJacobs@gmail.com"
|
22
|
+
s.homepage = "https://github.com/matthewrudy/memoist2"
|
23
|
+
|
24
|
+
s.has_rdoc = true
|
25
|
+
s.extra_rdoc_files = %w(README.md)
|
26
|
+
s.rdoc_options = %w(--main README.md)
|
27
|
+
|
28
|
+
# Add any extra files to include in the gem
|
29
|
+
s.files = `git ls-files`.split
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# Dependencies to run tests
|
33
|
+
s.add_development_dependency("rspec")
|
34
|
+
s.add_development_dependency("rake")
|
35
|
+
end
|
36
|
+
|
37
|
+
# This task actually builds the gem. We also regenerate a static
|
38
|
+
# .gemspec file, which is useful if something (i.e. GitHub) will
|
39
|
+
# be automatically building a gem for this project. If you're not
|
40
|
+
# using GitHub, edit as appropriate.
|
41
|
+
#
|
42
|
+
# To publish your gem online, install the 'gemcutter' gem; Read more
|
43
|
+
# about that here: http://gemcutter.org/pages/gem_docs
|
44
|
+
Gem::PackageTask.new(spec) do |pkg|
|
45
|
+
pkg.gem_spec = spec
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Build the gemspec file #{spec.name}.gemspec"
|
49
|
+
task :gemspec do
|
50
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
51
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
52
|
+
end
|
53
|
+
|
54
|
+
# If you don't want to generate the .gemspec file, just remove this line. Reasons
|
55
|
+
# why you might want to generate a gemspec:
|
56
|
+
# - using bundler with a git source
|
57
|
+
# - building the gem without rake (i.e. gem build blah.gemspec)
|
58
|
+
# - maybe others?
|
59
|
+
task :package => :gemspec
|
60
|
+
|
61
|
+
# Generate documentation
|
62
|
+
RDoc::Task.new do |rd|
|
63
|
+
rd.main = "README.md"
|
64
|
+
rd.rdoc_files.include("README.md", "lib/**/*.rb")
|
65
|
+
rd.rdoc_dir = "rdoc"
|
66
|
+
end
|
67
|
+
|
68
|
+
desc 'Clear out RDoc and generated packages'
|
69
|
+
task :clean => [:clobber_rdoc, :clobber_package] do
|
70
|
+
rm "#{spec.name}.gemspec"
|
71
|
+
end
|
data/example/foo.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.expand_path('../../lib/memoist2', __FILE__)
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
extend Memoist2
|
5
|
+
|
6
|
+
def string
|
7
|
+
"string"
|
8
|
+
end
|
9
|
+
|
10
|
+
def string_memoized
|
11
|
+
"string"
|
12
|
+
end
|
13
|
+
memoize :string_memoized
|
14
|
+
|
15
|
+
def fixnum
|
16
|
+
42
|
17
|
+
end
|
18
|
+
|
19
|
+
def fixnum_memoized
|
20
|
+
42
|
21
|
+
end
|
22
|
+
memoize :fixnum_memoized
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'benchmark'
|
26
|
+
|
27
|
+
TIMES = 10_000_000
|
28
|
+
|
29
|
+
Benchmark.bmbm do |x|
|
30
|
+
|
31
|
+
x.report "string - memoized" do
|
32
|
+
foo = Foo.new
|
33
|
+
TIMES.times do
|
34
|
+
foo.string_memoized
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
x.report "string - unmemoized" do
|
39
|
+
foo = Foo.new
|
40
|
+
TIMES.times do
|
41
|
+
foo.string
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
x.report "fixnum - memoized" do
|
46
|
+
foo = Foo.new
|
47
|
+
TIMES.times do
|
48
|
+
foo.fixnum_memoized
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
x.report "fixnum - unmemoized" do
|
53
|
+
foo = Foo.new
|
54
|
+
TIMES.times do
|
55
|
+
foo.fixnum
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/memoist2.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Memoist2
|
2
|
+
|
3
|
+
def self.memoized_ivar_for(symbol)
|
4
|
+
"@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
|
5
|
+
end
|
6
|
+
|
7
|
+
def memoize(method_name)
|
8
|
+
memoized_ivar = Memoist2.memoized_ivar_for(method_name)
|
9
|
+
memoized_module = Module.new do
|
10
|
+
module_eval <<-EVAL
|
11
|
+
def #{method_name}
|
12
|
+
if !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty?
|
13
|
+
#{memoized_ivar} = [super]
|
14
|
+
end
|
15
|
+
#{memoized_ivar}[0]
|
16
|
+
end
|
17
|
+
EVAL
|
18
|
+
end
|
19
|
+
prepend memoized_module
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'memoist2'
|
2
|
+
|
3
|
+
describe Memoist2 do
|
4
|
+
|
5
|
+
class Foo
|
6
|
+
extend Memoist2
|
7
|
+
|
8
|
+
def bar
|
9
|
+
count!
|
10
|
+
end
|
11
|
+
memoize :bar
|
12
|
+
|
13
|
+
def question?
|
14
|
+
count!
|
15
|
+
end
|
16
|
+
memoize :question?
|
17
|
+
|
18
|
+
def bang!
|
19
|
+
count!
|
20
|
+
end
|
21
|
+
memoize :bang!
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def count!
|
26
|
+
@count ||= 0
|
27
|
+
@count += 1
|
28
|
+
end
|
29
|
+
|
30
|
+
class << self
|
31
|
+
extend Memoist2
|
32
|
+
|
33
|
+
def bar
|
34
|
+
@bar_count ||= 0
|
35
|
+
@bar_count += 1
|
36
|
+
end
|
37
|
+
memoize :bar
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "instance methods" do
|
42
|
+
subject{ Foo.new }
|
43
|
+
|
44
|
+
it "can be memoized" do
|
45
|
+
subject.bar.should == subject.bar
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "class methods" do
|
50
|
+
subject{ Foo }
|
51
|
+
|
52
|
+
it "can be memoized" do
|
53
|
+
subject.bar.should == subject.bar
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "punctuated methods" do
|
58
|
+
subject{ Foo.new }
|
59
|
+
|
60
|
+
it "can memoize question methods" do
|
61
|
+
subject.question?.should == subject.question?
|
62
|
+
end
|
63
|
+
|
64
|
+
it "can memoize bang methods" do
|
65
|
+
subject.bang!.should == subject.bang!
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memoist2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Rudy Jacobs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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
|
+
description:
|
42
|
+
email: MatthewRudyJacobs@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files:
|
46
|
+
- README.md
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- .ruby-version
|
51
|
+
- .travis.yml
|
52
|
+
- Gemfile
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- example/foo.rb
|
56
|
+
- lib/memoist2.rb
|
57
|
+
- spec/memoist2_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: https://github.com/matthewrudy/memoist2
|
60
|
+
licenses: []
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --main
|
65
|
+
- README.md
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.0.3
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Really simple memoization for ruby 2.0
|
84
|
+
test_files: []
|