aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/active_support_overview.textile
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2009-07-26 01:44:48 +0200
committerXavier Noria <fxn@hashref.com>2009-07-26 01:44:48 +0200
commitc783b48a8d71eab34f2b4a120fbeb35361714647 (patch)
tree37686d7fd75c8448819e32722b189a9c6edf53b3 /railties/guides/source/active_support_overview.textile
parente6b89126db723b7f29bc067917f9bb0b3145d9e4 (diff)
downloadrails-c783b48a8d71eab34f2b4a120fbeb35361714647.tar.gz
rails-c783b48a8d71eab34f2b4a120fbeb35361714647.tar.bz2
rails-c783b48a8d71eab34f2b4a120fbeb35361714647.zip
AS guide: explains File.atomic_write
Diffstat (limited to 'railties/guides/source/active_support_overview.textile')
-rw-r--r--railties/guides/source/active_support_overview.textile20
1 files changed, 19 insertions, 1 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile
index aea77c8d4e..93c189ddb6 100644
--- a/railties/guides/source/active_support_overview.textile
+++ b/railties/guides/source/active_support_overview.textile
@@ -789,7 +789,25 @@ h3. Extensions to +Pathname+
h3. Extensions to +File+
-...
+h4. +atomic_write+
+
+With the class method +File.atomic_write+ you can write to a file in a way that will prevent any reader from seeing half-written content.
+
+The name of the file is passed as an argument, and the method yields a file handle opened for writing. Once the block is done +atomic_write+ closes the file handle and completes its job.
+
+For example, Action Pack uses this method to write asset cache files like +all.css+:
+
+<ruby>
+File.atomic_write(joined_asset_path) do |cache|
+ cache.write(join_asset_file_contents(asset_paths))
+end
+</ruby>
+
+To accomplish this +atomic_write+ creates a temporary file. That's the file the code in the block actually writes to. On completion, the temporary file is renamed. If the target file exists +atomic_write+ overwrites it and keeps owners and permissions.
+
+WARNING. Note you can't append with +atomic_write+.
+
+The auxiliary file is written in a standard directory for temporary files, but you can pass a directory of your choice as second argument.
h3. Extensions to +Exception+