mize 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +10 -0
- data/.utilsrc +26 -0
- data/Gemfile +5 -0
- data/README.md +39 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/config/broken_config.yml +1 -0
- data/config/config.yml +13 -0
- data/lib/mize/cache_methods.rb +52 -0
- data/lib/mize/cache_protocol.rb +32 -0
- data/lib/mize/configure.rb +17 -0
- data/lib/mize/default_cache.rb +33 -0
- data/lib/mize/global_clear.rb +41 -0
- data/lib/mize/memoize.rb +61 -0
- data/lib/mize/railtie.rb +8 -0
- data/lib/mize/reload.rb +12 -0
- data/lib/mize/version.rb +8 -0
- data/lib/mize.rb +12 -0
- data/mize.gemspec +53 -0
- data/spec/mize/global_clear_spec.rb +31 -0
- data/spec/mize/memoize_method_spec.rb +124 -0
- data/spec/mize/memoize_reload_spec.rb +34 -0
- data/spec/mize/version_spec.rb +8 -0
- data/spec/spec_helper.rb +16 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a3d262bfae6dc90882b9a6deeb29a655bebd647
|
4
|
+
data.tar.gz: 9e606ff6fae19a19b6c83f263e3d6ae58f18589a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2754ca93ed9e827b50d3f8528a535a9ac69790d8defbc9510d0408d4a0128a7f3b8a3356f5420a856cbb141b8fb42c187732be8ee934d6d03fb1a1db40b91c5
|
7
|
+
data.tar.gz: 715c53aa026e8270bcc9fc9e4fed0a35bafdc6514b57ce11c023d1e2797f113b073a7c2e9c2220bf5a374c1dda809e64443b54b4f5c6e9fe81716eec275144e9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.utilsrc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# vim: set ft=ruby:
|
2
|
+
|
3
|
+
search do
|
4
|
+
prune_dirs /\A(\.svn|\.git|CVS|tmp|tags|coverage|pkg)\z/
|
5
|
+
skip_files /(\A\.|\.sw[pon]\z|\.(log|fnm|jpg|jpeg|png|pdf|svg)\z|tags|~\z)/i
|
6
|
+
end
|
7
|
+
|
8
|
+
discover do
|
9
|
+
prune_dirs /\A(\.svn|\.git|CVS|tmp|tags|coverage|pkg)\z/
|
10
|
+
skip_files /(\A\.|\.sw[pon]\z|\.log\z|~\z)/
|
11
|
+
binary false
|
12
|
+
end
|
13
|
+
|
14
|
+
strip_spaces do
|
15
|
+
prune_dirs /\A(\..*|CVS|pkg)\z/
|
16
|
+
skip_files /(\A\.|\.sw[pon]\z|\.log\z|~\z)/
|
17
|
+
end
|
18
|
+
|
19
|
+
probe do
|
20
|
+
test_framework :rspec
|
21
|
+
#include_dirs 'features'
|
22
|
+
end
|
23
|
+
|
24
|
+
ssh_tunnel do
|
25
|
+
terminal_multiplexer :tmux
|
26
|
+
end
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Mize
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
You can use rubygems to fetch the gem and install it for you:
|
9
|
+
|
10
|
+
# gem install mize
|
11
|
+
|
12
|
+
You can also put this line into your Gemfile
|
13
|
+
|
14
|
+
gem 'mize'
|
15
|
+
|
16
|
+
and bundle.
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
### Configuration
|
21
|
+
|
22
|
+
|
23
|
+
## Changes
|
24
|
+
|
25
|
+
* 2015-12-10 Release 0.0.0
|
26
|
+
|
27
|
+
## Download
|
28
|
+
|
29
|
+
The homepage of this library is located at
|
30
|
+
|
31
|
+
* https://github.com/flori/mize
|
32
|
+
|
33
|
+
## Author
|
34
|
+
|
35
|
+
[Florian Frank](mailto:flori@ping.de)
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
This software is licensed under MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# vim: set filetype=ruby et sw=2 ts=2:
|
2
|
+
|
3
|
+
require 'gem_hadar'
|
4
|
+
|
5
|
+
GemHadar do
|
6
|
+
name 'mize'
|
7
|
+
author 'Florian Frank'
|
8
|
+
email 'flori@ping.de'
|
9
|
+
homepage "http://flori.github.com/#{name}"
|
10
|
+
summary 'Library that provides memoziation for methods and functions'
|
11
|
+
description "#{summary} for Ruby."
|
12
|
+
readme 'README.md'
|
13
|
+
licenses << 'MIT'
|
14
|
+
|
15
|
+
test_dir 'spec'
|
16
|
+
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc',
|
17
|
+
'.AppleDouble', 'tags', '.byebug_history', '.yard*', 'errors.lst'
|
18
|
+
|
19
|
+
|
20
|
+
dependency 'protocol'
|
21
|
+
development_dependency 'rake'
|
22
|
+
development_dependency 'simplecov'
|
23
|
+
development_dependency 'rspec'
|
24
|
+
development_dependency 'yard'
|
25
|
+
development_dependency 'byebug'
|
26
|
+
|
27
|
+
required_ruby_version '>= 2'
|
28
|
+
end
|
29
|
+
|
30
|
+
namespace :yard do
|
31
|
+
require 'pathname'
|
32
|
+
|
33
|
+
yard_dir = Pathname.new('yard')
|
34
|
+
|
35
|
+
desc 'Create yard documentation'
|
36
|
+
task :create do
|
37
|
+
sh 'yardoc'
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'View the yard documentation'
|
41
|
+
task :view do
|
42
|
+
index_file = yard_dir + 'index.html'
|
43
|
+
File.exist?(index_file)
|
44
|
+
sh "open #{index_file}"
|
45
|
+
end
|
46
|
+
|
47
|
+
desc 'Clean the yard documentation'
|
48
|
+
task :clean do
|
49
|
+
rm_rf yard_dir
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
task :yard => %i[ yard:create yard:view ]
|
54
|
+
|
55
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1 @@
|
|
1
|
+
,kaput
|
data/config/config.yml
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'mize/cache_protocol'
|
2
|
+
require 'mize/default_cache'
|
3
|
+
|
4
|
+
module Mize::CacheMethods
|
5
|
+
# Clear cached values for all methods/functions of this object.
|
6
|
+
def mize_cache_clear
|
7
|
+
__mize_cache__.clear
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
# Set the cache object to +cache+.
|
14
|
+
def __mize_cache__=(cache)
|
15
|
+
Mize::CacheProtocol.check cache
|
16
|
+
@__mize_cache__ = cache
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return the cache object.
|
20
|
+
def __mize_cache__
|
21
|
+
if @__mize_cache__
|
22
|
+
@__mize_cache__
|
23
|
+
else
|
24
|
+
self.__mize_cache__ = Mize.default_cache.prototype
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Build a key prefix for +name+.
|
29
|
+
def build_key_prefix(name)
|
30
|
+
"mize/#{name}"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Build a +name+ prefixed key for the arguments +args+.
|
34
|
+
def build_key(name, *args)
|
35
|
+
"#{build_key_prefix(name)}/#{Marshal.dump(args)}"
|
36
|
+
end
|
37
|
+
|
38
|
+
# Apply the visibility of method +id+ to the wrapper method of this method.
|
39
|
+
def memoize_apply_visibility(id)
|
40
|
+
visibility = instance_eval do
|
41
|
+
case
|
42
|
+
when private_method_defined?(id)
|
43
|
+
:private
|
44
|
+
when protected_method_defined?(id)
|
45
|
+
:protected
|
46
|
+
end
|
47
|
+
end
|
48
|
+
yield
|
49
|
+
ensure
|
50
|
+
visibility and __send__(visibility, id)
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'protocol'
|
2
|
+
|
3
|
+
# The protocol the cache object has to conform to. This is a minimal subset of
|
4
|
+
# the ActiveSupport::Cache::Store protocol.
|
5
|
+
Mize::CacheProtocol = Protocol do
|
6
|
+
# Clear the entire cache.
|
7
|
+
def clear(options = nil)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns true if the cache contains an entry for the given key.
|
11
|
+
def exist?(name, options = nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Fetches data from the cache, using the given key. If there is data in the
|
15
|
+
# cache with the given key, then that data is returned. Otherwise, nil is
|
16
|
+
# returned.
|
17
|
+
def read(name, options = nil)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Writes the value to the cache, with the key.
|
21
|
+
def write(name, value, options = nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Iterate over all cache entries and yield to their names.
|
25
|
+
def each_name(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Return another cache instance that was correctly configured.
|
29
|
+
def prototype
|
30
|
+
dup
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Mize::Configure
|
2
|
+
attr_accessor :default_cache
|
3
|
+
|
4
|
+
def cache(cache)
|
5
|
+
self.default_cache = cache
|
6
|
+
end
|
7
|
+
|
8
|
+
def configure(&block)
|
9
|
+
instance_eval(&block)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Mize
|
14
|
+
extend Mize::Configure
|
15
|
+
end
|
16
|
+
|
17
|
+
Mize.default_cache = Mize::DefaultCache.new
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
|
3
|
+
class Mize::DefaultCache < Hash
|
4
|
+
include MonitorMixin
|
5
|
+
|
6
|
+
private(*(instance_methods - Object.instance_methods - %i[ synchronize ]))
|
7
|
+
|
8
|
+
def clear(options = nil)
|
9
|
+
super()
|
10
|
+
end
|
11
|
+
|
12
|
+
def exist?(name, options = nil)
|
13
|
+
key?(name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def read(name, options = nil)
|
17
|
+
fetch(name, nil)
|
18
|
+
end
|
19
|
+
|
20
|
+
def write(name, value, options = nil)
|
21
|
+
store(name, value)
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete(name, options = nil)
|
25
|
+
super(name)
|
26
|
+
end
|
27
|
+
|
28
|
+
def each_name(&block)
|
29
|
+
each_key(&block)
|
30
|
+
end
|
31
|
+
|
32
|
+
alias prototype dup
|
33
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
|
3
|
+
module Mize
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Clear all memoization caches at once.
|
7
|
+
def cache_clear
|
8
|
+
each_cache do |id, cache|
|
9
|
+
cache and cache.clear
|
10
|
+
cache_tracker.delete id
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def each_cache
|
17
|
+
cache_tracker.synchronize do
|
18
|
+
cache_tracker.each_name do |id|
|
19
|
+
begin
|
20
|
+
cache = ObjectSpace._id2ref id
|
21
|
+
rescue RangeError
|
22
|
+
end
|
23
|
+
yield id, cache
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def cache_tracker
|
29
|
+
@cache_tracker ||= Mize.default_cache.dup
|
30
|
+
end
|
31
|
+
|
32
|
+
def track_cache(cache_id)
|
33
|
+
cache_tracker.exist?(cache_id) and return
|
34
|
+
cache_tracker.synchronize do
|
35
|
+
cache_tracker.write cache_id, true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
cache_tracker
|
41
|
+
end
|
data/lib/mize/memoize.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'mize/cache_methods'
|
2
|
+
require 'mize/reload'
|
3
|
+
|
4
|
+
module Mize
|
5
|
+
module Memoize
|
6
|
+
include CacheMethods
|
7
|
+
|
8
|
+
# Memoize either a +method+ or a +function+. In the former case the
|
9
|
+
# memoized results do NOT ONLY depend on the arguments, but ALSO on the
|
10
|
+
# object the method is called on. In the latter the memoized results ONLY
|
11
|
+
# depend on the arguments given to the function. If the +freeze+ argument
|
12
|
+
# is true, the result is frozen if possible to make it immutable.
|
13
|
+
def memoize(method: nil, function: nil, freeze: false)
|
14
|
+
if method && function
|
15
|
+
raise ArgumentError, 'memoize a method xor a function'
|
16
|
+
elsif method
|
17
|
+
wrap_method method, freeze: freeze
|
18
|
+
elsif function
|
19
|
+
wrap_method function, function: true, freeze: freeze
|
20
|
+
else
|
21
|
+
raise ArgumentError, 'missing keyword: method/function'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def wrap_method(method_id, freeze: false, function: false)
|
28
|
+
include CacheMethods
|
29
|
+
|
30
|
+
mc = __mize_cache__
|
31
|
+
|
32
|
+
unless function
|
33
|
+
prepend Mize::Reload
|
34
|
+
end
|
35
|
+
|
36
|
+
method_id = method_id.to_s.to_sym
|
37
|
+
memoize_apply_visibility method_id do
|
38
|
+
orig_method = instance_method(method_id)
|
39
|
+
__send__(:define_method, method_id) do |*args|
|
40
|
+
function or mc = __mize_cache__
|
41
|
+
Mize.__send__ :track_cache, mc.__id__
|
42
|
+
key = build_key(method_id, *args)
|
43
|
+
if mc.exist?(key) and result = mc.read(key)
|
44
|
+
result
|
45
|
+
else
|
46
|
+
result = orig_method.bind(self).call(*args)
|
47
|
+
mc.write(key, result)
|
48
|
+
if $DEBUG
|
49
|
+
warn "#{self.class} cached method "\
|
50
|
+
"#{method_id}(#{args.inspect unless args.empty?}) = "\
|
51
|
+
"#{result.inspect} [#{__id__}]"
|
52
|
+
end
|
53
|
+
freeze and result.freeze rescue result
|
54
|
+
end
|
55
|
+
result
|
56
|
+
end
|
57
|
+
end
|
58
|
+
method_id
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/mize/railtie.rb
ADDED
data/lib/mize/reload.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Mize::Reload
|
2
|
+
# Reload this object if a #reload method was provided up somewhere in the
|
3
|
+
# inheritance tree, afterwards clear the memoization cache and return self.
|
4
|
+
def reload(*)
|
5
|
+
result = self
|
6
|
+
if defined? super
|
7
|
+
result = super
|
8
|
+
end
|
9
|
+
mize_cache_clear
|
10
|
+
result
|
11
|
+
end
|
12
|
+
end
|
data/lib/mize/version.rb
ADDED
data/lib/mize.rb
ADDED
data/mize.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
# stub: mize 0.0.0 ruby lib
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "mize"
|
6
|
+
s.version = "0.0.0"
|
7
|
+
|
8
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
9
|
+
s.require_paths = ["lib"]
|
10
|
+
s.authors = ["Florian Frank"]
|
11
|
+
s.date = "2016-01-28"
|
12
|
+
s.description = "Library that provides memoziation for methods and functions for Ruby."
|
13
|
+
s.email = "flori@ping.de"
|
14
|
+
s.extra_rdoc_files = ["README.md", "lib/mize.rb", "lib/mize/cache_methods.rb", "lib/mize/cache_protocol.rb", "lib/mize/configure.rb", "lib/mize/default_cache.rb", "lib/mize/global_clear.rb", "lib/mize/memoize.rb", "lib/mize/railtie.rb", "lib/mize/reload.rb", "lib/mize/version.rb"]
|
15
|
+
s.files = [".gitignore", ".rspec", ".travis.yml", ".utilsrc", "Gemfile", "README.md", "Rakefile", "VERSION", "config/broken_config.yml", "config/config.yml", "lib/mize.rb", "lib/mize/cache_methods.rb", "lib/mize/cache_protocol.rb", "lib/mize/configure.rb", "lib/mize/default_cache.rb", "lib/mize/global_clear.rb", "lib/mize/memoize.rb", "lib/mize/railtie.rb", "lib/mize/reload.rb", "lib/mize/version.rb", "mize.gemspec", "spec/mize/global_clear_spec.rb", "spec/mize/memoize_method_spec.rb", "spec/mize/memoize_reload_spec.rb", "spec/mize/version_spec.rb", "spec/spec_helper.rb"]
|
16
|
+
s.homepage = "http://flori.github.com/mize"
|
17
|
+
s.licenses = ["MIT"]
|
18
|
+
s.rdoc_options = ["--title", "Mize - Library that provides memoziation for methods and functions", "--main", "README.md"]
|
19
|
+
s.required_ruby_version = Gem::Requirement.new(">= 2")
|
20
|
+
s.rubygems_version = "2.5.1"
|
21
|
+
s.summary = "Library that provides memoziation for methods and functions"
|
22
|
+
s.test_files = ["spec/mize/global_clear_spec.rb", "spec/mize/memoize_method_spec.rb", "spec/mize/memoize_reload_spec.rb", "spec/mize/version_spec.rb", "spec/spec_helper.rb"]
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
s.specification_version = 4
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_development_dependency(%q<gem_hadar>, ["~> 1.3.1"])
|
29
|
+
s.add_development_dependency(%q<rake>, [">= 0"])
|
30
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
31
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
32
|
+
s.add_development_dependency(%q<yard>, [">= 0"])
|
33
|
+
s.add_development_dependency(%q<byebug>, [">= 0"])
|
34
|
+
s.add_runtime_dependency(%q<protocol>, [">= 0"])
|
35
|
+
else
|
36
|
+
s.add_dependency(%q<gem_hadar>, ["~> 1.3.1"])
|
37
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
38
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
39
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
40
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
41
|
+
s.add_dependency(%q<byebug>, [">= 0"])
|
42
|
+
s.add_dependency(%q<protocol>, [">= 0"])
|
43
|
+
end
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<gem_hadar>, ["~> 1.3.1"])
|
46
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
47
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
48
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
49
|
+
s.add_dependency(%q<yard>, [">= 0"])
|
50
|
+
s.add_dependency(%q<byebug>, [">= 0"])
|
51
|
+
s.add_dependency(%q<protocol>, [">= 0"])
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
def self.reset
|
5
|
+
@@foo = nil
|
6
|
+
end
|
7
|
+
|
8
|
+
def reload
|
9
|
+
@reload = true
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def foo(*a)
|
14
|
+
@@foo ||= 0
|
15
|
+
@@foo += 1
|
16
|
+
end
|
17
|
+
memoize method: :foo
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Mize, 'cache_clear' do
|
21
|
+
let(:foo) { Foo.new }
|
22
|
+
|
23
|
+
describe '#cache_clear' do
|
24
|
+
it 'clears cache after Mize.cache_clear' do
|
25
|
+
expect(foo.foo).to eq 1
|
26
|
+
expect(foo.foo).to eq 1
|
27
|
+
Mize.cache_clear
|
28
|
+
expect(foo.foo).to eq 2
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FooBar
|
4
|
+
def self.reset
|
5
|
+
@@foo = nil
|
6
|
+
@@bar = nil
|
7
|
+
@@baz = nil
|
8
|
+
mize_cache_clear
|
9
|
+
end
|
10
|
+
|
11
|
+
def foo(*a)
|
12
|
+
@@foo ||= 0
|
13
|
+
@@foo += 1
|
14
|
+
end
|
15
|
+
memoize method: :foo
|
16
|
+
|
17
|
+
def footsie(*a)
|
18
|
+
@@footsie ||= 0
|
19
|
+
@@footsie += 1
|
20
|
+
end
|
21
|
+
protected :footsie
|
22
|
+
memoize method: :footsie
|
23
|
+
|
24
|
+
def bar(*a)
|
25
|
+
@@bar ||= 0
|
26
|
+
@@bar += 1
|
27
|
+
end
|
28
|
+
memoize function: :bar
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def baz(*a)
|
33
|
+
@@baz ||= 0
|
34
|
+
@@baz += 1
|
35
|
+
end
|
36
|
+
memoize function: :baz
|
37
|
+
end
|
38
|
+
|
39
|
+
describe Mize do
|
40
|
+
before do
|
41
|
+
FooBar.reset
|
42
|
+
class ::Mize::DefaultCache
|
43
|
+
public :empty?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
let(:fb1) { FooBar.new }
|
48
|
+
|
49
|
+
let(:fb2) { FooBar.new }
|
50
|
+
|
51
|
+
context 'memoize method' do
|
52
|
+
it 'can cache methods' do
|
53
|
+
expect(fb1.__send__(:__mize_cache__)).to be_empty
|
54
|
+
expect(fb2.__send__(:__mize_cache__)).to be_empty
|
55
|
+
expect(fb1.foo(1, 2)).to eq 1
|
56
|
+
expect(fb2.foo(1, 2)).to eq 2
|
57
|
+
expect(fb1.foo(1, 2, 3)).to eq 3
|
58
|
+
expect(fb2.foo(1, 2, 3)).to eq 4
|
59
|
+
expect(fb1.foo(1, 2)).to eq 1
|
60
|
+
expect(fb2.foo(1, 2)).to eq 2
|
61
|
+
fb1.mize_cache_clear
|
62
|
+
fb2.mize_cache_clear
|
63
|
+
expect(fb1.__send__(:__mize_cache__)).to be_empty
|
64
|
+
expect(fb2.__send__(:__mize_cache__)).to be_empty
|
65
|
+
expect(fb1.foo(1, 2)).to eq 5
|
66
|
+
expect(fb2.foo(1, 2)).to eq 6
|
67
|
+
expect(fb1.foo(1, 2)).to eq 5
|
68
|
+
expect(fb2.foo(1, 2)).to eq 6
|
69
|
+
expect(fb1.__send__(:__mize_cache__)).not_to be_empty
|
70
|
+
expect(fb2.__send__(:__mize_cache__)).not_to be_empty
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'can cache protected methods' do
|
74
|
+
expect(fb1.__send__(:__mize_cache__)).to be_empty
|
75
|
+
expect(fb2.__send__(:__mize_cache__)).to be_empty
|
76
|
+
expect(fb1.__send__(:footsie, 1, 2)).to eq 1
|
77
|
+
expect(fb2.__send__(:footsie, 1, 2)).to eq 2
|
78
|
+
expect(fb1.__send__(:footsie, 1, 2, 3)).to eq 3
|
79
|
+
expect(fb2.__send__(:footsie, 1, 2, 3)).to eq 4
|
80
|
+
expect(fb1.__send__(:footsie, 1, 2)).to eq 1
|
81
|
+
expect(fb2.__send__(:footsie, 1, 2)).to eq 2
|
82
|
+
fb1.mize_cache_clear
|
83
|
+
fb2.mize_cache_clear
|
84
|
+
expect(fb1.__send__(:__mize_cache__)).to be_empty
|
85
|
+
expect(fb2.__send__(:__mize_cache__)).to be_empty
|
86
|
+
expect(fb1.__send__(:footsie, 1, 2)).to eq 5
|
87
|
+
expect(fb2.__send__(:footsie, 1, 2)).to eq 6
|
88
|
+
expect(fb1.__send__(:footsie, 1, 2)).to eq 5
|
89
|
+
expect(fb2.__send__(:footsie, 1, 2)).to eq 6
|
90
|
+
expect(fb1.__send__(:__mize_cache__)).not_to be_empty
|
91
|
+
expect(fb2.__send__(:__mize_cache__)).not_to be_empty
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'memoize function' do
|
96
|
+
it 'can cache functions' do
|
97
|
+
expect(FooBar.__send__(:__mize_cache__)).to be_empty
|
98
|
+
expect(fb1.bar(1, 2)).to eq 1
|
99
|
+
expect(fb2.bar(1, 2)).to eq 1
|
100
|
+
expect(fb1.bar(1, 2, 3)).to eq 2
|
101
|
+
expect(fb2.bar(1, 2, 3)).to eq 2
|
102
|
+
expect(fb1.bar(1, 2)).to eq 1
|
103
|
+
expect(fb2.bar(1, 2)).to eq 1
|
104
|
+
FooBar.mize_cache_clear
|
105
|
+
expect(fb1.bar(1, 2)).to eq 3
|
106
|
+
expect(fb2.bar(1, 2)).to eq 3
|
107
|
+
expect(FooBar.__send__(:__mize_cache__)).not_to be_empty
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'can cache private functions' do
|
111
|
+
expect(FooBar.__send__(:__mize_cache__)).to be_empty
|
112
|
+
expect(fb1.__send__(:baz, 1, 2)).to eq 1
|
113
|
+
expect(fb2.__send__(:baz, 1, 2)).to eq 1
|
114
|
+
expect(fb1.__send__(:baz, 1, 2, 3)).to eq 2
|
115
|
+
expect(fb2.__send__(:baz, 1, 2, 3)).to eq 2
|
116
|
+
expect(fb1.__send__(:baz, 1, 2)).to eq 1
|
117
|
+
expect(fb2.__send__(:baz, 1, 2)).to eq 1
|
118
|
+
FooBar.mize_cache_clear
|
119
|
+
expect(fb1.__send__(:baz, 1, 2)).to eq 3
|
120
|
+
expect(fb2.__send__(:baz, 1, 2)).to eq 3
|
121
|
+
expect(FooBar.__send__(:__mize_cache__)).not_to be_empty
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Foo
|
4
|
+
def self.reset
|
5
|
+
@@foo = nil
|
6
|
+
end
|
7
|
+
|
8
|
+
def reload
|
9
|
+
@reload = true
|
10
|
+
self
|
11
|
+
end
|
12
|
+
|
13
|
+
def foo(*a)
|
14
|
+
@@foo ||= 0
|
15
|
+
@@foo += 1
|
16
|
+
end
|
17
|
+
memoize method: :foo
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Mize::Reload do
|
21
|
+
before do
|
22
|
+
Foo.reset
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:foo) { Foo.new }
|
26
|
+
|
27
|
+
describe '#reload' do
|
28
|
+
it 'clears cache after reload' do
|
29
|
+
expect(foo.foo).to eq 1
|
30
|
+
expect(foo.foo).to eq 1
|
31
|
+
expect(foo.reload.foo).to eq 2
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
if ENV['START_SIMPLECOV'].to_i == 1
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter "#{File.basename(File.dirname(__FILE__))}/"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
if ENV['CODECLIMATE_REPO_TOKEN']
|
8
|
+
require "codeclimate-test-reporter"
|
9
|
+
CodeClimate::TestReporter.start
|
10
|
+
end
|
11
|
+
require 'rspec'
|
12
|
+
begin
|
13
|
+
require 'byebug'
|
14
|
+
rescue LoadError
|
15
|
+
end
|
16
|
+
require 'mize'
|
metadata
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florian Frank
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: gem_hadar
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.3.1
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.3.1
|
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: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: protocol
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Library that provides memoziation for methods and functions for Ruby.
|
112
|
+
email: flori@ping.de
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files:
|
116
|
+
- README.md
|
117
|
+
- lib/mize.rb
|
118
|
+
- lib/mize/cache_methods.rb
|
119
|
+
- lib/mize/cache_protocol.rb
|
120
|
+
- lib/mize/configure.rb
|
121
|
+
- lib/mize/default_cache.rb
|
122
|
+
- lib/mize/global_clear.rb
|
123
|
+
- lib/mize/memoize.rb
|
124
|
+
- lib/mize/railtie.rb
|
125
|
+
- lib/mize/reload.rb
|
126
|
+
- lib/mize/version.rb
|
127
|
+
files:
|
128
|
+
- ".gitignore"
|
129
|
+
- ".rspec"
|
130
|
+
- ".travis.yml"
|
131
|
+
- ".utilsrc"
|
132
|
+
- Gemfile
|
133
|
+
- README.md
|
134
|
+
- Rakefile
|
135
|
+
- VERSION
|
136
|
+
- config/broken_config.yml
|
137
|
+
- config/config.yml
|
138
|
+
- lib/mize.rb
|
139
|
+
- lib/mize/cache_methods.rb
|
140
|
+
- lib/mize/cache_protocol.rb
|
141
|
+
- lib/mize/configure.rb
|
142
|
+
- lib/mize/default_cache.rb
|
143
|
+
- lib/mize/global_clear.rb
|
144
|
+
- lib/mize/memoize.rb
|
145
|
+
- lib/mize/railtie.rb
|
146
|
+
- lib/mize/reload.rb
|
147
|
+
- lib/mize/version.rb
|
148
|
+
- mize.gemspec
|
149
|
+
- spec/mize/global_clear_spec.rb
|
150
|
+
- spec/mize/memoize_method_spec.rb
|
151
|
+
- spec/mize/memoize_reload_spec.rb
|
152
|
+
- spec/mize/version_spec.rb
|
153
|
+
- spec/spec_helper.rb
|
154
|
+
homepage: http://flori.github.com/mize
|
155
|
+
licenses:
|
156
|
+
- MIT
|
157
|
+
metadata: {}
|
158
|
+
post_install_message:
|
159
|
+
rdoc_options:
|
160
|
+
- "--title"
|
161
|
+
- Mize - Library that provides memoziation for methods and functions
|
162
|
+
- "--main"
|
163
|
+
- README.md
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '2'
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
requirements: []
|
177
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 2.5.1
|
179
|
+
signing_key:
|
180
|
+
specification_version: 4
|
181
|
+
summary: Library that provides memoziation for methods and functions
|
182
|
+
test_files:
|
183
|
+
- spec/mize/global_clear_spec.rb
|
184
|
+
- spec/mize/memoize_method_spec.rb
|
185
|
+
- spec/mize/memoize_reload_spec.rb
|
186
|
+
- spec/mize/version_spec.rb
|
187
|
+
- spec/spec_helper.rb
|