From 8e56f5ea3e5394caa2ffee466a7395876c288c2a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 24 Jun 2005 16:40:01 +0000 Subject: Improved performance of Routes generation by a factor of 5 #1434 [Nicholas Seckar] Added named routes (NEEDS BETTER DESCRIPTION) #1434 [Nicholas Seckar] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1496 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/core_ext/array.rb | 5 +++ .../lib/active_support/core_ext/array/to_param.rb | 12 ++++++ activesupport/lib/active_support/core_ext/cgi.rb | 5 +++ .../core_ext/cgi/escape_skipping_slashes.rb | 14 +++++++ activesupport/lib/active_support/dependencies.rb | 46 +++++++++++----------- 5 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/array.rb create mode 100644 activesupport/lib/active_support/core_ext/array/to_param.rb create mode 100644 activesupport/lib/active_support/core_ext/cgi.rb create mode 100644 activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb new file mode 100644 index 0000000000..83eef79b38 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -0,0 +1,5 @@ +require File.dirname(__FILE__) + '/array/to_param' + +class Array #:nodoc: + include ActiveSupport::CoreExtensions::Array::ToParam +end diff --git a/activesupport/lib/active_support/core_ext/array/to_param.rb b/activesupport/lib/active_support/core_ext/array/to_param.rb new file mode 100644 index 0000000000..85e91e6b1a --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/to_param.rb @@ -0,0 +1,12 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Array #:nodoc: + module ToParam #:nodoc: + # When an array is given to url_for, it is converted to a slash separated string. + def to_param + join '/' + end + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/cgi.rb b/activesupport/lib/active_support/core_ext/cgi.rb new file mode 100644 index 0000000000..2378297b7d --- /dev/null +++ b/activesupport/lib/active_support/core_ext/cgi.rb @@ -0,0 +1,5 @@ +require File.dirname(__FILE__) + '/cgi/escape_skipping_slashes' + +class CGI + extend(ActiveSupport::CoreExtensions::CGI::EscapeSkippingSlashes) +end diff --git a/activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb b/activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb new file mode 100644 index 0000000000..a21e98fa80 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb @@ -0,0 +1,14 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module CGI #:nodoc: + module EscapeSkippingSlashes #:nodoc: + def escape_skipping_slashes(str) + str = str.join('/') if str.respond_to? :join + str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do + "%#{$1.unpack('H2').first.upcase}" + end.tr(' ', '+') + end + end + end + end +end diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index dd210a8ebd..ef5fd0ce1f 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -14,7 +14,7 @@ module Dependencies end def depend_on(file_name, swallow_load_errors = false) - if !loaded.include?(file_name) + unless loaded.include?(file_name) loaded << file_name begin @@ -34,7 +34,7 @@ module Dependencies end def require_or_load(file_name) - file_name = "#{file_name}.rb" unless ! load? || /\.rb$/ =~ file_name + file_name = "#{file_name}.rb" unless ! load? || file_name[-3..-1] == '.rb' load? ? load(file_name) : require(file_name) end @@ -52,8 +52,10 @@ module Dependencies attr_reader :path attr_reader :root - def self.root(*load_paths) - RootLoadingModule.new(*load_paths) + class << self + def root(*load_paths) + RootLoadingModule.new(*load_paths) + end end def initialize(root, path=[]) @@ -61,7 +63,7 @@ module Dependencies @root = root end - def root?() self.root == self end + def root?() self.root == self end def load_paths() self.root.load_paths end # Load missing constants if possible. @@ -71,13 +73,15 @@ module Dependencies # Load the controller class or a parent module. def const_load!(name, file_name = nil) + file_name ||= 'application' if root? && name.to_s == 'ApplicationController' path = self.path + [file_name || name] load_paths.each do |load_path| fs_path = load_path.filesystem_path(path) next unless fs_path - if File.directory?(fs_path) + case + when File.directory?(fs_path) new_module = LoadingModule.new(self.root, self.path + [name]) self.const_set name, new_module if self.root? @@ -88,7 +92,7 @@ module Dependencies Object.const_set(name, new_module) end break - elsif File.file?(fs_path) + when File.file?(fs_path) self.root.load_file!(fs_path) # Import the loaded constant from Object provided we are the root node. @@ -97,7 +101,7 @@ module Dependencies end end - return self.const_defined?(name) + self.const_defined?(name) end # Is this name present or loadable? @@ -141,7 +145,7 @@ module Dependencies # if the path leads to a module, or the path to a file if it leads to an object. def filesystem_path(path, allow_module=true) fs_path = [@root] - fs_path += path[0..-2].collect {|name| const_name_to_module_name name} + fs_path += path[0..-2].map {|name| const_name_to_module_name name} if allow_module result = File.join(fs_path, const_name_to_module_name(path.last)) @@ -150,7 +154,7 @@ module Dependencies result = File.join(fs_path, const_name_to_file_name(path.last)) - return File.file?(result) ? result : nil + File.file?(result) ? result : nil end def const_name_to_file_name(name) @@ -164,8 +168,8 @@ module Dependencies end Object.send(:define_method, :require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) -Object.send(:define_method, :require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency) -Object.send(:define_method, :require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association) +Object.send(:define_method, :require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency) +Object.send(:define_method, :require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association) class Module #:nodoc: # Use const_missing to autoload associations so we don't have to @@ -186,19 +190,17 @@ end class Object #:nodoc: def load(file, *extras) - begin super(file, *extras) - rescue Object => exception - exception.blame_file! file - raise - end + super(file, *extras) + rescue Object => exception + exception.blame_file! file + raise end def require(file, *extras) - begin super(file, *extras) - rescue Object => exception - exception.blame_file! file - raise - end + super(file, *extras) + rescue Object => exception + exception.blame_file! file + raise end end -- cgit v1.2.3