Skip to content

Commit

Permalink
added namespace option for AutoLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Jul 20, 2011
1 parent 7f56613 commit d78e566
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
29 changes: 24 additions & 5 deletions lib/sugar-high/class_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,48 @@ def include_and_extend(the_module, options={})

def autoload_modules *args

options = args.extract_options!
options = args.extract_options!
root = options[:root] || AutoLoader.root || ''
path = root.strip.empty? ? self.name.to_s.underscore : [root, self.name.to_s.underscore].join('/')
from = options[:from] || path

# Here also could be adding of the file in top of load_paths like: $:.unshift File.dirname(__FILE__)
# It is very useful for situations of having not load_paths built Rails or Gems way.
args.each do |req_name|
send :autoload, req_name, "#{from}/#{req_name.to_s.underscore}"
args.each do |req_name|
ruby_file = req_name.to_s.underscore

send :autoload, req_name, AutoLoader.translate("#{from}/#{ruby_file}")
end
end

end

module AutoLoader
@@root = ''
@@namespaces = {}

def self.root
@@root
end

def self.root= root_dir
@@root = root_dir
def self.namespaces
@@namespaces
end

def self.root= root
@@root = root
end

def self.namespaces= namespaces
@@namespaces = namespaces
end

def self.translate name
names = name.split('/')
names.map do |name|
clazz_name = name.to_s.camelize
namespaces[clazz_name.to_sym] ? namespaces[clazz_name.to_sym] : name
end.join('/')
end
end

Expand Down
3 changes: 3 additions & 0 deletions spec/auto_load_blank_root.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module AutoLoadBlankRoot
autoload_modules :HelloSailor
end
7 changes: 7 additions & 0 deletions spec/autoload_blank_root/sailor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module AutoLoadBlankRoot
module HelloSailor
def self.test
"test"
end
end
end
48 changes: 31 additions & 17 deletions spec/sugar-high/class_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,53 @@ def trial
describe Module do

describe "#include_and_extend" do
it "should include class methods" do
Third.should respond_to(:class_method)
it "should include class methods directly" do
Second.should respond_to(:class_method)
end


it "should not include class methods indirectly" do
Third.should_not respond_to(:class_method)
end

it "should include class methods" do
Third.new.should respond_to(:instance_method)
end
end

describe "#autoload_modules" do
it "should autoload modules using :from => path" do
require 'fixtures/autoload_modules'
AutoloadModules::Third.should respond_to(:test)
end

it "should autoload modules from __FILE__'s dir if :from is omitted'" do
require 'fixtures/autoload_modulez'
AutoloadModulez::ThirdOneHere.should respond_to(:test)
end

context 'using AutoLoader.root' do
it 'empty root' do
AutoLoader.root = ''
require 'autoload_blank_root'
AutoloadBlankRoot::Hello.should respond_to(:test)
end
end

it 'should autoload modules using ClassExt#autoload_root' do
AutoLoader.root = 'fixtures'
require 'fixtures/autoload_modules_root'
AutoloadModulesRoot::Third.should respond_to(:test)
end
end
end
end

context 'using AutoLoader.namespaces' do
it 'empty root' do
AutoLoader.root = ''
AutoLoader.namespaces= {:AutoLoadBlankRoot => 'autoload_blank_root', :HelloSailor => 'sailor'}
require 'auto_load_blank_root'
AutoLoadBlankRoot::HelloSailor.should respond_to(:test)
end
end
end
end

Expand Down Expand Up @@ -116,7 +130,7 @@ def trial
trial.try_class('GoodBye').should be_false
end
end

describe '#class_exists?' do
it "should return false if no class found" do
trial.class_exists?('Blip').should be_false
Expand All @@ -129,8 +143,8 @@ def trial
it "should return false if module found" do
trial.class_exists?('GoodBye').should be_false
end
end
end

describe '#module_exists?' do
it "should return false if no module found" do
trial.module_exists?('Blip').should be_false
Expand All @@ -139,27 +153,27 @@ def trial
it "should return true if module found" do
trial.module_exists?('GoodBye').should be_true
end

it "should return false if only class found" do
trial.module_exists?('Hello').should be_false
end
end
end

describe '#try_module_only' do
it 'should find module' do
trial.try_module_only('Hello').should be_false
trial.try_module_only('GoodBye').should be_true
end
end
end

describe '#find_first_class' do
it 'should find first class' do
trial.find_first_class('GoodBye', 'Hello').should == Hello
end

it 'should not find any module' do
lambda {trial.find_first_class('Good', 'Bye') }.should raise_error
end
end
end

describe '#find_first_module' do
Expand All @@ -171,5 +185,5 @@ def trial
it 'should not find any module' do
lambda {trial.find_first_module('Good', 'Bye') }.should raise_error
end
end
end
end

0 comments on commit d78e566

Please sign in to comment.