Skip to content

Commit

Permalink
Use autoload instead of require/require_relative
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Hollinger <[email protected]>
  • Loading branch information
jhollinger committed Jul 19, 2024
1 parent 2745cdb commit f7fa580
Show file tree
Hide file tree
Showing 13 changed files with 53 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 << 'spec' << 'lib'
t.pattern = 'spec/benchmarks/**/*_test.rb'
t.verbose = false
end
Expand Down
36 changes: 33 additions & 3 deletions lib/blueprinter.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions lib/blueprinter/association.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

require_relative 'field'
require_relative 'blueprint_validator'

module Blueprinter
# @api private
class Association < Field
Expand Down
21 changes: 0 additions & 21 deletions lib/blueprinter/base.rb
Original file line number Diff line number Diff line change
@@ -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
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
15 changes: 8 additions & 7 deletions lib/blueprinter/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'json'
require_relative 'extensions'

module Blueprinter
class Configuration
Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions lib/blueprinter/empty_types.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative 'helpers/type_helpers'

module Blueprinter
EMPTY_COLLECTION = 'empty_collection'
EMPTY_HASH = 'empty_hash'
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
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

0 comments on commit f7fa580

Please sign in to comment.