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 18, 2024
1 parent ae408fb commit 6a06e67
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 41 deletions.
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
16 changes: 8 additions & 8 deletions lib/blueprinter/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative 'extensions'

module Blueprinter
class Configuration
attr_accessor :association_default, :datetime_format, :deprecations, :field_default, :generator, :if, :method,
Expand Down Expand Up @@ -47,13 +45,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

0 comments on commit 6a06e67

Please sign in to comment.