diff --git a/Rakefile b/Rakefile index 363b14fe..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' + t.libs.append('lib', 'spec') t.pattern = 'spec/benchmarks/**/*_test.rb' t.verbose = false end diff --git a/lib/blueprinter.rb b/lib/blueprinter.rb index bf0c399c..76cbd50d 100644 --- a/lib/blueprinter.rb +++ b/lib/blueprinter.rb @@ -1,7 +1,26 @@ # frozen_string_literal: true -require_relative 'blueprinter/base' -require_relative 'blueprinter/extension' - module Blueprinter + autoload :Base, 'blueprinter/base' + autoload :BlueprinterError, 'blueprinter/blueprinter_error' + autoload :Configuration, 'blueprinter/configuration' + autoload :Errors, 'blueprinter/errors' + autoload :Extension, 'blueprinter/extension' + autoload :Transformer, 'blueprinter/transformer' + + class << self + # @return [Configuration] + def configuration + @configuration ||= Configuration.new + end + + def configure + yield(configuration) if block_given? + end + + # Resets global configuration. + def reset_configuration! + @configuration = nil + end + end end diff --git a/lib/blueprinter/association.rb b/lib/blueprinter/association.rb index 555c1afd..a8d563e6 100644 --- a/lib/blueprinter/association.rb +++ b/lib/blueprinter/association.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true -require_relative 'field' -require_relative 'blueprint_validator' +require 'blueprinter/field' +require 'blueprinter/blueprint_validator' +require 'blueprinter/extractors/association_extractor' module Blueprinter # @api private diff --git a/lib/blueprinter/base.rb b/lib/blueprinter/base.rb index 0083dde3..b37961c0 100644 --- a/lib/blueprinter/base.rb +++ b/lib/blueprinter/base.rb @@ -1,25 +1,10 @@ # 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' +require 'blueprinter/association' +require 'blueprinter/extractors/association_extractor' +require 'blueprinter/field' +require 'blueprinter/helpers/base_helpers' +require 'blueprinter/reflection' module Blueprinter class Base 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..ce19178e 100644 --- a/lib/blueprinter/configuration.rb +++ b/lib/blueprinter/configuration.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require 'json' -require_relative 'extensions' +require 'blueprinter/extensions' +require 'blueprinter/extractors/auto_extractor' module Blueprinter class Configuration @@ -49,12 +50,4 @@ def valid_callable?(callable_name) VALID_CALLABLES.include?(callable_name) end end - - def self.configuration - @configuration ||= Configuration.new - end - - def self.configure - yield configuration if block_given? - end end diff --git a/lib/blueprinter/empty_types.rb b/lib/blueprinter/empty_types.rb index 03debe98..0a881a7c 100644 --- a/lib/blueprinter/empty_types.rb +++ b/lib/blueprinter/empty_types.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative 'helpers/type_helpers' +require 'blueprinter/helpers/type_helpers' module Blueprinter EMPTY_COLLECTION = 'empty_collection' 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/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/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 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 }