dememoize 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +126 -0
- data/lib/dememoize/rspec_dememoize.rb +35 -0
- data/lib/dememoize/version.rb +5 -0
- data/lib/dememoize.rb +25 -0
- metadata +167 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8c7f5057cf01e4f06611fad7b1545f4594adcd564b8b094563c9eeee6e278628
|
4
|
+
data.tar.gz: db3ce11f1c3fc50f9bd35ad37992114730acdcd130dd102110fb7e7ad99db0c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ab5799fb52990b8d84280542a29cb73d04a6d77bbc71e17f7a706262ac0620868b74f6d605c67c03e78947fb08bb6896b2c5c3b43790c28e5c77e27347f32dc7
|
7
|
+
data.tar.gz: b07ab5360ea302854486dc6bf27b1b13de458394733eb2448b086b3be088853de00cd19cae2a629ff948c0395e63b3b137cf92748fb0bf764956f000efeb6277
|
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
dememoize
|
2
|
+
=========
|
3
|
+
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/dememoize.svg)](https://badge.fury.io/rb/dememoize)
|
5
|
+
|
6
|
+
## Description
|
7
|
+
|
8
|
+
`dememoize` is a gem that provides a method to remove a memoized value (an instance variable) from a
|
9
|
+
class or an object instance.
|
10
|
+
|
11
|
+
The primary use case is usage in spec tests. If you have a code that memoizes a value, but you
|
12
|
+
would like to check that the value is calculated differently under different circumstances, you can
|
13
|
+
use `dememoize` to remove the memoized value, leaving you free to add another spec that can check
|
14
|
+
the value again.
|
15
|
+
|
16
|
+
eg:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class Product
|
20
|
+
class << self
|
21
|
+
def categories
|
22
|
+
@categories ||= expensive_calculation
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "test different memoized values based on environment" do
|
28
|
+
# Here is the shared_context that dememoize provides that aids with the specs
|
29
|
+
include_context "with dememoized class variable", :@categories
|
30
|
+
|
31
|
+
it "should return one value for prelive" do
|
32
|
+
expect(Rails).to receive(:env).and_return("prelive")
|
33
|
+
expect(Product.categories).to eq(1)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return another value for production" do
|
37
|
+
expect(Rails).to receive(:env).and_return("production")
|
38
|
+
expect(Product.categories).to eq(2)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
Add this line to your application's Gemfile:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
gem 'dememoize'
|
49
|
+
```
|
50
|
+
|
51
|
+
## Simple usage
|
52
|
+
|
53
|
+
You can dememoize an instance:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class Car
|
57
|
+
def bolts
|
58
|
+
@bolts ||= expensive_calculation
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
car = Car.new
|
63
|
+
car.bolts # => 28376
|
64
|
+
car.dememoize(:@bolts)
|
65
|
+
car.instance_variable_defined?(:@bolts) # => false
|
66
|
+
|
67
|
+
# You can also specify the instance variable name as a string
|
68
|
+
car.bolts # => 28376
|
69
|
+
car.dememoize("@bolts")
|
70
|
+
car.instance_variable_defined?(:@bolts) # => false
|
71
|
+
```
|
72
|
+
|
73
|
+
You can dememoize a class variable:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class Product
|
77
|
+
class << self
|
78
|
+
def categories
|
79
|
+
@categories ||= expensive_calculation
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
Product.categories # => 4324
|
85
|
+
Product.dememoize(:@categories)
|
86
|
+
Product.instance_variable_defined?(:@categories) # => false
|
87
|
+
```
|
88
|
+
|
89
|
+
## Usage with RSpec
|
90
|
+
|
91
|
+
`dememoize` comes with rspec context helpers to dememoize a class variable in a spec:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
# In this example, dememoize assumes that the class under test is the object that should have the
|
95
|
+
# class instance variable removed.
|
96
|
+
describe "without specifying a class it uses described_class" do
|
97
|
+
include_context "with dememoized class variable", :@categories
|
98
|
+
|
99
|
+
it "should return one value for prelive" do
|
100
|
+
expect(Rails).to receive(:env).and_return("prelive")
|
101
|
+
expect(Product.categories).to eq(1234)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should return another value for production" do
|
105
|
+
expect(Rails).to receive(:env).and_return("production")
|
106
|
+
expect(Product.categories).to eq(5678)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# If you want to dememoize a class variable on a different class, you can pass the class as the
|
111
|
+
# first argument to dememoized.
|
112
|
+
|
113
|
+
describe "specify a different class (Bar) to dememoize" do
|
114
|
+
include_context "with dememoized class variable", :@categories, Bar # <-- Bar is the class
|
115
|
+
# ...
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
## Upgrading
|
120
|
+
|
121
|
+
`dememoize` uses [semantic versioning](https://semver.org/), so major version changes will usually
|
122
|
+
require additional actions to be taken upgrading from one major version to another.
|
123
|
+
|
124
|
+
## Changelog
|
125
|
+
|
126
|
+
A full changelog can be found here: [CHANGELOG.md](https://github.com/hlascelles/dememoize/blob/master/CHANGELOG.md)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
return unless Gem.loaded_specs["rspec"]
|
4
|
+
|
5
|
+
require "rspec/core"
|
6
|
+
|
7
|
+
module RSpec
|
8
|
+
module Dememoize
|
9
|
+
class << self
|
10
|
+
def included(base)
|
11
|
+
base.extend Dememoize
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec.shared_context "with dememoized variable" do |name, object:|
|
18
|
+
around(:each) do |example|
|
19
|
+
Dememoize.remove_instance_variable_if_defined(object, name)
|
20
|
+
example.run
|
21
|
+
Dememoize.remove_instance_variable_if_defined(object, name)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
RSpec.shared_context "with dememoized class variable" do |name, clazz: described_class|
|
26
|
+
around(:each) do |example|
|
27
|
+
Dememoize.remove_instance_variable_if_defined(clazz, name)
|
28
|
+
example.run
|
29
|
+
Dememoize.remove_instance_variable_if_defined(clazz, name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
RSpec.configure do |config|
|
34
|
+
config.include RSpec::Dememoize
|
35
|
+
end
|
data/lib/dememoize.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dememoize/version"
|
4
|
+
require "dememoize/rspec_dememoize"
|
5
|
+
|
6
|
+
module Dememoize
|
7
|
+
def dememoize(obj, *instance_variables)
|
8
|
+
use_names = if instance_variables.empty?
|
9
|
+
obj.instance_variables
|
10
|
+
else
|
11
|
+
instance_variables
|
12
|
+
end
|
13
|
+
use_names.each do |name|
|
14
|
+
remove_instance_variable_if_defined(obj, name)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def remove_instance_variable_if_defined(obj, name)
|
19
|
+
return unless obj.instance_variable_defined?(name)
|
20
|
+
|
21
|
+
obj.remove_instance_variable(name)
|
22
|
+
end
|
23
|
+
|
24
|
+
module_function :dememoize, :remove_instance_variable_if_defined
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dememoize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Harry Lascelles
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: fasterer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry-byebug
|
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: rake
|
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: reek
|
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: rspec
|
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: rubocop
|
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: rubocop-rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: |
|
126
|
+
A gem to clean up memoization. This is useful for testing, where you want to
|
127
|
+
ensure that a method is called again after a change to the object.
|
128
|
+
email:
|
129
|
+
- harry@harrylascelles.com
|
130
|
+
executables: []
|
131
|
+
extensions: []
|
132
|
+
extra_rdoc_files: []
|
133
|
+
files:
|
134
|
+
- README.md
|
135
|
+
- lib/dememoize.rb
|
136
|
+
- lib/dememoize/rspec_dememoize.rb
|
137
|
+
- lib/dememoize/version.rb
|
138
|
+
homepage: https://github.com/hlascelles/dememoize
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata:
|
142
|
+
homepage_uri: https://github.com/hlascelles/dememoize
|
143
|
+
documentation_uri: https://github.com/hlascelles/dememoize
|
144
|
+
changelog_uri: https://github.com/hlascelles/dememoize/blob/master/CHANGELOG.md
|
145
|
+
source_code_uri: https://github.com/hlascelles/dememoize/
|
146
|
+
bug_tracker_uri: https://github.com/hlascelles/dememoize/issues
|
147
|
+
rubygems_mfa_required: 'true'
|
148
|
+
post_install_message:
|
149
|
+
rdoc_options: []
|
150
|
+
require_paths:
|
151
|
+
- lib
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '2.5'
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
requirements: []
|
163
|
+
rubygems_version: 3.1.6
|
164
|
+
signing_key:
|
165
|
+
specification_version: 4
|
166
|
+
summary: A gem to clean up memoization
|
167
|
+
test_files: []
|