simple_memoize 1.0.0 → 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jack Danger Canty
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.txt CHANGED
@@ -2,10 +2,13 @@
2
2
 
3
3
  * http://github.com/JackDanger/simple_memoize/
4
4
 
5
+ Developed for and by the engineers at http://adPickles.com/
6
+
5
7
  == DESCRIPTION:
6
8
 
7
9
  Provides in-memory caching of any Ruby method. It's dead simple and won't get in the way of any of your code.
8
10
  For something more robust that offers persistence try http://github.com/JackDanger/cached_values/
11
+ Documentation also available here: http://objectproxy.rubyforge.org/simple_memoize/
9
12
 
10
13
  == USAGE:
11
14
 
@@ -88,7 +91,7 @@ As a gem:
88
91
 
89
92
  (The MIT License)
90
93
 
91
- Copyright (c) 2008 FIX
94
+ Copyright (c) 2008 Jack Danger Canty, Joel Friedman
92
95
 
93
96
  Permission is hereby granted, free of charge, to any person obtaining
94
97
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -1,12 +1,15 @@
1
- # -*- ruby -*-
2
-
3
- require 'rubygems'
4
- require 'hoe'
5
- require './lib/simple_memoize.rb'
6
-
7
- Hoe.new('simple_memoize', SimpleMemoize::VERSION) do |p|
8
- p.rubyforge_name = 'objectproxy' # if different than lowercase project name
9
- p.developer('Jack Danger Canty', 'rubyforge@6brand.com')
10
- end
11
-
12
1
  # vim: syntax=Ruby
2
+ begin
3
+ require 'jeweler'
4
+ Jeweler::Tasks.new do |gem|
5
+ gem.name = "simple_memoize"
6
+ gem.summary = %Q{Very lightweight way to ensure some methods only run once}
7
+ gem.email = "rubygems@6brand.com"
8
+ gem.homepage = "http://github.com/JackDanger/simple_memoize"
9
+ gem.authors = ["Jack Danger Canty"]
10
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
11
+ end
12
+ Jeweler::GemcutterTasks.new
13
+ rescue LoadError
14
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
15
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'simple_memoize'
@@ -1,36 +1,48 @@
1
1
  module SimpleMemoize
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.2'
3
3
 
4
4
  module Module
5
5
  def memoize(*method_names)
6
6
  method_names.each do |method_name|
7
7
  method_name = method_name.to_s
8
- memoized_method_name = "#{method_name}_with_memo"
9
- regular_method_name = "#{method_name}_without_memo"
8
+ stripped_method_name = method_name.sub(/([!?])$/, '')
10
9
 
11
- unless (instance_methods + private_instance_methods).include?(method_name)
10
+ punctuation = $1
11
+ wordy_punctuation = (punctuation == '!' ? '_bang' : '_huh') if punctuation
12
+ ivar_name = "@#{stripped_method_name}#{wordy_punctuation}"
13
+
14
+ memoized_method_name = "#{stripped_method_name}_with_memo#{punctuation}"
15
+ regular_method_name = "#{stripped_method_name}_without_memo#{punctuation}"
16
+
17
+ unless __include_method_name__( (instance_methods + private_instance_methods), method_name )
12
18
  raise NoMethodError, "The Method '#{method_name}' cannot be memoized because it doesn't exist in #{self}"
13
19
  end
14
20
  return if self.method_defined?(memoized_method_name)
15
21
 
16
- self.class_eval do
17
-
18
- define_method memoized_method_name do |*args|
19
- @simple_memoize ||= {}
20
- @simple_memoize[method_name] ||= {}
21
- @simple_memoize[method_name][args] ||= send(regular_method_name, *args)
22
+ self.class_eval "
23
+ def #{memoized_method_name}(*args)
24
+ if defined?(#{ivar_name})
25
+ #{ivar_name}
26
+ else
27
+ #{ivar_name} = #{regular_method_name}(*args)
28
+ end
22
29
  end
23
30
 
24
- alias_method regular_method_name, method_name
25
- alias_method method_name, memoized_method_name
26
-
27
- protected method_name if protected_instance_methods.include?(regular_method_name)
28
- private method_name if private_instance_methods.include?(regular_method_name)
31
+ alias_method :#{regular_method_name}, :#{method_name}
32
+ alias_method :#{method_name}, :#{memoized_method_name}
29
33
 
30
- end
34
+ protected :#{method_name} if __include_method_name__( protected_instance_methods, '#{regular_method_name}' )
35
+ private :#{method_name} if __include_method_name__( private_instance_methods, '#{regular_method_name}' )
36
+ "
31
37
  end
32
38
  end
39
+
40
+ private
41
+ # Necessary because ruby1.9 now returns methods as symbols not strings
42
+ def __include_method_name__(methods, method_name)
43
+ methods.include?( method_name ) || methods.include?( method_name.to_sym )
44
+ end
33
45
  end
34
46
  end
35
47
 
36
- Module.send :include, SimpleMemoize::Module
48
+ Module.send :include, SimpleMemoize::Module
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'simple_memoize'
@@ -0,0 +1,46 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{simple_memoize}
8
+ s.version = "1.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jack Danger Canty"]
12
+ s.date = %q{2011-04-10}
13
+ s.email = %q{rubygems@6brand.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.txt"
17
+ ]
18
+ s.files = [
19
+ "LICENSE",
20
+ "README.txt",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "init.rb",
24
+ "lib/simple_memoize.rb",
25
+ "rails/init.rb",
26
+ "simple_memoize.gemspec",
27
+ "test/test_simple_memoize.rb"
28
+ ]
29
+ s.homepage = %q{http://github.com/JackDanger/simple_memoize}
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.4.2}
32
+ s.summary = %q{Very lightweight way to ensure some methods only run once}
33
+ s.test_files = [
34
+ "test/test_simple_memoize.rb"
35
+ ]
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 3
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ else
42
+ end
43
+ else
44
+ end
45
+ end
46
+
@@ -8,6 +8,11 @@ module Barks
8
8
  'Grrrrr'
