aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/active_support_overview.textile
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2009-08-05 23:23:23 +0200
committerXavier Noria <fxn@hashref.com>2009-08-05 23:54:01 +0200
commit54b09fce0f7e4043875584fa9c68ed415d6c6e5a (patch)
tree08ff6b5658708008f57158d63402a6931b61959e /railties/guides/source/active_support_overview.textile
parent96780d52f667c81d5055913ef97bb4fefe6e2860 (diff)
downloadrails-54b09fce0f7e4043875584fa9c68ed415d6c6e5a.tar.gz
rails-54b09fce0f7e4043875584fa9c68ed415d6c6e5a.tar.bz2
rails-54b09fce0f7e4043875584fa9c68ed415d6c6e5a.zip
AS guide: documents extensions to LoadError
Diffstat (limited to 'railties/guides/source/active_support_overview.textile')
-rw-r--r--railties/guides/source/active_support_overview.textile26
1 files changed, 26 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile
index ba1742d238..3296cc7f4e 100644
--- a/railties/guides/source/active_support_overview.textile
+++ b/railties/guides/source/active_support_overview.textile
@@ -856,7 +856,33 @@ h3. Extensions to +NameError+
h3. Extensions to +LoadError+
+Rails hijacks +LoadError.new+ to return a +MissingSourceFile+ exception:
+
+<shell>
+$ ruby -e 'require "nonexistent"'
+...: no such file to load -- nonexistent (LoadError)
+...
+$ script/runner 'require "nonexistent"'
+...: no such file to load -- nonexistent (MissingSourceFile)
...
+</shell>
+
+The class +MissingSourceFile+ is a subclass of +LoadError+, so any code that rescues +LoadError+ as usual still works as expected. Point is these exception objects respond to +is_missing?+, which given a path name tests whether the exception was raised due to that particular file (except perhaps for the ".rb" extension).
+
+For example, when an action of +PostsController+ is called Rails tries to load +posts_helper.rb+, but that file may not exist. That's fine, the helper module is not mandatory so Rails silences a load error. But it could be the case that the helper module does exist, but it in turn requires another library that is missing. In that case Rails must reraise the exception. The method +is_missing?+ provides a way to distinguish both cases:
+
+<ruby>
+def inherited_with_helper(child)
+ inherited_without_helper(child)
+ begin
+ child.master_helper_module = Module.new
+ child.master_helper_module.__send__(:include, master_helper_module)
+ child.helper child.name.to_s.underscore
+ rescue MissingSourceFile => e
+ raise unless e.is_missing?("#{child.name.to_s.underscore}_helper")
+ end
+end
+</ruby>
h3. Extensions to +CGI+