Permalink
Browse files

small refactoring with lots of changes

  • Loading branch information...
1 parent 6c8c42e commit 693950d73f8b3dde65672815166b096bb7a9c1a7 @alexeypetrushin committed May 19, 2011
View
@@ -1,5 +1,8 @@
raise 'ruby 1.9.2 or higher required!' if RUBY_VERSION < '1.9.2'
+module ClassLoader
+end
+
%w(
support
file_system_adapter/camel_case_translator
@@ -1,44 +1,42 @@
-module ClassLoader
- class ChainedAdapter
- attr_accessor :adapters
+class ClassLoader::ChainedAdapter
+ attr_accessor :adapters
- def initialize
- @adapters = []
- end
-
- %w(
- exist?
- read
- to_file_path
- to_class_name
- ).each do |method|
- define_method method do |*args|
- catch :found do
- adapters.each do |a|
- value = a.send method, *args
- throw :found, value if value
- end
- nil
+ def initialize
+ @adapters = []
+ end
+
+ %w(
+ exist?
+ read
+ to_file_path
+ to_class_name
+ ).each do |method|
+ define_method method do |*args|
+ catch :found do
+ adapters.each do |a|
+ value = a.send method, *args
+ throw :found, value if value
end
+ nil
end
end
-
- def each_changed_class &block
- adapters.each{|a| a.each_changed_class &block}
- end
-
- def each_class &block
- adapters.each{|a| a.each_class &block}
- end
-
- def clear
- adapters.each{|a| a.clear}
- end
-
- def add_path *args
- adapters.each do |a|
- a.add_path *args if a.respond_to? :add_path
- end
+ end
+
+ def each_changed_class &block
+ adapters.each{|a| a.each_changed_class &block}
+ end
+
+ def each_class &block
+ adapters.each{|a| a.each_class &block}
+ end
+
+ def clear
+ adapters.each{|a| a.clear}
+ end
+
+ def add_path *args
+ adapters.each do |a|
+ a.add_path *args if a.respond_to? :add_path
end
end
end
@@ -1,141 +1,139 @@
-module ClassLoader
- class FileSystemAdapter
- attr_reader :translator
-
- def initialize class_name_translator
- @translator = class_name_translator
- @paths, @watched_paths, @file_name_cache = [], [], {}
- @watched_files, @first_check = {}, true
- end
-
- def exist? class_name
- !!to_file_path(class_name)
- end
- alias_method :exists?, :exist?
-
- def read class_name
- file_path = to_file_path class_name
- return nil unless file_path
-
- if file_path =~ /\.rb$/
- File.open(file_path){|f| f.read}
- else
- "module #{class_name}; end;"
- end
- end
+class ClassLoader::FileSystemAdapter
+ attr_reader :translator
+
+ def initialize class_name_translator
+ @translator = class_name_translator
+ @paths, @watched_paths, @file_name_cache = [], [], {}
+ @watched_files, @first_check = {}, true
+ end
+
+ def exist? class_name
+ !!to_file_path(class_name)
+ end
+ alias_method :exists?, :exist?
+
+ def read class_name
+ file_path = to_file_path class_name
+ return nil unless file_path
- def to_file_path class_name
- file_path, exist = @file_name_cache[class_name] || []
- unless exist
- normalized_name = translator.to_file_path class_name
- file_path = catch :found do
- # files
- paths.each do |base|
- try = "#{base}/#{normalized_name}.rb"
- if File.exist? try
- throw :found, try
- end
- end
-
- # dirs
- paths.each do |base|
- try = "#{base}/#{normalized_name}"
- if File.exist? try
- throw :found, try
- end
+ if file_path =~ /\.rb$/
+ File.open(file_path){|f| f.read}
+ else
+ "module #{class_name}; end;"
+ end
+ end
+
+ def to_file_path class_name
+ file_path, exist = @file_name_cache[class_name] || []
+ unless exist
+ normalized_name = translator.to_file_path class_name
+ file_path = catch :found do
+ # files
+ paths.each do |base|
+ try = "#{base}/#{normalized_name}.rb"
+ if File.exist? try
+ throw :found, try
end
-
- nil
end
- @file_name_cache[class_name] = [file_path, true]
- end
- file_path
- end
-
- def to_class_name normalized_path
- raise "Internal error, file_name should be absolute path (#{normalized_path})!" unless normalized_path =~ /^\//
- raise "Internal error, file_name should be without .rb suffix (#{normalized_path})!" if normalized_path =~ /\.rb$/
-
- paths.each do |base_path|
- if normalized_path.start_with? base_path
- normalized_name = normalized_path.sub(base_path + '/', '')
- if translator.is_it_class? normalized_name
- return translator.to_class_name(normalized_name)
+
+ # dirs
+ paths.each do |base|
+ try = "#{base}/#{normalized_name}"
+ if File.exist? try
+ throw :found, try
end
end
+
+ nil
end
- nil
+ @file_name_cache[class_name] = [file_path, true]
end
+ file_path
+ end
+
+ def to_class_name normalized_path
+ raise "Internal error, file_name should be absolute path (#{normalized_path})!" unless normalized_path =~ /^\//
+ raise "Internal error, file_name should be without .rb suffix (#{normalized_path})!" if normalized_path =~ /\.rb$/
- def add_path base_path, watch = false
- base_path = File.expand_path(base_path)
- # raise "#{base_path} already added!" if paths.include? base_path
- unless paths.include? base_path
- paths << base_path
- watched_paths << base_path if watch
+ paths.each do |base_path|
+ if normalized_path.start_with? base_path
+ normalized_name = normalized_path.sub(base_path + '/', '')
+ if translator.is_it_class? normalized_name
+ return translator.to_class_name(normalized_name)
+ end
end
end
-
- def clear
- @paths, @watched_paths, @file_name_cache = [], [], {}
- @watched_files, @first_check = {}, true
- end
-
- def each_changed_class &block
- unless @first_check == @watched_paths
- @first_check = @watched_paths.clone
- each_watched_file{|file_path, file_name| remember_file file_path}
- else
- each_watched_file do |file_path, file_name|
- if file_changed? file_path
- remember_file file_path
-
- normalized_name = file_name.sub(/\.rb$/, "")
- block.call translator.to_class_name(normalized_name)
- end
- end
- end
+ nil
+ end
+
+ def add_path base_path, watch = false
+ base_path = File.expand_path(base_path)
+ # raise "#{base_path} already added!" if paths.include? base_path
+ unless paths.include? base_path
+ paths << base_path
+ watched_paths << base_path if watch
end
-
- def each_class &block
- @paths.each do |base_path|
- Dir.glob("#{base_path}/**/*.rb").each do |file_path|
- normalized_path = file_path.sub(/\.rb$/, "")
+ end
+
+ def clear
+ @paths, @watched_paths, @file_name_cache = [], [], {}
+ @watched_files, @first_check = {}, true
+ end
+
+ def each_changed_class &block
+ unless @first_check == @watched_paths
+ @first_check = @watched_paths.clone
+ each_watched_file{|file_path, file_name| remember_file file_path}
+ else
+ each_watched_file do |file_path, file_name|
+ if file_changed? file_path
+ remember_file file_path
- normalized_name = normalized_path.sub(base_path + "/", '')
- class_name = translator.to_class_name(normalized_name)
- block.call class_name
+ normalized_name = file_name.sub(/\.rb$/, "")
+ block.call translator.to_class_name(normalized_name)
end
+ end
+ end
+ end
+
+ def each_class &block
+ @paths.each do |base_path|
+ Dir.glob("#{base_path}/**/*.rb").each do |file_path|
+ normalized_path = file_path.sub(/\.rb$/, "")
+
+ normalized_name = normalized_path.sub(base_path + "/", '')
+ class_name = translator.to_class_name(normalized_name)
+ block.call class_name
end
end
-
- def each_watched_file &block
- @watched_paths.each do |base_path|
- Dir.glob("#{base_path}/**/*.rb").each do |file_path|
- file_name = file_path.sub(base_path + '/', '')
+ end
+
+ def each_watched_file &block
+ @watched_paths.each do |base_path|
+ Dir.glob("#{base_path}/**/*.rb").each do |file_path|
+ file_name = file_path.sub(base_path + '/', '')
- if translator.is_it_class? file_name
- block.call file_path, file_name
- end
+ if translator.is_it_class? file_name
+ block.call file_path, file_name
end
end
end
+ end
+
+ def inspect
+ "FileSystemAdapter (#{@paths.join(', ')})"
+ end
+
+ protected
+ attr_reader :paths, :watched_paths, :watcher, :watched_files
- def inspect
- "FileSystemAdapter (#{@paths.join(', ')})"
- end
-
- protected
- attr_reader :paths, :watched_paths, :watcher, :watched_files
-
- def file_changed? file_path
- old_time = watched_files[file_path]
- old_time == nil or old_time != File.mtime(file_path)
- end
+ def file_changed? file_path
+ old_time = watched_files[file_path]
+ old_time == nil or old_time != File.mtime(file_path)
+ end
- def remember_file file_path
- watched_files[file_path] = File.mtime(file_path)
- end
-
- end
+ def remember_file file_path
+ watched_files[file_path] = File.mtime(file_path)
+ end
+
end
@@ -1,18 +1,16 @@
-module ClassLoader
- class CamelCaseTranslator
- def self.to_class_name normalized_file_name
- raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
- normalized_file_name.gsub('/', '::')
- end
-
- def self.to_file_path class_name
- raise "internall error, invalid format for #{class_name}!" if class_name =~ /^::/
- class_name.gsub('::', '/')
- end
-
- def self.is_it_class? normalized_file_name
- raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
- normalized_file_name[0..0] =~ /[A-Z]/
- end
+class ClassLoader::CamelCaseTranslator
+ def self.to_class_name normalized_file_name
+ raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
+ normalized_file_name.gsub('/', '::')
+ end
+
+ def self.to_file_path class_name
+ raise "internall error, invalid format for #{class_name}!" if class_name =~ /^::/
+ class_name.gsub('::', '/')
+ end
+
+ def self.is_it_class? normalized_file_name
+ raise "internall error, invalid format for #{normalized_file_name}!" if normalized_file_name =~ /^\//
+ normalized_file_name[0..0] =~ /[A-Z]/
end
end
Oops, something went wrong.

0 comments on commit 693950d

Please sign in to comment.