Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load autoloaded bucket components #678

Merged
merged 5 commits into from
Mar 16, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 29 additions & 12 deletions lib/phlex/bucket.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# frozen_string_literal: true

module Phlex::Bucket
module Proxy
def method_missing(name, *args, **kwargs, &block)
if self.class.constants.include?(name) && self.class.const_get(name) && methods.include?(name)
public_send(name, *args, **kwargs, &block)
else
super
end
end
joeldrapper marked this conversation as resolved.
Show resolved Hide resolved

def respond_to_missing?(name, include_private = false)
self.class.constants.include?(name) && self.class.const_get(name) && methods.include?(name)
end
joeldrapper marked this conversation as resolved.
Show resolved Hide resolved
end

def self.extended(mod)
warn "🚨 [WARNING] Phlex::Bucket is experimental and may be removed from future versions of Phlex."
mod.include(Proxy)
end

def method_missing(name, *args, **kwargs, &block)
if constants.include?(name) && const_get(name) && methods.include?(name)
public_send(name, *args, **kwargs, &block)
else
super
end
end

# Eager load all constants in the module for apps that use Zeitwerk.
mod.constants.each { |c| mod.const_get(c) }
def respond_to_missing?(name, include_private = false)
constants.include?(name) && const_get(name) && methods.include?(name)
end

def const_added(name)
# This can sometime be triggered by an autoload, which means it gets
# triggered a second time when we call `const_get` below and Ruby loads it.
return super if Fiber[:__phlex_adding_bucket_const__]

begin
Fiber[:__phlex_adding_bucket_const__] = true
constant = const_get(name)
ensure
Fiber[:__phlex_adding_bucket_const__] = false
end
return if autoload?(name)

constant = const_get(name)

if instance_methods.include?(name)
raise NameError, "The instance method `#{name}' is already defined on `#{inspect}`."
Expand Down