memoizable 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -73,6 +73,38 @@ not safe in every domain. If it was possible for one of the attributes to
73
73
  change between method calls, memoizing that value could produce the wrong
74
74
  result. Please keep this in mind when considering which methods to memoize.
75
75
 
76
+ Supported Ruby Versions
77
+ -----------------------
78
+
79
+ This library aims to support and is [tested against][travis] the following Ruby
80
+ implementations:
81
+
82
+ * Ruby 1.8.7
83
+ * Ruby 1.9.2
84
+ * Ruby 1.9.3
85
+ * Ruby 2.0.0
86
+ * [JRuby][]
87
+ * [Rubinius][]
88
+ * [Ruby Enterprise Edition][ree]
89
+
90
+ [jruby]: http://jruby.org/
91
+ [rubinius]: http://rubini.us/
92
+ [ree]: http://www.rubyenterpriseedition.com/
93
+
94
+ If something doesn't work on one of these versions, it's a bug.
95
+
96
+ This library may inadvertently work (or seem to work) on other Ruby versions or
97
+ implementations, however support will only be provided for the implementations
98
+ listed above.
99
+
100
+ If you would like this library to support another Ruby version or
101
+ implementation, you may volunteer to be a maintainer. Being a maintainer
102
+ entails making sure all tests run and pass on that implementation. When
103
+ something breaks on your implementation, you will be responsible for providing
104
+ patches in a timely fashion. If critical issues for a particular implementation
105
+ exist at the time of a major release, support for that Ruby version may be
106
+ dropped.
107
+
76
108
  ## Copyright
77
109
 
78
110
  Copyright © 2013 Dan Kubb, Erik Michaels-Ober. See LICENSE for details.
@@ -7,6 +7,8 @@ module Memoizable
7
7
 
8
8
  # Initialize the memory storage for memoized methods
9
9
  #
10
+ # @param [ThreadSafe::Cache] memory
11
+ #
10
12
  # @return [undefined]
11
13
  #
12
14
  # @api private
@@ -75,5 +77,28 @@ module Memoizable
75
77
  @memory.key?(name)
76
78
  end
77
79
 
80
+ # A hook that allows Marshal to dump the object
81
+ #
82
+ # @return [Hash]
83
+ # A hash used to populate the internal memory
84
+ #
85
+ # @api public
86
+ def marshal_dump
87
+ @memory.marshal_dump
88
+ end
89
+
90
+ # A hook that allows Marshal to load the object
91
+ #
92
+ # @param [Hash] hash
93
+ # A hash used to populate the internal memory
94
+ #
95
+ # @return [undefined]
96
+ #
97
+ # @api public
98
+ def marshal_load(hash)
99
+ initialize
100
+ @memory.marshal_load(hash)
101
+ end
102
+
78
103
  end # Memory
79
104
  end # Memoizable
@@ -99,7 +99,8 @@ module Memoizable
99
99
  #
100
100
  # @api private
101
101
  def remove_original_method
102
- descendant_exec(@method_name) { |name| undef_method(name) }
102
+ name = @method_name
103
+ @descendant.module_eval { undef_method(name) }
103
104
  end
104
105
 
105
106
  # Create a new memoized method
@@ -108,7 +109,8 @@ module Memoizable
108
109
  #
109
110
  # @api private
110
111
  def create_memoized_method
111
- descendant_exec(@method_name, @original_method, @freezer) do |name, method, freezer|
112
+ name, method, freezer = @method_name, @original_method, @freezer
113
+ @descendant.module_eval do
112
114
  define_method(name) do |&block|
113
115
  fail BlockNotAllowedError.new(self.class, name) if block
114
116
  memoized_method_cache.fetch(name) do
@@ -139,16 +141,5 @@ module Memoizable
139
141
  end
140
142
  end
141
143
 
142
- # Helper method to execute code within the descendant scope
143
- #
144
- # @param [Array] args
145
- #
146
- # @return [undefined]
147
- #
148
- # @api private
149
- def descendant_exec(*args, &block)
150
- @descendant.instance_exec(*args, &block)
151
- end
152
-
153
144
  end # MethodBuilder
154
145
  end # Memoizable
@@ -3,6 +3,6 @@
3
3
  module Memoizable
4
4
 
5
5
  # Gem version
6
- VERSION = '0.3.1'.freeze
6
+ VERSION = '0.4.0'.freeze
7
7
 
8
8
  end # Memoizable
@@ -5,8 +5,8 @@ require File.expand_path('../lib/memoizable/version', __FILE__)
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = 'memoizable'
7
7
  gem.version = Memoizable::VERSION.dup
8
- gem.authors = ['Dan Kubb']
9
- gem.email = 'dan.kubb@gmail.com'
8
+ gem.authors = ['Dan Kubb', 'Erik Michaels-Ober']
9
+ gem.email = ['dan.kubb@gmail.com', 'sferik@gmail.com']
10
10
  gem.description = 'Memoize method return values'
