Skip to content

Commit

Permalink
Refactor Application to use Pathnames
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Sep 24, 2023
1 parent 4d9a124 commit c58452b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
12 changes: 4 additions & 8 deletions lib/kangaru/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions sig/kangaru/application.rbs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 2 additions & 14 deletions spec/kangaru/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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

Expand Down

0 comments on commit c58452b

Please sign in to comment.