Class: Amp::Opener
Overview
opens files
Direct Known Subclasses
Repositories::Stores::EncodedOpener, Repositories::Stores::FilenameCache::FilenameCacheOpener
Instance Attribute Summary collapse
-
#create_mode ⇒ Object
Returns the value of attribute create_mode.
-
#default ⇒ Object
Returns the value of attribute default.
-
#root ⇒ Object
(also: #base)
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#initialize(base) ⇒ Opener
constructor
Creates a new opener with a root of
base
, and also set to open files in the .hg subdirectory. - #join(file) ⇒ Object
-
#open(file, mode = 'r') { ... } ⇒ Object
Opens up the given file, exactly like you would do with File.open.
-
#open_file(file, mode = 'w') {|file| ... } ⇒ Object
Opens a file in the repository (not in .hg).
-
#open_hg(file, mode = 'w') {|file| ... } ⇒ Object
Opens a file in the .hg repository using @root.
-
#open_up_file(dir, file, mode, &block) ⇒ Object
This does the actual opening of a file.
-
#path ⇒ Object
Returns the path to the opener’s root.
-
#read(file) ⇒ String
(also: #contents)
Read the file passed in with mode ‘r’.
Constructor Details
#initialize(base) ⇒ Opener
Creates a new opener with a root of base
, and also set to open files in the .hg subdirectory. If you set .default = :open_file, it will no longer open files in the .hg subdir.
19 20 21 22 23 |
# File 'lib/amp/support/openers.rb', line 19 def initialize(base) @root = File. base @create_mode = nil @default = nil end |
Instance Attribute Details
#create_mode ⇒ Object
Returns the value of attribute create_mode.
7 8 9 |
# File 'lib/amp/support/openers.rb', line 7 def create_mode @create_mode end |
#default ⇒ Object
Returns the value of attribute default.
8 9 10 |
# File 'lib/amp/support/openers.rb', line 8 def default @default end |
Instance Method Details
#join(file) ⇒ Object
72 73 74 |
# File 'lib/amp/support/openers.rb', line 72 def join(file) File.join(root, file) end |
#open(file, mode = 'r') { ... } ⇒ Object
Opens up the given file, exactly like you would do with File.open. The parameters are the same. Defaults to opening a file in the .hg/ folder, but if @default == :open_file, will open it from the working directory.
If the mode includes write privileges, then the write will use an atomic temporary file.
64 65 66 67 68 69 70 |
# File 'lib/amp/support/openers.rb', line 64 def open(file, mode='r', &block) if @default == :open_file open_file file, mode, &block else open_hg file, mode, &block end end |
#open_file(file, mode = 'w') {|file| ... } ⇒ Object
Opens a file in the repository (not in .hg). Writes are done atomically, and reads are efficiently done with Kernel::open. THIS IS NOT open_up_file
!!!
101 102 103 |
# File 'lib/amp/support/openers.rb', line 101 def open_file(file, mode='w', &block) open_up_file root, file, mode, &block end |
#open_hg(file, mode = 'w') {|file| ... } ⇒ Object
Opens a file in the .hg repository using @root. This method operates atomically, and ensures that the file is always closed after use. The temporary files (while being atomically written) are stored in “#@root/.hg”, and are deleted after use. If only a read is being done, it instead uses Kernel::open instead of File::amp_atomic_write.
88 89 90 |
# File 'lib/amp/support/openers.rb', line 88 def open_hg(file, mode='w', &block) open_up_file File.join(root, ".hg"), file, mode, &block end |
#open_up_file(dir, file, mode, &block) ⇒ Object
This does the actual opening of a file.
111 112 113 114 115 116 117 118 |
# File 'lib/amp/support/openers.rb', line 111 def open_up_file(dir, file, mode, &block) path = File.join dir, file if mode == 'r' # if we're doing a read, make this super snappy Kernel::open path, mode, &block else # we're doing a write File::amp_atomic_write path, mode, @create_mode, dir, &block end end |
#path ⇒ Object
Returns the path to the opener’s root.
29 30 31 32 33 34 35 |
# File 'lib/amp/support/openers.rb', line 29 def path if @default == :open_file "#{root}/" else "#{root}/.hg/" end end |
#read(file) ⇒ String Also known as: contents
Read the file passed in with mode ‘r’. Synonymous with File.open(file
, ‘r’) {|f| f.read } and File.read(file
)
44 45 46 47 48 |
# File 'lib/amp/support/openers.rb', line 44 def read(file) res = nil open(file, 'r') {|f| res = f.read } res end |