Skip to content

Commit

Permalink
Merge pull request #67 from eudoxa/refactor_loader_and_related_spec
Browse files Browse the repository at this point in the history
refactor loader and related spec
  • Loading branch information
eudoxa committed Dec 5, 2022
2 parents fe6e6bb + ef1da3c commit 45ee176
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
43 changes: 22 additions & 21 deletions lib/chanko/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def load
constantize
rescue NameError
# Chanko never raise error even if the constant fails to reference
nil
false
end

def constantize
Expand Down Expand Up @@ -98,33 +98,38 @@ def self.load(name)
self.new(name).load
end

def initialize(name)
@name = name
def self.load_from_cache(name)
self.cache[name]
end

def load
if loaded?
load_from_cache
else
load_from_file
end
def self.save_to_cache(name, unit)
self.cache[name] = unit
end

def loaded?
cache[@name] != nil
def initialize(name)
@name = name
end

def load_from_cache
cache[@name]
def load
load_from_cache.then do |unit|
next unit unless unit.nil?
load_from_file_and_store_to_cache
end
end

def load_from_file
def load_from_file_and_store_to_cache
add_autoload_path
cache[@name] = constantize
constantize.tap do |unit|
self.class.save_to_cache(@name, unit)
end
rescue Exception => exception
ExceptionHandler.handle(exception)
cache[@name] = false
nil
self.class.save_to_cache(@name, false)
false
end

def load_from_cache
self.class.load_from_cache(@name)
end

def add_autoload_path
Expand All @@ -139,10 +144,6 @@ def autoload_path
def constantize
@name.to_s.camelize.constantize
end

def cache
self.class.cache
end
end
end
end
16 changes: 9 additions & 7 deletions spec/chanko/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ module Chanko

context "when non-existent unit name is passed" do
it "returns nil" do
expect(Chanko::Loader.load(:non_existent_unit)).to eq(nil)
expect(Chanko::Loader.load(:non_existent_unit)).to eq(false)
end
end

context "when loader has ever loaded specified unit" do
it "load unit from cache", classic: true do
expect_any_instance_of(Chanko::Loader::ClassicLoader).to receive(:load_from_file).and_call_original
Chanko::Loader.load(:example_unit)
Chanko::Loader.load(:example_unit)
expect(Chanko::Loader::ClassicLoader).to receive(:load_from_cache).twice.and_call_original
expect(Chanko::Loader::ClassicLoader).to receive(:save_to_cache).with(anything, ExampleUnit).and_call_original
expect(Chanko::Loader.load(:example_unit)).to eq(ExampleUnit)
expect(Chanko::Loader.load(:example_unit)).to eq(ExampleUnit)
end
end

Expand All @@ -33,9 +34,10 @@ module Chanko
end

it "load unit from cache", classic: true do
expect_any_instance_of(Chanko::Loader::ClassicLoader).to receive(:load_from_file).and_call_original
Chanko::Loader.load(:non_existent_unit)
Chanko::Loader.load(:non_existent_unit)
expect(Chanko::Loader::ClassicLoader).to receive(:load_from_cache).twice.and_call_original
expect(Chanko::Loader::ClassicLoader).to receive(:save_to_cache).with(anything, false).and_call_original
expect(Chanko::Loader.load(:non_existent_unit)).to eq(false)
expect(Chanko::Loader.load(:non_existent_unit)).to eq(false)
end
end
end
Expand Down

0 comments on commit 45ee176

Please sign in to comment.