Skip to content

Commit 339ed0d

Browse files
committed
Update according to Rubocop conventions
1 parent a7b7d3c commit 339ed0d

6 files changed

Lines changed: 40 additions & 53 deletions

File tree

.rubocop.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
require: 'rubocop-rspec'
22

33
AllCops:
4-
TargetRubyVersion: 2.3
4+
NewCops: enable
5+
TargetRubyVersion: 2.5
56

7+
Layout/LineLength:
8+
Max: 120
9+
IgnoredPatterns: ['#.*']
610
Layout/SpaceInsideHashLiteralBraces:
711
EnforcedStyle: no_space
812

9-
Metrics/LineLength:
10-
Max: 120
11-
IgnoredPatterns: ['#.*']
1213
Metrics/MethodLength:
1314
Max: 25
1415

Guardfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#
1818
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
1919

20-
# Note: The cmd option is now required due to the increasing number of ways
20+
# NOTE: The cmd option is now required due to the increasing number of ways
2121
# rspec may be run, below are examples of the most common uses.
2222
# * bundler: 'bundle exec rspec'
2323
# * bundler binstubs: 'bin/rspec'
@@ -43,7 +43,7 @@ guard :rspec, cmd: 'bundle exec rspec' do
4343
dsl.watch_spec_files_for(ruby.lib_files)
4444
end
4545

46-
guard :rubocop, cli: '-RD' do
46+
guard :rubocop, cli: '-D' do
4747
watch(/.+\.rb$/)
4848
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
4949
end

cacheable.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require 'cacheable/version'
66
Gem::Specification.new do |s|
77
s.name = 'cacheable'
88
s.version = Cacheable::VERSION
9-
s.date = '2018-07-31'
109
s.summary = 'Add caching to any Ruby method in a aspect orientated programming approach.'
1110
s.description = 'Add caching simply without modifying your existing code. '\
1211
'Includes configurable options for simple cache invalidation. '\
@@ -16,5 +15,6 @@ Gem::Specification.new do |s|
1615
s.files = Dir['lib/**/*', 'README.md', 'cache-adapters.md']
1716
s.homepage = 'https://github.com/splitwise/cacheable'
1817
s.licenses = 'MIT'
19-
s.required_ruby_version = '>= 2.3.0'
18+
s.required_ruby_version = '>= 2.5.0'
19+
s.metadata = {'rubygems_mfa_required' => 'true'}
2020
end

lib/cacheable/method_generator.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'English'
4-
53
module Cacheable
64
module MethodGenerator
75
def cacheable(*original_method_names, **opts)
@@ -66,7 +64,7 @@ def default_key_format
6664

6765
def create_method_names(original_method_name)
6866
method_name_without_punctuation = original_method_name.to_s.sub(/([?!=])$/, '')
69-
punctuation = $LAST_PAREN_MATCH
67+
punctuation = Regexp.last_match(-1)
7068

