Skip to content

Commit c093764

Browse files
authored
Add autoloading via Zeitwerk (#891)
Closes #886
1 parent 042c2d5 commit c093764

File tree

6 files changed

+17
-39
lines changed

6 files changed

+17
-39
lines changed

config/quickdraw.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
require "phlex"
2525

26-
Phlex.eager_load
26+
Phlex::Loader.eager_load
2727

2828
# Previous content of test helper now starts here
2929
$LOAD_PATH.unshift(File.expand_path("../fixtures", __dir__))

lib/phlex.rb

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
require "erb"
44
require "set"
5+
require "zeitwerk"
56

67
module Phlex
7-
autoload :VERSION, "phlex/version"
8-
9-
autoload :Kit, "phlex/kit"
10-
autoload :FIFO, "phlex/fifo"
11-
autoload :Helpers, "phlex/helpers"
12-
autoload :FIFOCacheStore, "phlex/fifo_cache_store"
13-
14-
autoload :CSV, "phlex/csv"
15-
autoload :SVG, "phlex/svg"
16-
autoload :HTML, "phlex/html"
17-
autoload :SGML, "phlex/sgml"
18-
19-
autoload :Error, "phlex/error"
20-
autoload :NameError, "phlex/errors/name_error"
21-
autoload :RuntimeError, "phlex/errors/runtime_error"
22-
autoload :ArgumentError, "phlex/errors/argument_error"
23-
autoload :DoubleRenderError, "phlex/errors/double_render_error"
8+
Loader = Zeitwerk::Loader.for_gem.tap do |loader|
9+
loader.push_dir("lib/phlex/errors", namespace: Phlex)
10+
loader.inflector.inflect(
11+
"csv" => "CSV",
12+
"fifo" => "FIFO",
13+
"fifo_cache_store" => "FIFOCacheStore",
14+
"html" => "HTML",
15+
"sgml" => "SGML",
16+
"svg" => "SVG",
17+
)
18+
19+
loader.setup
20+
end
2421

2522
Escape = ERB::Escape
2623

@@ -35,17 +32,6 @@ def self.__expand_attribute_cache__(file_path)
3532
end
3633
end
3734

38-
def self.eager_load
39-
queue = [self]
40-
41-
while (mod = queue.shift)
42-
mod.constants.each do |const_name|
43-
const = mod.const_get(const_name)
44-
queue << const if Module === const
45-
end
46-
end
47-
end
48-
4935
# Generate an HTML string using Phlex’ HTML DSL
5036
def self.html(&block)
5137
HTML.call do |component|

lib/phlex/html.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# frozen_string_literal: true
22

33
class Phlex::HTML < Phlex::SGML
4-
autoload :StandardElements, "phlex/html/standard_elements"
5-
autoload :VoidElements, "phlex/html/void_elements"
6-
74
extend Phlex::SGML::Elements
85
include VoidElements, StandardElements
96

lib/phlex/sgml.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ def compiler.add_insert_cmd(out, content)
1616
end
1717
end
1818

19-
autoload :Elements, "phlex/sgml/elements"
20-
autoload :SafeObject, "phlex/sgml/safe_object"
21-
autoload :SafeValue, "phlex/sgml/safe_value"
22-
autoload :State, "phlex/sgml/state"
23-
2419
include Phlex::Helpers
2520

2621
class << self

lib/phlex/svg.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
class Phlex::SVG < Phlex::SGML
4-
autoload :StandardElements, "phlex/svg/standard_elements"
5-
64
include StandardElements
75

86
# Returns the string "image/svg+xml"

phlex.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ Gem::Specification.new do |spec|
2727
]
2828

2929
spec.require_paths = ["lib"]
30+
31+
spec.add_runtime_dependency "zeitwerk"
3032
end

0 commit comments

Comments
 (0)