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

Refactor Application #9

Merged
merged 2 commits into from
Sep 24, 2023
Merged
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
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
11 changes: 11 additions & 0 deletions sig/pathname.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Pathname
def initialize: (String path) -> void

def read: -> String

def write: (String contents) -> void

def to_s: -> String

def dirname: -> Pathname
end
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