From c58452b2f938a1de11f09317a637892891c6764f Mon Sep 17 00:00:00 2001 From: Chris Welham <71787007+apexatoll@users.noreply.github.com> Date: Sun, 24 Sep 2023 17:55:00 +0100 Subject: [PATCH] Refactor Application to use Pathnames --- lib/kangaru/application.rb | 12 ++++-------- sig/kangaru/application.rbs | 8 ++------ spec/kangaru/application_spec.rb | 16 ++-------------- 3 files changed, 8 insertions(+), 28 deletions(-) diff --git a/lib/kangaru/application.rb b/lib/kangaru/application.rb index c753616..29f13e2 100644 --- a/lib/kangaru/application.rb +++ b/lib/kangaru/application.rb @@ -5,15 +5,11 @@ class Application attr_reader :root_file, :root_dir, :namespace def initialize(root_file:, namespace:) - @root_file = root_file - @root_dir = File.dirname(root_file) + @root_file = Pathname.new(root_file) + @root_dir = @root_file.dirname @namespace = namespace end - def app_dir - @app_dir ||= File.join(root_dir, namespace.to_s.to_snakecase) - end - def setup autoloader.setup end @@ -28,8 +24,8 @@ def run!(argv) def autoloader @autoloader ||= Zeitwerk::Loader.new.tap do |loader| - loader.inflector = Zeitwerk::GemInflector.new(root_file) - loader.push_dir(app_dir, namespace:) + loader.inflector = Zeitwerk::GemInflector.new(root_file.to_s) + loader.push_dir(root_dir.to_s) end end end diff --git a/sig/kangaru/application.rbs b/sig/kangaru/application.rbs index c57dfc9..26e21f6 100644 --- a/sig/kangaru/application.rbs +++ b/sig/kangaru/application.rbs @@ -1,15 +1,11 @@ module Kangaru class Application - attr_reader root_file: String - attr_reader root_dir: String + attr_reader root_file: Pathname + attr_reader root_dir: Pathname attr_reader namespace: Module - @app_dir: String - def initialize: (root_file: String, namespace: Module) -> void - def app_dir: -> String - def setup: -> void def run!: (Array[String]) -> void diff --git a/spec/kangaru/application_spec.rb b/spec/kangaru/application_spec.rb index 3d31142..9e1e043 100644 --- a/spec/kangaru/application_spec.rb +++ b/spec/kangaru/application_spec.rb @@ -7,18 +7,6 @@ before { stub_const "SomeApp", Module.new } - describe "#app_dir" do - subject(:app_dir) { application.app_dir } - - it "returns a string" do - expect(app_dir).to be_a(String) - end - - it "returns the expected app directory" do - expect(app_dir).to eq("/some_app/lib/some_app") - end - end - describe "#setup" do subject(:setup) { application.setup } @@ -45,12 +33,12 @@ expect(loader).to have_received(:inflector=).with(gem_inflector).once end - it "configures the loader to load the app dir" do + it "configures the loader to load the root dir" do setup expect(loader) .to have_received(:push_dir) - .with(application.app_dir, namespace:) + .with("/some_app/lib") .once end