7169
{
7270
with_cache_method_name: "#{method_name_without_punctuation}_with_cache#{punctuation}",

spec/.rubocop.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
inherit_from: ../.rubocop.yml
22

3+
Layout/LineLength:
4+
Max: 170
5+
36
# This cop does not like rspec dsl syntax https://github.com/bbatsov/rubocop/pull/4237#issuecomment-291408032
47
Lint/AmbiguousBlockAssociation:
58
Enabled: false
69

710
Metrics/BlockLength:
811
Enabled: false
912

10-
Metrics/LineLength:
11-
Max: 170
12-
1313
RSpec/AnyInstance:
1414
Enabled: false
1515

@@ -32,3 +32,7 @@ RSpec/NestedGroups:
3232
RSpec/SubjectStub:
3333
Exclude:
3434
- 'cacheable/cacheable_spec.rb'
35+
36+
Style/RedundantFetchBlock:
37+
Exclude:
38+
- 'cacheable/cache_adapters/memory_adapter_spec.rb' # Rubocop cannot tell Cache#fetch from Hash#fetch

spec/cacheable/cacheable_spec.rb

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@
7070
end
7171

7272
it 'allows access to `super` via a module interceptor' do
73-
class BetterString < String
73+
better_string = Class.new(String) do
7474
include Cacheable
7575

76-
def to_s
76+
cacheable def to_s
7777
"Better #{super}"
7878
end
79-
80-
cacheable :to_s
8179
end
80+
stub_const('BetterString', better_string)
8281

8382
better_string = BetterString.new('my string')
8483
expect(better_string).to receive(:to_s_with_cache).twice.and_call_original
@@ -87,18 +86,21 @@ def to_s
8786
end
8887

8988
it 'uses the class name to define an interceptor module' do
90-
class RealClassName
91-
include Cacheable
92-
end
93-
class_name = RealClassName
89+
# This is done specifically this way to be compatible w/ RSpec best practices
90+
# Once Cacheable is included in a class, it uses the name of the class to define the
91+
# interceptor module. However, it is considered bad practice to create constants in RSpec
92+
# so they're typically made with `stub_const`. We need to include Cacheable after the
93+
# anonymous class has been created and assigned to the stubbed constant for this order to work.
94+
stub_const('RealClassName', Class.new)
95+
class_name = RealClassName.include(described_class)
96+
9497
expect(class_name.ancestors.map(&:to_s)).to include("Cacheable::#{class_name}Cacher")
9598
end
9699

97100
it 'uses the class address to define an interceptor module for anonymous classes' do
98-
custom_class = Class.new do
99-
include Cacheable
100-
end
101+
custom_class = Class.new { include Cacheable }
101102
class_name = custom_class.to_s.tr('#:<>', '')
103+
102104
expect(custom_class.ancestors.map(&:to_s)).to include("Cacheable::#{class_name}Cacher")
103105
end
104106

@@ -107,9 +109,7 @@ class RealClassName
107109
it "appends #{special_character} to the end of generated method names" do
108110
method_name = "cacheable_method#{special_character}"
109111
cacheable_class.class_eval do
110-
define_method(method_name) {}
111-
112-
cacheable method_name
112+
cacheable define_method(method_name, -> {})
113113
end
114114

115115
expect(cacheable_object).to respond_to("cacheable_method_without_cache#{special_character}")
@@ -120,29 +120,10 @@ class RealClassName
120120

121121
context 'when classes are nested' do
122122
it 'correctly names interceptor modules' do
123-
expect do
124-
class Outer
125-
class Inner
126-
include Cacheable
123+
stub_const('Outer::Inner', Class.new)
124+
Outer::Inner.include(described_class)
127125

128-
cacheable :outer_method
129-
130-
def outer_method
131-
inner_method
132-
'outer_method'
133-
end
134-
135-
def inner_method
136-
'inner_method'
137-
end
138-
end
139-
end
140-
end.not_to raise_error
141-
142-
cacheable_instance = Outer::Inner.new
143-
expect(cacheable_instance).to receive(:inner_method).once.and_call_original
144-
145-
2.times { cacheable_instance.outer_method }
126+
expect(Outer::Inner.ancestors.map(&:to_s)).to include('Cacheable::OuterInnerCacher')
146127
end
147128
end
148129
end
@@ -163,9 +144,10 @@ def inner_method
163144
describe 'key generation' do
164145
context 'without any customization' do
165146
it 'uses the class name and method name for the cache key' do
166-
TotallyRealClassName = cacheable_class
147+
stub_const('TotallyRealClassName', cacheable_class)
167148
cacheable_object = TotallyRealClassName.new
168149
key = cacheable_object.cacheable_method_key_format
150+
169151
expect(key).to eq([cacheable_object.class.name, cacheable_method])
170152
expect { cacheable_object.cacheable_method }
171153
.to change { described_class.cache_adapter.exist?(key) }.from(false).to(true)
@@ -458,8 +440,10 @@ def cache_control_method
458440
end
459441

460442
it 'uses the class name and method name for the cache key' do
461-
AnotherTotallyRealClassName = cacheable_class
462-
expect(AnotherTotallyRealClassName.cacheable_method_key_format).to eq([cacheable_class.name, cacheable_method])
443+
stub_const('AnotherTotallyRealClassName', cacheable_class)
444+
key = AnotherTotallyRealClassName.cacheable_method_key_format
445+
446+
expect(key).to eq([cacheable_class.name, cacheable_method])
463447
end
464448
end
465449

0 commit comments

Comments
 (0)