memor 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Guardfile +8 -0
- data/README.md +5 -5
- data/lib/memor.rb +60 -17
- data/lib/memor/version.rb +1 -1
- data/memor.gemspec +2 -0
- data/spec/{memor_spec.rb → lib/memor_spec.rb} +34 -7
- data/spec/spec_helper.rb +13 -0
- metadata +31 -6
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Memor
|
2
2
|
|
3
3
|
## USAGE
|
4
|
-
This lib has one utility method called **memor** which takes *
|
5
|
-
|
4
|
+
This lib has one utility method called **memor** which takes *binding* as an
|
5
|
+
argument, see the following example:
|
6
6
|
|
7
7
|
``` ruby
|
8
8
|
require 'memor'
|
@@ -11,19 +11,19 @@ first argument and all the host methods other arguments, see the following examp
|
|
11
11
|
include Memor
|
12
12
|
|
13
13
|
def slow_method1
|
14
|
-
memor
|
14
|
+
memor binding do
|
15
15
|
# slow stuff
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
def slow_method2(a, b)
|
20
|
-
memor
|
20
|
+
memor binding do
|
21
21
|
# slow stuff
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
def slow_method3(a, *args)
|
26
|
-
memor
|
26
|
+
memor binding do
|
27
27
|
# slow stuff
|
28
28
|
end
|
29
29
|
end
|
data/lib/memor.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'memor/version'
|
2
2
|
|
3
3
|
module Memor
|
4
4
|
|
@@ -7,38 +7,81 @@ module Memor
|
|
7
7
|
# class Foo
|
8
8
|
# include Memor
|
9
9
|
#
|
10
|
-
# def
|
11
|
-
# memor
|
10
|
+
# def foo(a, b)
|
11
|
+
# memor binding do
|
12
|
+
# # slow stuff
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# def bar(a, b)
|
17
|
+
# memor binding do
|
12
18
|
# # slow stuff
|
13
19
|
# end
|
14
20
|
# end
|
15
21
|
# end
|
16
22
|
#
|
17
|
-
def memor(
|
23
|
+
def memor(context_binding)
|
24
|
+
callee = context_binding.eval '__callee__'
|
25
|
+
|
18
26
|
memor_name = "@_memor_#{callee}".gsub('?', '_question_mark')
|
19
27
|
.gsub('!', '_bang')
|
20
28
|
|
21
|
-
|
22
|
-
unless instance_variable_defined? memor_name
|
23
|
-
instance_variable_set memor_name, yield
|
24
|
-
end
|
29
|
+
memor_method = method(callee)
|
25
30
|
|
26
|
-
|
31
|
+
if memor_method.arity == 0 # no argument
|
32
|
+
_memor_with_no_arg(memor_name) { yield }
|
27
33
|
|
28
34
|
else
|
29
|
-
|
30
|
-
|
31
|
-
|
35
|
+
# FIXME args may be insufficient
|
36
|
+
args = _memor_args(memor_method, context_binding)
|
37
|
+
|
38
|
+
_memor_with_args(memor_name, args) { yield }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
32
42
|
|
33
|
-
|
34
|
-
|
43
|
+
def _memor_args(memor_method, context_binding)
|
44
|
+
arg_names = _memor_arg_names(memor_method)
|
35
45
|
|
36
|
-
|
37
|
-
|
46
|
+
arg_names.map do |arg_name|
|
47
|
+
context_binding.eval arg_name.to_s
|
48
|
+
end
|
49
|
+
end
|
50
|
+
private :_memor_args
|
51
|
+
|
52
|
+
def _memor_arg_names(memor_method)
|
53
|
+
# parameters is like [[:req, :a], [:req, :b], [:rest, :c], [:block, :d]]
|
54
|
+
value_parameters = memor_method.parameters.map do |pp|
|
55
|
+
if pp[0] == :req or pp[0] == :rest
|
56
|
+
pp[1]
|
38
57
|
end
|
58
|
+
end.compact
|
59
|
+
end
|
60
|
+
private :_memor_arg_names
|
39
61
|
|
40
|
-
|
62
|
+
def _memor_with_no_arg(memor_name)
|
63
|
+
unless instance_variable_defined? memor_name
|
64
|
+
instance_variable_set memor_name, yield
|
41
65
|
end
|
66
|
+
|
67
|
+
instance_variable_get memor_name
|
68
|
+
end
|
69
|
+
private :_memor_with_no_arg
|
70
|
+
|
71
|
+
def _memor_with_args(memor_name, args)
|
72
|
+
unless instance_variable_defined? memor_name
|
73
|
+
instance_variable_set memor_name, {}
|
74
|
+
end
|
75
|
+
|
76
|
+
memor = instance_variable_get memor_name
|
77
|
+
key = [* args] # use the args array as the cache key
|
78
|
+
|
79
|
+
unless memor.has_key?(key)
|
80
|
+
memor[key] = yield
|
81
|
+
end
|
82
|
+
|
83
|
+
memor[key]
|
42
84
|
end
|
85
|
+
private :_memor_with_args
|
43
86
|
|
44
87
|
end
|
data/lib/memor/version.rb
CHANGED
data/memor.gemspec
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'spec_helper'
|
1
2
|
require 'memor'
|
2
3
|
|
3
4
|
describe Memor do
|
@@ -11,31 +12,37 @@ describe Memor do
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def no_arg
|
14
|
-
memor
|
15
|
+
memor binding do
|
15
16
|
slow_method
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
20
|
def with_args1(a, b)
|
20
|
-
memor
|
21
|
+
memor binding do
|
21
22
|
slow_method
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
25
26
|
def with_args2(*args)
|
26
|
-
memor
|
27
|
+
memor binding do
|
28
|
+
slow_method
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def with_args3(a, *args)
|
33
|
+
memor binding do
|
27
34
|
slow_method
|
28
35
|
end
|
29
36
|
end
|
30
37
|
|
31
38
|
def query?
|
32
|
-
memor
|
39
|
+
memor binding do
|
33
40
|
slow_method
|
34
41
|
end
|
35
42
|
end
|
36
43
|
|
37
44
|
def bang!
|
38
|
-
memor
|
45
|
+
memor binding do
|
39
46
|
slow_method
|
40
47
|
end
|
41
48
|
end
|
@@ -59,15 +66,28 @@ describe Memor do
|
|
59
66
|
it 'fix arguments' do
|
60
67
|
foo.with_args1(1, 2).should == 'slow'
|
61
68
|
foo.with_args1(1, 2).should == 'slow'
|
69
|
+
foo.with_args1(2, 2).should == 'slow'
|
70
|
+
foo.with_args1(2, 2).should == 'slow'
|
62
71
|
|
63
|
-
foo.slows.should ==
|
72
|
+
foo.slows.should == 2
|
64
73
|
end
|
65
74
|
|
66
75
|
it 'splat arguments' do
|
67
76
|
foo.with_args2('bar').should == 'slow'
|
68
77
|
foo.with_args2('bar').should == 'slow'
|
78
|
+
foo.with_args2('foo').should == 'slow'
|
79
|
+
foo.with_args2('foo').should == 'slow'
|
69
80
|
|
70
|
-
foo.slows.should ==
|
81
|
+
foo.slows.should == 2
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'normal arguments and splat arguments' do
|
85
|
+
foo.with_args3('bar', 4).should == 'slow'
|
86
|
+
foo.with_args3('bar', 4).should == 'slow'
|
87
|
+
foo.with_args3('bar', 5).should == 'slow'
|
88
|
+
foo.with_args3('bar', 5).should == 'slow'
|
89
|
+
|
90
|
+
foo.slows.should == 2
|
71
91
|
end
|
72
92
|
|
73
93
|
it '! in method name' do
|
@@ -83,4 +103,11 @@ describe Memor do
|
|
83
103
|
|
84
104
|
foo.slows.should == 1
|
85
105
|
end
|
106
|
+
|
107
|
+
it '#_memor_parameters' do
|
108
|
+
foo.send(:_memor_arg_names, foo.method(:no_arg)).should == []
|
109
|
+
foo.send(:_memor_arg_names, foo.method(:with_args1)).should == [:a, :b]
|
110
|
+
foo.send(:_memor_arg_names, foo.method(:with_args2)).should == [:args]
|
111
|
+
foo.send(:_memor_arg_names, foo.method(:with_args3)).should == [:a, :args]
|
112
|
+
end
|
86
113
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
# ## Mock Framework
|
3
|
+
#
|
4
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
5
|
+
#
|
6
|
+
# config.mock_with :mocha
|
7
|
+
# config.mock_with :flexmock
|
8
|
+
# config.mock_with :rr
|
9
|
+
|
10
|
+
config.filter_run focus: true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-23 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &10183170 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,29 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *10183170
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: guard-rspec
|
27
|
+
requirement: &10182960 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *10182960
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: ruby-debug19
|
38
|
+
requirement: &10182750 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *10182750
|
25
47
|
description: memoize function without alias method chain
|
26
48
|
email:
|
27
49
|
- Aaron2Ti@gmail.com
|
@@ -32,13 +54,15 @@ files:
|
|
32
54
|
- .gitignore
|
33
55
|
- .travis.yml
|
34
56
|
- Gemfile
|
57
|
+
- Guardfile
|
35
58
|
- MIT-LICENSE
|
36
59
|
- README.md
|
37
60
|
- Rakefile
|
38
61
|
- lib/memor.rb
|
39
62
|
- lib/memor/version.rb
|
40
63
|
- memor.gemspec
|
41
|
-
- spec/memor_spec.rb
|
64
|
+
- spec/lib/memor_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
42
66
|
homepage: https://rubygems.org/gems/memor
|
43
67
|
licenses: []
|
44
68
|
post_install_message:
|
@@ -64,5 +88,6 @@ signing_key:
|
|
64
88
|
specification_version: 3
|
65
89
|
summary: simple memoize function without alias method chain
|
66
90
|
test_files:
|
67
|
-
- spec/memor_spec.rb
|
91
|
+
- spec/lib/memor_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
68
93
|
has_rdoc:
|