From d20067a8e65cbe83b931558893de7d39f32fe396 Mon Sep 17 00:00:00 2001 From: Jordan Hollinger Date: Thu, 18 Jul 2024 16:47:32 -0400 Subject: [PATCH 1/3] Use autoload instead of require/require_relative Signed-off-by: Jordan Hollinger --- Rakefile | 2 +- lib/blueprinter.rb | 36 +++++++++++++++++++-- lib/blueprinter/association.rb | 3 -- lib/blueprinter/base.rb | 21 ------------ lib/blueprinter/blueprint_validator.rb | 2 -- lib/blueprinter/configuration.rb | 15 +++++---- lib/blueprinter/empty_types.rb | 2 -- lib/blueprinter/errors.rb | 7 ++++ lib/blueprinter/errors/invalid_blueprint.rb | 2 -- spec/benchmarks/active_record_big_o_test.rb | 2 +- spec/benchmarks/active_record_ips_test.rb | 2 +- spec/benchmarks/big_o_test.rb | 2 +- spec/benchmarks/ips_test.rb | 2 +- 13 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 lib/blueprinter/errors.rb diff --git a/Rakefile b/Rakefile index 363b14fe..0c7bff41 100644 --- a/Rakefile +++ b/Rakefile @@ -34,7 +34,7 @@ YARD::Rake::YardocTask.new do |t| end Rake::TestTask.new(:benchmarks) do |t| - t.libs << 'spec' + t.libs << 'spec' << 'lib' t.pattern = 'spec/benchmarks/**/*_test.rb' t.verbose = false end diff --git a/lib/blueprinter.rb b/lib/blueprinter.rb index bf0c399c..f90a939d 100644 --- a/lib/blueprinter.rb +++ b/lib/blueprinter.rb @@ -1,7 +1,37 @@ # frozen_string_literal: true -require_relative 'blueprinter/base' -require_relative 'blueprinter/extension' - module Blueprinter + # Core + autoload :Association, 'blueprinter/association' + autoload :Base, 'blueprinter/base' + autoload :Configuration, 'blueprinter/configuration' + autoload :Deprecation, 'blueprinter/deprecation' + autoload :Extension, 'blueprinter/extension' + autoload :Extensions, 'blueprinter/extensions' + autoload :Field, 'blueprinter/field' + autoload :Reflection, 'blueprinter/reflection' + autoload :View, 'blueprinter/view' + autoload :ViewCollection, 'blueprinter/view_collection' + + # Extractors & Transfomers + autoload :AssociationExtractor, 'blueprinter/extractors/association_extractor' + autoload :AutoExtractor, 'blueprinter/extractors/auto_extractor' + autoload :BlockExtractor, 'blueprinter/extractors/block_extractor' + autoload :Extractor, 'blueprinter/extractor' + autoload :HashExtractor, 'blueprinter/extractors/hash_extractor' + autoload :PublicSendExtractor, 'blueprinter/extractors/public_send_extractor' + autoload :Transformer, 'blueprinter/transformer' + + # Helpers & Types + autoload :BaseHelpers, 'blueprinter/helpers/base_helpers' + autoload :DateTimeFormatter, 'blueprinter/formatters/date_time_formatter' + autoload :EmptyTypes, 'blueprinter/empty_types' + autoload :TypeHelpers, 'blueprinter/helpers/type_helpers' + + # Errors & Validation + autoload :BlueprinterError, 'blueprinter/blueprinter_error' + autoload :BlueprintValidator, 'blueprinter/blueprint_validator' + autoload :Errors, 'blueprinter/errors' + + extend Configuration::Configurable end diff --git a/lib/blueprinter/association.rb b/lib/blueprinter/association.rb index 555c1afd..b94b97c6 100644 --- a/lib/blueprinter/association.rb +++ b/lib/blueprinter/association.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -require_relative 'field' -require_relative 'blueprint_validator' - module Blueprinter # @api private class Association < Field diff --git a/lib/blueprinter/base.rb b/lib/blueprinter/base.rb index 0083dde3..04022df8 100644 --- a/lib/blueprinter/base.rb +++ b/lib/blueprinter/base.rb @@ -1,26 +1,5 @@ # frozen_string_literal: true -require_relative 'association' -require_relative 'blueprint_validator' -require_relative 'blueprinter_error' -require_relative 'configuration' -require_relative 'deprecation' -require_relative 'empty_types' -require_relative 'extractor' -require_relative 'extractors/association_extractor' -require_relative 'extractors/auto_extractor' -require_relative 'extractors/block_extractor' -require_relative 'extractors/hash_extractor' -require_relative 'extractors/public_send_extractor' -require_relative 'field' -require_relative 'formatters/date_time_formatter' -require_relative 'helpers/base_helpers' -require_relative 'helpers/type_helpers' -require_relative 'reflection' -require_relative 'transformer' -require_relative 'view_collection' -require_relative 'view' - module Blueprinter class Base include BaseHelpers diff --git a/lib/blueprinter/blueprint_validator.rb b/lib/blueprinter/blueprint_validator.rb index dc7a922d..865e9548 100644 --- a/lib/blueprinter/blueprint_validator.rb +++ b/lib/blueprinter/blueprint_validator.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require_relative 'errors/invalid_blueprint' - module Blueprinter # @api private class BlueprintValidator diff --git a/lib/blueprinter/configuration.rb b/lib/blueprinter/configuration.rb index bdea52b8..b56c94d4 100644 --- a/lib/blueprinter/configuration.rb +++ b/lib/blueprinter/configuration.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'json' -require_relative 'extensions' module Blueprinter class Configuration @@ -48,13 +47,15 @@ def jsonify(blob) def valid_callable?(callable_name) VALID_CALLABLES.include?(callable_name) end - end - def self.configuration - @configuration ||= Configuration.new - end + module Configurable + def configuration + @configuration ||= Configuration.new + end - def self.configure - yield configuration if block_given? + def configure + yield configuration if block_given? + end + end end end diff --git a/lib/blueprinter/empty_types.rb b/lib/blueprinter/empty_types.rb index 03debe98..5d36de74 100644 --- a/lib/blueprinter/empty_types.rb +++ b/lib/blueprinter/empty_types.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require_relative 'helpers/type_helpers' - module Blueprinter EMPTY_COLLECTION = 'empty_collection' EMPTY_HASH = 'empty_hash' diff --git a/lib/blueprinter/errors.rb b/lib/blueprinter/errors.rb new file mode 100644 index 00000000..a3adb656 --- /dev/null +++ b/lib/blueprinter/errors.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Blueprinter + module Errors + autoload :InvalidBlueprint, 'blueprinter/errors/invalid_blueprint' + end +end diff --git a/lib/blueprinter/errors/invalid_blueprint.rb b/lib/blueprinter/errors/invalid_blueprint.rb index 97965653..dbd9c373 100644 --- a/lib/blueprinter/errors/invalid_blueprint.rb +++ b/lib/blueprinter/errors/invalid_blueprint.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'blueprinter/blueprinter_error' - module Blueprinter module Errors class InvalidBlueprint < Blueprinter::BlueprinterError; end diff --git a/spec/benchmarks/active_record_big_o_test.rb b/spec/benchmarks/active_record_big_o_test.rb index ac2d163e..72706cab 100644 --- a/spec/benchmarks/active_record_big_o_test.rb +++ b/spec/benchmarks/active_record_big_o_test.rb @@ -2,7 +2,7 @@ require 'activerecord_helper' require 'benchmark_helper' -require 'blueprinter/base' +require 'blueprinter' class Blueprinter::ActiveRecordBigOTest < Minitest::Benchmark include FactoryBot::Syntax::Methods diff --git a/spec/benchmarks/active_record_ips_test.rb b/spec/benchmarks/active_record_ips_test.rb index 57b27db7..4a7c89d2 100644 --- a/spec/benchmarks/active_record_ips_test.rb +++ b/spec/benchmarks/active_record_ips_test.rb @@ -2,7 +2,7 @@ require 'activerecord_helper' require 'benchmark_helper' -require 'blueprinter/base' +require 'blueprinter' class Blueprinter::ActiveRecordIPSTest < Minitest::Test include FactoryBot::Syntax::Methods diff --git a/spec/benchmarks/big_o_test.rb b/spec/benchmarks/big_o_test.rb index 19e89420..938032df 100644 --- a/spec/benchmarks/big_o_test.rb +++ b/spec/benchmarks/big_o_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'benchmark_helper' -require 'blueprinter/base' +require 'blueprinter' require 'ostruct' class Blueprinter::BigOTest < Minitest::Benchmark diff --git a/spec/benchmarks/ips_test.rb b/spec/benchmarks/ips_test.rb index adcc5f22..8eab2bfc 100644 --- a/spec/benchmarks/ips_test.rb +++ b/spec/benchmarks/ips_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require 'benchmark_helper' -require 'blueprinter/base' +require 'blueprinter' require 'ostruct' class Blueprinter::IPSTest < Minitest::Test From 0787fc5ba0d5643a3b963938263431f3951442aa Mon Sep 17 00:00:00 2001 From: Jordan Hollinger Date: Mon, 22 Jul 2024 18:12:38 -0400 Subject: [PATCH 2/3] Update Rakefile Co-authored-by: Jake Sheehy Signed-off-by: Jordan Hollinger Signed-off-by: Jordan Hollinger --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 0c7bff41..75478643 100644 --- a/Rakefile +++ b/Rakefile @@ -34,7 +34,7 @@ YARD::Rake::YardocTask.new do |t| end Rake::TestTask.new(:benchmarks) do |t| - t.libs << 'spec' << 'lib' + t.libs.append('lib', 'spec') t.pattern = 'spec/benchmarks/**/*_test.rb' t.verbose = false end From 888fca8b9f84af6d4dcdfa596441d5f560087d96 Mon Sep 17 00:00:00 2001 From: Jordan Hollinger Date: Tue, 23 Jul 2024 14:36:29 -0400 Subject: [PATCH 3/3] Hybrid autoload/require Signed-off-by: Jordan Hollinger --- lib/blueprinter.rb | 41 +++++++------------ lib/blueprinter/association.rb | 4 ++ lib/blueprinter/base.rb | 6 +++ lib/blueprinter/configuration.rb | 12 +----- lib/blueprinter/empty_types.rb | 2 + .../extractors/association_extractor.rb | 3 ++ lib/blueprinter/extractors/auto_extractor.rb | 7 ++++ lib/blueprinter/extractors/block_extractor.rb | 2 + lib/blueprinter/extractors/hash_extractor.rb | 2 + .../extractors/public_send_extractor.rb | 2 + lib/blueprinter/helpers/base_helpers.rb | 3 ++ lib/blueprinter/view_collection.rb | 2 + spec/spec_helper.rb | 2 +- spec/units/association_spec.rb | 2 + spec/units/blueprint_validator_spec.rb | 2 + spec/units/configuration_spec.rb | 1 + spec/units/date_time_formatter_spec.rb | 3 ++ spec/units/deprecation_spec.rb | 2 + spec/units/extensions_spec.rb | 1 + spec/units/reflection_spec.rb | 2 + spec/units/view_collection_spec.rb | 2 + 21 files changed, 66 insertions(+), 37 deletions(-) diff --git a/lib/blueprinter.rb b/lib/blueprinter.rb index f90a939d..76cbd50d 100644 --- a/lib/blueprinter.rb +++ b/lib/blueprinter.rb @@ -1,37 +1,26 @@ # frozen_string_literal: true module Blueprinter - # Core - autoload :Association, 'blueprinter/association' autoload :Base, 'blueprinter/base' + autoload :BlueprinterError, 'blueprinter/blueprinter_error' autoload :Configuration, 'blueprinter/configuration' - autoload :Deprecation, 'blueprinter/deprecation' + autoload :Errors, 'blueprinter/errors' autoload :Extension, 'blueprinter/extension' - autoload :Extensions, 'blueprinter/extensions' - autoload :Field, 'blueprinter/field' - autoload :Reflection, 'blueprinter/reflection' - autoload :View, 'blueprinter/view' - autoload :ViewCollection, 'blueprinter/view_collection' - - # Extractors & Transfomers - autoload :AssociationExtractor, 'blueprinter/extractors/association_extractor' - autoload :AutoExtractor, 'blueprinter/extractors/auto_extractor' - autoload :BlockExtractor, 'blueprinter/extractors/block_extractor' - autoload :Extractor, 'blueprinter/extractor' - autoload :HashExtractor, 'blueprinter/extractors/hash_extractor' - autoload :PublicSendExtractor, 'blueprinter/extractors/public_send_extractor' autoload :Transformer, 'blueprinter/transformer' - # Helpers & Types - autoload :BaseHelpers, 'blueprinter/helpers/base_helpers' - autoload :DateTimeFormatter, 'blueprinter/formatters/date_time_formatter' - autoload :EmptyTypes, 'blueprinter/empty_types' - autoload :TypeHelpers, 'blueprinter/helpers/type_helpers' + class << self + # @return [Configuration] + def configuration + @configuration ||= Configuration.new + end - # Errors & Validation - autoload :BlueprinterError, 'blueprinter/blueprinter_error' - autoload :BlueprintValidator, 'blueprinter/blueprint_validator' - autoload :Errors, 'blueprinter/errors' + def configure + yield(configuration) if block_given? + end - extend Configuration::Configurable + # Resets global configuration. + def reset_configuration! + @configuration = nil + end + end end diff --git a/lib/blueprinter/association.rb b/lib/blueprinter/association.rb index b94b97c6..a8d563e6 100644 --- a/lib/blueprinter/association.rb +++ b/lib/blueprinter/association.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true +require 'blueprinter/field' +require 'blueprinter/blueprint_validator' +require 'blueprinter/extractors/association_extractor' + module Blueprinter # @api private class Association < Field diff --git a/lib/blueprinter/base.rb b/lib/blueprinter/base.rb index 04022df8..b37961c0 100644 --- a/lib/blueprinter/base.rb +++ b/lib/blueprinter/base.rb @@ -1,5 +1,11 @@ # frozen_string_literal: true +require 'blueprinter/association' +require 'blueprinter/extractors/association_extractor' +require 'blueprinter/field' +require 'blueprinter/helpers/base_helpers' +require 'blueprinter/reflection' + module Blueprinter class Base include BaseHelpers diff --git a/lib/blueprinter/configuration.rb b/lib/blueprinter/configuration.rb index b56c94d4..ce19178e 100644 --- a/lib/blueprinter/configuration.rb +++ b/lib/blueprinter/configuration.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require 'json' +require 'blueprinter/extensions' +require 'blueprinter/extractors/auto_extractor' module Blueprinter class Configuration @@ -47,15 +49,5 @@ def jsonify(blob) def valid_callable?(callable_name) VALID_CALLABLES.include?(callable_name) end - - module Configurable - def configuration - @configuration ||= Configuration.new - end - - def configure - yield configuration if block_given? - end - end end end diff --git a/lib/blueprinter/empty_types.rb b/lib/blueprinter/empty_types.rb index 5d36de74..0a881a7c 100644 --- a/lib/blueprinter/empty_types.rb +++ b/lib/blueprinter/empty_types.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/helpers/type_helpers' + module Blueprinter EMPTY_COLLECTION = 'empty_collection' EMPTY_HASH = 'empty_hash' diff --git a/lib/blueprinter/extractors/association_extractor.rb b/lib/blueprinter/extractors/association_extractor.rb index b9ddbe8c..9b8bcf36 100644 --- a/lib/blueprinter/extractors/association_extractor.rb +++ b/lib/blueprinter/extractors/association_extractor.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require 'blueprinter/extractor' +require 'blueprinter/empty_types' + module Blueprinter # @api private class AssociationExtractor < Extractor diff --git a/lib/blueprinter/extractors/auto_extractor.rb b/lib/blueprinter/extractors/auto_extractor.rb index 226ba214..9b302424 100644 --- a/lib/blueprinter/extractors/auto_extractor.rb +++ b/lib/blueprinter/extractors/auto_extractor.rb @@ -1,5 +1,12 @@ # frozen_string_literal: true +require 'blueprinter/extractor' +require 'blueprinter/empty_types' +require 'blueprinter/extractors/block_extractor' +require 'blueprinter/extractors/hash_extractor' +require 'blueprinter/extractors/public_send_extractor' +require 'blueprinter/formatters/date_time_formatter' + module Blueprinter # @api private class AutoExtractor < Extractor diff --git a/lib/blueprinter/extractors/block_extractor.rb b/lib/blueprinter/extractors/block_extractor.rb index 925d8e8e..44210262 100644 --- a/lib/blueprinter/extractors/block_extractor.rb +++ b/lib/blueprinter/extractors/block_extractor.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/extractor' + module Blueprinter # @api private class BlockExtractor < Extractor diff --git a/lib/blueprinter/extractors/hash_extractor.rb b/lib/blueprinter/extractors/hash_extractor.rb index 714739df..fa38efb4 100644 --- a/lib/blueprinter/extractors/hash_extractor.rb +++ b/lib/blueprinter/extractors/hash_extractor.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/extractor' + module Blueprinter # @api private class HashExtractor < Extractor diff --git a/lib/blueprinter/extractors/public_send_extractor.rb b/lib/blueprinter/extractors/public_send_extractor.rb index d3a0c320..31aa2430 100644 --- a/lib/blueprinter/extractors/public_send_extractor.rb +++ b/lib/blueprinter/extractors/public_send_extractor.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/extractor' + module Blueprinter # @api private class PublicSendExtractor < Extractor diff --git a/lib/blueprinter/helpers/base_helpers.rb b/lib/blueprinter/helpers/base_helpers.rb index 79079254..7e094ff8 100644 --- a/lib/blueprinter/helpers/base_helpers.rb +++ b/lib/blueprinter/helpers/base_helpers.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require 'blueprinter/helpers/type_helpers' +require 'blueprinter/view_collection' + module Blueprinter module BaseHelpers def self.included(base) diff --git a/lib/blueprinter/view_collection.rb b/lib/blueprinter/view_collection.rb index e624a118..ccd758a5 100644 --- a/lib/blueprinter/view_collection.rb +++ b/lib/blueprinter/view_collection.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/view' + module Blueprinter # @api private class ViewCollection diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a6e6707d..09a370fd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,7 +7,7 @@ module SpecHelpers def reset_blueprinter_config! - Blueprinter.instance_variable_set(:@configuration, nil) + Blueprinter.reset_configuration! end end diff --git a/spec/units/association_spec.rb b/spec/units/association_spec.rb index 7478ed3a..01e8c148 100644 --- a/spec/units/association_spec.rb +++ b/spec/units/association_spec.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/association' + describe Blueprinter::Association do describe '#initialize' do let(:blueprint) { Class.new(Blueprinter::Base) } diff --git a/spec/units/blueprint_validator_spec.rb b/spec/units/blueprint_validator_spec.rb index 159b623b..c058ebfa 100644 --- a/spec/units/blueprint_validator_spec.rb +++ b/spec/units/blueprint_validator_spec.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/blueprint_validator' + describe Blueprinter::BlueprintValidator do describe 'validate!' do context 'when provided object subclasses Blueprinter::Base' do diff --git a/spec/units/configuration_spec.rb b/spec/units/configuration_spec.rb index c162dc5d..80411ef4 100644 --- a/spec/units/configuration_spec.rb +++ b/spec/units/configuration_spec.rb @@ -78,6 +78,7 @@ class UpcaseTransform < Blueprinter::Transformer; end end it 'should default the `extractor_default` option' do + Blueprinter.reset_configuration! expect(Blueprinter.configuration.extractor_default).to eq(Blueprinter::AutoExtractor) end diff --git a/spec/units/date_time_formatter_spec.rb b/spec/units/date_time_formatter_spec.rb index 8b49107d..5184f208 100644 --- a/spec/units/date_time_formatter_spec.rb +++ b/spec/units/date_time_formatter_spec.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require 'date' +require 'blueprinter/formatters/date_time_formatter' + describe '::DateTimeFormatter' do let(:formatter) { Blueprinter::DateTimeFormatter.new } let(:valid_date) { Date.new(1994, 3, 4) } diff --git a/spec/units/deprecation_spec.rb b/spec/units/deprecation_spec.rb index a1e6e3b5..f73ff3bb 100644 --- a/spec/units/deprecation_spec.rb +++ b/spec/units/deprecation_spec.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/deprecation' + describe 'Blueprinter::Deprecation' do describe '#report' do TEST_MESSAGE = "Test Message" diff --git a/spec/units/extensions_spec.rb b/spec/units/extensions_spec.rb index eb985197..09a341b3 100644 --- a/spec/units/extensions_spec.rb +++ b/spec/units/extensions_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'ostruct' +require 'blueprinter/extensions' describe Blueprinter::Extensions do let(:all_extensions) { diff --git a/spec/units/reflection_spec.rb b/spec/units/reflection_spec.rb index 89955359..2f86490d 100644 --- a/spec/units/reflection_spec.rb +++ b/spec/units/reflection_spec.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/reflection' + describe Blueprinter::Reflection do let(:category_blueprint) { Class.new(Blueprinter::Base) do diff --git a/spec/units/view_collection_spec.rb b/spec/units/view_collection_spec.rb index cf86d74b..cde369df 100644 --- a/spec/units/view_collection_spec.rb +++ b/spec/units/view_collection_spec.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'blueprinter/view_collection' + describe 'ViewCollection' do subject(:view_collection) { Blueprinter::ViewCollection.new }