Use dedicated methods for memoized values (#1211)

RuboCop didn't seem to like having memoization in the
`default_constants` method that didn't match the method name. This
satisfies RuboCop, and saves us an array allocation or two when we run
specs that don't use any of these helpers.
This commit is contained in:
Daniel Colson 2018-10-05 11:19:48 -04:00 committed by GitHub
parent 95bd3c6076
commit c9989e99e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 20 deletions

View File

@ -76,11 +76,6 @@ Metrics/BlockLength:
Metrics/LineLength:
Max: 189
# Offense count: 1
Naming/MemoizedInstanceVariableName:
Exclude:
- 'spec/support/macros/define_constant.rb'
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods.

View File

@ -6,7 +6,7 @@ module DefineConstantMacros
klass = Class.new(base)
namespace.const_set(class_name, klass)
klass.class_eval(&block) if block_given?
@defined_constants << path
defined_constants << path
klass
end
@ -26,7 +26,7 @@ module DefineConstantMacros
begin
connection.execute("DROP TABLE IF EXISTS #{table_name}")
connection.create_table(table_name, &block)
@created_tables << table_name
created_tables << table_name
connection
rescue Exception => exception
connection.execute("DROP TABLE IF EXISTS #{table_name}")
@ -41,27 +41,32 @@ module DefineConstantMacros
[namespace, class_name]
end
def default_constants
@defined_constants ||= []
@created_tables ||= []
end
def clear_generated_constants
@defined_constants.reverse.each do |path|
defined_constants.reverse.each do |path|
namespace, class_name = *constant_path(path)
namespace.send(:remove_const, class_name)
end
@defined_constants.clear
defined_constants.clear
end
def clear_generated_tables
@created_tables.each do |table_name|
created_tables.each do |table_name|
ActiveRecord::Base.
connection.
execute("DROP TABLE IF EXISTS #{table_name}")
end
@created_tables.clear
created_tables.clear
end
private
def defined_constants
@defined_constants ||= []
end
def created_tables
@created_tables ||= []
end
end
@ -75,10 +80,6 @@ RSpec.configure do |config|
)
end
config.before do
default_constants
end
config.after do
clear_generated_constants
clear_generated_tables