mem 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62d24793e77a9b1f81cd1695cc7885f3c9d24ceb
4
- data.tar.gz: 47c3486cc3b2cb33cab110754ed307df7b6231a1
3
+ metadata.gz: ac41bdc5119cf45f7824c911ae16f5df3e2c808d
4
+ data.tar.gz: 1f63987a155340dacbffc0d19648bdc08b5c9970
5
5
  SHA512:
6
- metadata.gz: fabce24420248b57a76e99cccf9469a4dc7684ca05c2b5f8f7312817943d278224a962b75051f15a13d0730daeedaa912d0427a0c3bc057901fb70175ac90ba7
7
- data.tar.gz: 8b94d0885c1b9f7f819ea5ff9fdbea2ca1fc9ed71148f2ee01ee659ea9c501197207fb318373f8fe266f52413e72ea70318c6e7cd88342ca0e1153f6f065789a
6
+ metadata.gz: 3166d66167e8b19d45e281f57c289cee57d34ecd0eca5e3f0608bac09381a45b8b596d675cacebbe1f5714fbc8b8feac9384905939049af65d470abe2810ec05
7
+ data.tar.gz: a9b281277509b6731eda20c6c65d8985de7a930d8f4fc4fa70b057df093b7ab92a68e71ab325a23497180845287a2025dcbcfec0d45045abaaf8a1901066c688
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## 0.1.0
2
+ * Change interface from `extend Mem` to `include Mem`
3
+ * Change stored data structure from ivar to hash table
4
+ * Add some instance methods to inspect memoized table
5
+
6
+ ## 0.0.2
7
+ * Remove dependency on ActiveSupport
8
+
9
+ ## 0.0.1
10
+ * 1st release
data/README.md CHANGED
@@ -9,7 +9,7 @@ gem install mem
9
9
  ## Usage
10
10
  ```ruby
11
11
  class Foo
12
- extend Mem
12
+ include Mem
13
13
 
14
14
  def initialize
15
15
  @count = 0
@@ -20,7 +20,7 @@ class Foo
20
20
  end
21
21
 
22
22
  # `memoize` defines bar_with_memoize & bar_without_memoize,
23
- # and the result of the 1st method call is stored into @bar.
23
+ # and the result of the 1st method call is stored into @memoized_table.
24
24
  memoize :bar
25
25
 
26
26
  private
@@ -34,4 +34,7 @@ foo = Foo.new
34
34
  foo.bar #=> 1
35
35
  foo.bar #=> 1
36
36
  foo.bar #=> 1
37
+ foo.has_memoized?(:bar) #=> true
38
+ foo.memoized(:bar) #=> 1
39
+ foo.memoized_table #=> { bar: 1 }
37
40
  ```
data/lib/mem/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mem
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/mem.rb CHANGED
@@ -1,15 +1,37 @@
1
1
  require "mem/version"
2
2
 
3
3
  module Mem
4
- def memoize(method_name)
5
- define_method("#{method_name}_with_memoize") do |*args, &block|
6
- if instance_variable_defined?("@#{method_name}")
7
- instance_variable_get("@#{method_name}")
8
- else
9
- instance_variable_set("@#{method_name}", send("#{method_name}_without_memoize", *args, &block))
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def has_memoized?(key)
9
+ memoized_table.key?(key)
10
+ end
11
+
12
+ def memoize(key, value)
13
+ memoized_table[key] = value
14
+ end
15
+
16
+ def memoized(key)
17
+ memoized_table[key]
18
+ end
19
+
20
+ def memoized_table
21
+ @memoized_table ||= {}
22
+ end
23
+
24
+ module ClassMethods
25
+ def memoize(method_name)
26
+ define_method("#{method_name}_with_memoize") do |*args, &block|
27
+ if has_memoized?(method_name)
28
+ memoized(method_name)
29
+ else
30
+ memoize(method_name, send("#{method_name}_without_memoize", *args, &block))
31
+ end
10
32
  end
33
+ alias_method "#{method_name}_without_memoize", method_name
34
+ alias_method method_name, "#{method_name}_with_memoize"
11
35
  end
12
- alias_method "#{method_name}_without_memoize", method_name
13
- alias_method method_name, "#{method_name}_with_memoize"
14
36
  end
15
37
  end
data/mem.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ["lib"]
18
18
 
19
- spec.add_development_dependency "bundler", "~> 1.5"
19
+ spec.add_development_dependency "bundler"
20
20
  spec.add_development_dependency "rake"
21
21
  spec.add_development_dependency "rspec", "2.14.1"
22
22
  end
data/spec/mem_spec.rb CHANGED
@@ -2,28 +2,57 @@ require "spec_helper"
2
2
 
3
3
  describe Mem do
4
4
  describe "#memoize" do
5
- let(:klass) do
6
- Class.new do
7
- extend Mem
5
+ let(:object) do
6
+ klass.new
7
+ end
8
8
 
9
- def foo(&block)
10
- bar(&block)
11
- end
12
- memoize :foo
9
+ context "with normal method name" do
10
+ let(:klass) do
11
+ Class.new do
12
+ include Mem
13
13
 
14
- def bar
15
- yield
14
+ def a(value, &block)
15
+ b(value, &block)
16
+ end
17
+ memoize :a
18
+
19
+ def b(value)
20
+ [value, yield]
21
+ end
16
22
  end
17
23
  end
18
- end
19
24
 
20
- let(:object) do
21
- klass.new
25
+ it "memoizes the method call" do
26
+ expect(object).to receive(:b).once.and_call_original
27
+ expect(object.a(1) { 2 }).to eq [1, 2]
28
+ expect(object.a(3) { 4 }).to eq [1, 2]
29
+ end
22
30
  end
23
31
 
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"
32
+ context "with normal method name" do
33
+ let(:klass) do
34
+ Class.new do
35
+ include Mem
36
+
37
+ def a!(value, &block)
38
+ b(value, &block)
39
+ end
40
+ memoize :a!
41
+
42
+ def b(value)
43
+ [value, yield]
44
+ end
45
+ end
46
+ end
47
+
48
+ it "memoizes the method call" do
49
+ expect(object).to receive(:b).once.and_call_original
50
+ expect(object.a!(1) { 2 }).to eq [1, 2]
51
+ expect(object.a!(3) { 4 }).to eq [1, 2]
52
+ object.should have_memoized(:a!)
53
+ object.memoized(:a!).should == [1, 2]
54
+ object.memoized_table.should == { a!: [1, 2] }
55
+ end
27
56
  end
28
57
  end
29
58
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-19 00:00:00.000000000 Z
11
+ date: 2013-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - .gitignore
63
+ - CHANGELOG.md
63
64
  - Gemfile
64
65
  - LICENSE.txt
65
66
  - README.md