9
9
  end
10
10
  memoize :growl
11
+
12
+ def muzzle
13
+ @growl = false
14
+ end
15
+ memoize :muzzle
11
16
 
12
17
  def protected_growl
13
18
  'Grrrrr'
@@ -36,7 +41,12 @@ class Dog
36
41
  'slurp'
37
42
  end
38
43
  memoize :drink
39
-
44
+
45
+ def hungry?
46
+ true
47
+ end
48
+ memoize :hungry?
49
+
40
50
  class << self
41
51
  def breeds
42
52
  ['doberman', 'dalmatian']
@@ -52,6 +62,18 @@ class SimpleMemoizeTest < Test::Unit::TestCase
52
62
  4.times { dog.growl }
53
63
  end
54
64
 
65
+ def test_method_punctuation_carried_to_end_of_method
66
+ dog = Dog.new
67
+ dog.expects(:hungry_without_memo?).returns(true).once
68
+ 4.times { dog.hungry? }
69
+ end
70
+
71
+ def test_module_mehthod_only_calls_memoized_once_for_non_truthy_result
72
+ dog = Dog.new
73
+ dog.expects(:muzzle_without_memo).returns(false).once
74
+ 4.times { dog.muzzle }
75
+ end
76
+
55
77
  def test_module_method_calls_method_several_times
56
78
  dog = Dog.new
57
79
  dog.expects(:growl).returns('Grrrrr').times(4)
@@ -98,14 +120,16 @@ class SimpleMemoizeTest < Test::Unit::TestCase
98
120
 
99
121
  def test_protected_methods_remain_protected
100
122
  dog = Dog.new
101
- assert dog.protected_methods.include?('protected_growl_without_memo')
102
- assert dog.protected_methods.include?('protected_growl')
123
+ methods = dog.protected_methods
124
+ assert methods.include?('protected_growl_without_memo') || methods.include?( :protected_growl_without_memo )
125
+ assert methods.include?('protected_growl') || methods.include?( :protected_growl )
103
126
  end
104
127
 
105
128
  def test_private_methods_remain_private
106
129
  dog = Dog.new
107
- assert dog.private_methods.include?('private_growl_without_memo')
108
- assert dog.private_methods.include?('private_growl')
130
+ methods = dog.private_methods
131
+ assert methods.include?('private_growl_without_memo') || methods.include?( :private_growl_without_memo )
132
+ assert methods.include?('private_growl') || methods.include?( :private_growl )
109
133
  end
110
134
 
111
135
  def test_cant_memoize_a_missing_method
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_memoize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
5
11
  platform: ruby
6
12
  authors:
7
13
  - Jack Danger Canty
@@ -9,62 +15,62 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2008-03-26 00:00:00 -07:00
18
+ date: 2011-04-10 00:00:00 -07:00
13
19
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: hoe
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 1.5.1
23
- version:
24
- description: Provides in-memory caching of any Ruby method. It's dead simple and won't get in the way of any of your code. For something more robust that offers persistence try http://github.com/JackDanger/cached_values/
25
- email:
26
- - rubyforge@6brand.com
20
+ dependencies: []
21
+
22
+ description:
23
+ email: rubygems@6brand.com
27
24
  executables: []
28
25
 
29
26
  extensions: []
30
27
 
31
28
  extra_rdoc_files:
32
- - History.txt
33
- - Manifest.txt
29
+ - LICENSE
34
30
  - README.txt
35
31
  files:
36
- - History.txt
37
- - Manifest.txt
32
+ - LICENSE
38
33
  - README.txt
39
34
  - Rakefile
35
+ - VERSION
36
+ - init.rb
40
37
  - lib/simple_memoize.rb
38
+ - rails/init.rb
39
+ - simple_memoize.gemspec
41
40
  - test/test_simple_memoize.rb
42
41
  has_rdoc: true
43
- homepage: http://github.com/JackDanger/simple_memoize/
42
+ homepage: http://github.com/JackDanger/simple_memoize
43
+ licenses: []
44
+
44
45
  post_install_message:
45
- rdoc_options:
46
- - --main
47
- - README.txt
46
+ rdoc_options: []
47
+
48
48
  require_paths:
49
49
  - lib
50
50
  required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
51
52
  requirements:
52
53
  - - ">="
53
54
  - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
54
58
  version: "0"
55
- version:
56
59
  required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
57
61
  requirements:
58
62
  - - ">="
59
63
  - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
60
67
  version: "0"
61
- version:
62
68
  requirements: []
63
69
 
64
- rubyforge_project: objectproxy
65
- rubygems_version: 1.0.1
70
+ rubyforge_project:
71
+ rubygems_version: 1.4.2
66
72
  signing_key:
67
- specification_version: 2
68
- summary: Provides in-memory caching of any Ruby method
73
+ specification_version: 3
74
+ summary: Very lightweight way to ensure some methods only run once
69
75
  test_files:
70
76
  - test/test_simple_memoize.rb
data/History.txt DELETED
@@ -1,6 +0,0 @@
1
- === 1.0.0 / 2008-03-25
2
-
3
- * 1 major enhancement
4
-
5
- * Clean little gem
6
-
data/Manifest.txt DELETED
@@ -1,6 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- lib/simple_memoize.rb
6
- test/test_simple_memoize.rb