Skip to content

Commit

Permalink
Merge pull request #445 from procore-oss/autoload
Browse files Browse the repository at this point in the history
Use autoload instead of require/require_relative
  • Loading branch information
jhollinger authored Jul 25, 2024
2 parents a3099b8 + 888fca8 commit f99f5f6
Show file tree
Hide file tree
Showing 29 changed files with 82 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions lib/blueprinter.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions lib/blueprinter/association.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 5 additions & 20 deletions lib/blueprinter/base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/blueprinter/blueprint_validator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative 'errors/invalid_blueprint'

module Blueprinter
# @api private
class BlueprintValidator
Expand Down
11 changes: 2 additions & 9 deletions lib/blueprinter/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/blueprinter/empty_types.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative 'helpers/type_helpers'
require 'blueprinter/helpers/type_helpers'

module Blueprinter
EMPTY_COLLECTION = 'empty_collection'
Expand Down
7 changes: 7 additions & 0 deletions lib/blueprinter/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Blueprinter
module Errors
autoload :InvalidBlueprint, 'blueprinter/errors/invalid_blueprint'
end
end
2 changes: 0 additions & 2 deletions lib/blueprinter/errors/invalid_blueprint.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'blueprinter/blueprinter_error'

module Blueprinter
module Errors
class InvalidBlueprint < Blueprinter::BlueprinterError; end
Expand Down
3 changes: 3 additions & 0 deletions lib/blueprinter/extractors/association_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require 'blueprinter/extractor'
require 'blueprinter/empty_types'

module Blueprinter
# @api private
class AssociationExtractor < Extractor
Expand Down
7 changes: 7 additions & 0 deletions lib/blueprinter/extractors/auto_extractor.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/blueprinter/extractors/block_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/extractor'

module Blueprinter
# @api private
class BlockExtractor < Extractor
Expand Down
2 changes: 2 additions & 0 deletions lib/blueprinter/extractors/hash_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/extractor'

module Blueprinter
# @api private
class HashExtractor < Extractor
Expand Down
2 changes: 2 additions & 0 deletions lib/blueprinter/extractors/public_send_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/extractor'

module Blueprinter
# @api private
class PublicSendExtractor < Extractor
Expand Down
3 changes: 3 additions & 0 deletions lib/blueprinter/helpers/base_helpers.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/blueprinter/view_collection.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/view'

module Blueprinter
# @api private
class ViewCollection
Expand Down
2 changes: 1 addition & 1 deletion spec/benchmarks/active_record_big_o_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'activerecord_helper'
require 'benchmark_helper'
require 'blueprinter/base'
require 'blueprinter'

class Blueprinter::ActiveRecordBigOTest < Minitest::Benchmark
include FactoryBot::Syntax::Methods
Expand Down
2 changes: 1 addition & 1 deletion spec/benchmarks/active_record_ips_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'activerecord_helper'
require 'benchmark_helper'
require 'blueprinter/base'
require 'blueprinter'

class Blueprinter::ActiveRecordIPSTest < Minitest::Test
include FactoryBot::Syntax::Methods
Expand Down
2 changes: 1 addition & 1 deletion spec/benchmarks/big_o_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'benchmark_helper'
require 'blueprinter/base'
require 'blueprinter'
require 'ostruct'

class Blueprinter::BigOTest < Minitest::Benchmark
Expand Down
2 changes: 1 addition & 1 deletion spec/benchmarks/ips_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'benchmark_helper'
require 'blueprinter/base'
require 'blueprinter'
require 'ostruct'

class Blueprinter::IPSTest < Minitest::Test
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module SpecHelpers
def reset_blueprinter_config!
Blueprinter.instance_variable_set(:@configuration, nil)
Blueprinter.reset_configuration!
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/units/association_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/association'

describe Blueprinter::Association do
describe '#initialize' do
let(:blueprint) { Class.new(Blueprinter::Base) }
Expand Down
2 changes: 2 additions & 0 deletions spec/units/blueprint_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/units/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions spec/units/date_time_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
2 changes: 2 additions & 0 deletions spec/units/deprecation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/deprecation'

describe 'Blueprinter::Deprecation' do
describe '#report' do
TEST_MESSAGE = "Test Message"
Expand Down
1 change: 1 addition & 0 deletions spec/units/extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'ostruct'
require 'blueprinter/extensions'

describe Blueprinter::Extensions do
let(:all_extensions) {
Expand Down
2 changes: 2 additions & 0 deletions spec/units/reflection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/reflection'

describe Blueprinter::Reflection do
let(:category_blueprint) {
Class.new(Blueprinter::Base) do
Expand Down
2 changes: 2 additions & 0 deletions spec/units/view_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'blueprinter/view_collection'

describe 'ViewCollection' do
subject(:view_collection) { Blueprinter::ViewCollection.new }

Expand Down

0 comments on commit f99f5f6

Please sign in to comment.