autoload_for 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in autoload_for.gemspec
4
+ gemspec
@@ -0,0 +1,34 @@
1
+ autoload_for
2
+ ====
3
+
4
+ Autoload_for is a small gem that allows for autoloading of all ruby files underneath
5
+ a directory, assuming a class namespacing scheme consistent with the directory
6
+ structure.
7
+
8
+ ## Why?
9
+
10
+ Rails spoiled us. If we have the following paths to files in our Rails app with contents as below:
11
+
12
+ PATH TO FILE CONTENTS OF FILE
13
+ ------------------------------------------------ ----------------------------------------------------------------------------------------------
14
+ app/controllers/users_controller.rb class UserController < ActionController::Base; def index; @users = User.all; end; end
15
+ app/controllers/users/roles_controller.rb class Users::RolesController < ActionController::Base; end
16
+ app/models/user.rb class User < ActiveRecord::Base; @role = Role.new; end
17
+ app/models/user/role.rb class User::Role; @user = User.new; end
18
+
19
+ then Rails automatically loads the classes in. As long as the class name corresponds to the directory and file name it resides in, we're good to go.
20
+
21
+ But when we step outside of our cushy Rails world and we want to write a Ruby gem, or a Sinatra app, or something else, then we have to manually require these files, and in the right order. If the above files were not in a non-Rails application, we would have to require user.rb before users_controller.rb, since UserController depends on User. What's worse, there is a circular dependency between user.rb and role.rb. The only way to make it work is to use the autoload kernel method. **autoload_for** fixes all that.
22
+
23
+ ## How to use it
24
+
25
+ gem install autoload_for
26
+
27
+ And in your code (say you want to autoload the files listed in the above example):
28
+
29
+ require 'autoload_for'
30
+ include AutoloadFor
31
+ autoload_for('app/controllers'))
32
+ autoload_for('app/models')
33
+
34
+ That's all there is to it. Happy autoloading!
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "autoload_for"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "autoload_for"
7
+ s.version = AutoloadFor::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Charles Finkel"]
10
+ s.email = ["charles.finkel@gmail.com"]
11
+ s.homepage = "https://github.com/charleseff/autoload_for"
12
+ s.summary = %q{Have your classes in a directory autoloaded, just like Rails does with app/models, app/controllers, etc.}
13
+ s.description = %q{Have your classes in a directory autoloaded, just like Rails does with app/models, app/controllers, etc.}
14
+
15
+ s.rubyforge_project = "autoload_for"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activesupport'
23
+ s.add_development_dependency 'rspec', '>= 2.6.0'
24
+
25
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_support/core_ext'
2
+
3
+ module AutoloadFor
4
+ VERSION = '0.0.2'
5
+
6
+ def autoload_for(dir)
7
+ Dir.glob(File.join(dir, "**/")).each do |inner_dir|
8
+ dir_to_modulize = inner_dir.split("#{dir}/")[1]
9
+ eval "module ::#{dir_to_modulize.chop.camelize}; end" if dir_to_modulize
10
+ end
11
+
12
+ Dir.glob(File.join(dir, '**', '*.rb')).each do |file|
13
+ split = file.split("#{dir}/")[1].split('/')
14
+ class_symbol = "#{split.last.chomp('.rb').camelize}".to_sym
15
+ if (split.size > 1)
16
+ the_module = eval(split[0, split.size-1].join('/').camelize)
17
+ the_module.autoload class_symbol, file
18
+ else
19
+ ::Object.autoload class_symbol, file
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ class Animal::Fish; end
@@ -0,0 +1 @@
1
+ class Animal::Mammal::Cat; end
@@ -0,0 +1 @@
1
+ class Animal::Mammal::Dog; end
@@ -0,0 +1 @@
1
+ class Zoo; end
@@ -0,0 +1,33 @@
1
+ require File.join(File.expand_path('../../..', __FILE__), 'lib', 'autoload_for')
2
+ include AutoloadFor
3
+
4
+ require 'active_support/inflector'
5
+
6
+ describe AutoloadFor do
7
+
8
+ describe '.autoload_for' do
9
+
10
+ let(:fixtures_dir) { (File.join(File.expand_path('../..', __FILE__), 'fixtures')) }
11
+
12
+ it "should not have classes or modules to be loaded if not called" do
13
+ expect { Animal }.to raise_error
14
+ expect { Animal::Mammal }.to raise_error
15
+ expect { Animal::Mammal::Cat }.to raise_error
16
+ expect { Animal::Mammal::Dog }.to raise_error
17
+ expect { Animal::Fish }.to raise_error
18
+ expect { Zoo }.to raise_error
19
+
20
+ autoload_for fixtures_dir
21
+
22
+ expect { Animal }.to_not raise_error
23
+ expect { Animal::Mammal }.to_not raise_error
24
+ expect { Animal::Mammal::Cat }.to_not raise_error
25
+ expect { Animal::Mammal::Dog }.to_not raise_error
26
+ expect { Animal::Fish }.to_not raise_error
27
+ expect { Zoo }.to_not raise_error
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autoload_for
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Charles Finkel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-19 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.6.0
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: Have your classes in a directory autoloaded, just like Rails does with app/models, app/controllers, etc.
38
+ email:
39
+ - charles.finkel@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - README.md
50
+ - Rakefile
51
+ - autoload_for.gemspec
52
+ - lib/autoload_for.rb
53
+ - spec/fixtures/animal/fish.rb
54
+ - spec/fixtures/animal/mammal/cat.rb
55
+ - spec/fixtures/animal/mammal/dog.rb
56
+ - spec/fixtures/zoo.rb
57
+ - spec/lib/autoload_for_spec.rb
58
+ homepage: https://github.com/charleseff/autoload_for
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: autoload_for
81
+ rubygems_version: 1.8.2
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Have your classes in a directory autoloaded, just like Rails does with app/models, app/controllers, etc.
85
+ test_files: []
86
+