Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.envrc
14 changes: 0 additions & 14 deletions my_namespace-my_gem/lib/my_namespace/my_gem.rb

This file was deleted.

7 changes: 0 additions & 7 deletions my_namespace-my_gem/lib/my_namespace/my_gem/version.rb

This file was deleted.

2 changes: 1 addition & 1 deletion my_rails_app/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

ruby "3.3.0"

gem "my_namespace-my_gem", path: "../my_namespace-my_gem"
gem "my_root_namespace-my_namespace-my_gem", path: "../my_root_namespace-my_namespace-my_gem"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.1.3"
Expand Down
7 changes: 4 additions & 3 deletions my_rails_app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PATH
remote: ../my_namespace-my_gem
remote: ../my_root_namespace-my_namespace-my_gem
specs:
my_namespace-my_gem (0.1.0)
my_root_namespace-my_namespace-my_gem (0.1.0)
http
zeitwerk

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -269,7 +270,7 @@ DEPENDENCIES
bootsnap
debug
importmap-rails
my_namespace-my_gem!
my_root_namespace-my_namespace-my_gem!
propshaft
puma (>= 5.0)
rails (~> 7.1.3)
Expand Down
2 changes: 1 addition & 1 deletion my_rails_app/app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DashboardController < ApplicationController
def show
render inline: MyNamespace::MyGem.do_something
render inline: MyRootNamespace::MyNamespace::MyGem.do_something
end
end
28 changes: 28 additions & 0 deletions my_rails_app/app/utils/local_gem_reloading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class LocalGemReloading
def self.setup_reloading_for_local_zeitwerk_gem(gem_name, local_gems_folder)
gem_path = Pathname.new(File.join(local_gems_folder, gem_name, "lib"))

parts = gem_name.split("-")
gem_root = parts.delete(parts.last)
gem_namespaces = parts.map(&:camelize)

loader_tag = "#{[gem_namespaces.join("::"), gem_root.underscore].join('-')}"

gem_loader = Zeitwerk::Registry.loaders.find { |loader| loader.tag == loader_tag }

file_watcher = ActiveSupport::FileUpdateChecker.new(gem_path.glob('**/*')) do
gem_loader.reload
end

Rails.application.reloaders << Class.new do
def initialize(file_watcher)
@file_watcher = file_watcher
end

def updated?
@file_watcher.execute_if_updated
end
end.new(file_watcher)
true
end
end
6 changes: 6 additions & 0 deletions my_rails_app/config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "active_support/core_ext/integer/time"
require_relative "../../app/utils/local_gem_reloading"

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
Expand Down Expand Up @@ -71,4 +72,9 @@

# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true

LocalGemReloading.setup_reloading_for_local_zeitwerk_gem(
"my_root_namespace-my_namespace-my_gem",
Rails.root.join("../")
)
end
59 changes: 59 additions & 0 deletions my_root_namespace-my_namespace-my_gem/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
PATH
remote: .
specs:
my_root_namespace-my_namespace-my_gem (0.1.0)
http
zeitwerk

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
base64 (0.2.0)
diff-lcs (1.5.1)
domain_name (0.6.20240107)
ffi (1.16.3)
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
rake
http (5.2.0)
addressable (~> 2.8)
base64 (~> 0.1)
http-cookie (~> 1.0)
http-form_data (~> 2.2)
llhttp-ffi (~> 0.5.0)
http-cookie (1.0.5)
domain_name (~> 0.5)
http-form_data (2.3.0)
llhttp-ffi (0.5.0)
ffi-compiler (~> 1.0)
rake (~> 13.0)
public_suffix (5.0.4)
rake (13.1.0)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.0)
zeitwerk (2.6.13)

PLATFORMS
ruby
x86_64-darwin-21

DEPENDENCIES
my_root_namespace-my_namespace-my_gem!
rake (~> 13.0)
rspec (~> 3.0)

BUNDLED WITH
2.5.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "http"
require "zeitwerk"
loader = Zeitwerk::Loader.for_gem_extension(MyRootNamespace::MyNamespace)
loader.enable_reloading if ENV["LOCAL_GEM_RELOADING"]
loader.setup

module MyRootNamespace
module MyNamespace
module MyGem
def self.do_something
Something.new.do
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module MyRootNamespace
module MyNamespace
module MyGem
class Something
def do
return "A"
return "B"
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module MyRootNamespace
module MyNamespace
module MyGem
VERSION = "0.1.0"
end
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require_relative "lib/my_namespace/my_gem/version"
require_relative "lib/my_root_namespace/my_namespace/my_gem/version"

Gem::Specification.new do |spec|
spec.name = "my_namespace-my_gem"
spec.version = MyNamespace::MyGem::VERSION
spec.name = "my_root_namespace-my_namespace-my_gem"
spec.version = MyRootNamespace::MyNamespace::MyGem::VERSION
spec.authors = ["David"]
spec.email = ["asmoo252@gmail.com"]

Expand Down Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|

# Uncomment to register a new dependency of your gem
spec.add_dependency "http"
spec.add_dependency "zeitwerk"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down