11
11
  gem.summary = gem.description
12
12
  gem.homepage = 'https://github.com/dkubb/memoizable'
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ class Serializable
6
+ include Memoizable
7
+
8
+ def random_number
9
+ rand(10000)
10
+ end
11
+ memoize :random_number
12
+ end
13
+
14
+ describe 'A serializable object' do
15
+ let(:serializable) do
16
+ Serializable.new
17
+ end
18
+
19
+ before do
20
+ serializable.random_number # Call the memoized method to trigger lazy memoization
21
+ end
22
+
23
+ it 'is serializable with Marshal' do
24
+ expect { Marshal.dump(serializable) }.not_to raise_error
25
+ end
26
+
27
+ it 'is deserializable with Marshal' do
28
+ serialized = Marshal.dump(serializable)
29
+ deserialized = Marshal.load(serialized)
30
+
31
+ expect(deserialized).to be_an_instance_of(Serializable)
32
+ expect(deserialized.random_number).to eql(serializable.random_number)
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Memoizable::Memory do
4
+ let(:memory) { Memoizable::Memory.new }
5
+
6
+ context "serialization" do
7
+ let(:deserialized) { Marshal.load(Marshal.dump(memory)) }
8
+
9
+ it 'is serializable with Marshal' do
10
+ expect { Marshal.dump(memory) }.not_to raise_error
11
+ end
12
+
13
+ it 'is deserializable with Marshal' do
14
+ expect(deserialized).to be_an_instance_of(Memoizable::Memory)
15
+ end
16
+
17
+ it 'mantains the same class of cache when deserialized' do
18
+ original_cache = memory.instance_variable_get(:@memory)
19
+ deserialized_cache = deserialized.instance_variable_get(:@memory)
20
+
21
+ expect(deserialized_cache.class).to eql(original_cache.class)
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memoizable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dan Kubb
9
+ - Erik Michaels-Ober
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-12-18 00:00:00.000000000 Z
13
+ date: 2013-12-24 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: thread_safe
@@ -50,7 +51,9 @@ dependencies:
50
51
  - !ruby/object:Gem::Version
51
52
  version: 1.3.5
52
53
  description: Memoize method return values
53
- email: dan.kubb@gmail.com
54
+ email:
55
+ - dan.kubb@gmail.com
56
+ - sferik@gmail.com
54
57
  executables: []
55
58
  extensions: []
56
59
  extra_rdoc_files:
@@ -69,6 +72,7 @@ files:
69
72
  - lib/memoizable/module_methods.rb
70
73
  - lib/memoizable/version.rb
71
74
  - lib/memoizable.rb
75
+ - spec/integration/serializable_spec.rb
72
76
  - spec/shared/call_super_shared_spec.rb
73
77
  - spec/shared/command_method_behavior.rb
74
78
  - spec/spec_helper.rb
@@ -76,6 +80,7 @@ files:
76
80
  - spec/unit/memoizable/fixtures/classes.rb
77
81
  - spec/unit/memoizable/instance_methods/freeze_spec.rb
78
82
  - spec/unit/memoizable/instance_methods/memoize_spec.rb
83
+ - spec/unit/memoizable/memory_spec.rb
79
84
  - spec/unit/memoizable/method_builder/call_spec.rb
80
85
  - spec/unit/memoizable/method_builder/class_methods/new_spec.rb
81
86
  - spec/unit/memoizable/method_builder/original_method_spec.rb
@@ -98,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
103
  version: '0'
99
104
  segments:
100
105
  - 0
101
- hash: -3254840289484637315
106
+ hash: 1369072694341033356
102
107
  required_rubygems_version: !ruby/object:Gem::Requirement
103
108
  none: false
104
109
  requirements:
@@ -107,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
112
  version: '0'
108
113
  segments:
109
114
  - 0
110
- hash: -3254840289484637315
115
+ hash: 1369072694341033356
111
116
  requirements: []
112
117
  rubyforge_project:
113
118
  rubygems_version: 1.8.23
@@ -119,6 +124,7 @@ test_files:
119
124
  - spec/unit/memoizable/fixtures/classes.rb
120
125
  - spec/unit/memoizable/instance_methods/freeze_spec.rb
121
126
  - spec/unit/memoizable/instance_methods/memoize_spec.rb
127
+ - spec/unit/memoizable/memory_spec.rb
122
128
  - spec/unit/memoizable/method_builder/call_spec.rb
123
129
  - spec/unit/memoizable/method_builder/class_methods/new_spec.rb
124
130
  - spec/unit/memoizable/method_builder/original_method_spec.rb
@@ -126,3 +132,4 @@ test_files:
126
132
  - spec/unit/memoizable/module_methods/memoize_spec.rb
127
133
  - spec/unit/memoizable/module_methods/memoized_predicate_spec.rb
128
134
  - spec/unit/memoizable/module_methods/unmemoized_instance_method_spec.rb
135
+ - spec/integration/serializable_spec.rb