Class: Amp::Core::Support::RootedOpener
- Inherits:
-
Object
- Object
- Amp::Core::Support::RootedOpener
- Defined in:
- lib/amp-core/support/rooted_opener.rb
Overview
This creates an opener that prepends a directory to all File opening, allowing repository formats to worry only about file paths relative to the repository root.
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) ⇒ RootedOpener
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) ⇒ RootedOpener
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.
35 36 37 38 39 |
# File 'lib/amp-core/support/rooted_opener.rb', line 35 def initialize(base) @root = File. base @create_mode = nil @default = :open_hg end |
Instance Attribute Details
#create_mode ⇒ Object
Returns the value of attribute create_mode.
23 24 25 |
# File 'lib/amp-core/support/rooted_opener.rb', line 23 def create_mode @create_mode end |
#default ⇒ Object
Returns the value of attribute default.
24 25 26 |
# File 'lib/amp-core/support/rooted_opener.rb', line 24 def default @default end |
#root ⇒ Object (readonly) Also known as: base
Returns the value of attribute root.
22 23 24 |
# File 'lib/amp-core/support/rooted_opener.rb', line 22 def root @root end |
Instance Method Details
#join(file) ⇒ Object
93 94 95 |
# File 'lib/amp-core/support/rooted_opener.rb', line 93 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.
85 86 87 88 89 90 91 |
# File 'lib/amp-core/support/rooted_opener.rb', line 85 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
!!!
122 123 124 |
# File 'lib/amp-core/support/rooted_opener.rb', line 122 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.
109 110 111 |
# File 'lib/amp-core/support/rooted_opener.rb', line 109 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.
132 133 134 135 136 137 138 139 |
# File 'lib/amp-core/support/rooted_opener.rb', line 132 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.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/amp-core/support/rooted_opener.rb', line 45 def path case @default when :open_file "#{root}/" when :open_hg "#{root}/.hg/" when :open_git "#{root}/.git" else raise abort "Unknown opener format #{@default.inspect}" 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
)
65 66 67 68 69 |
# File 'lib/amp-core/support/rooted_opener.rb', line 65 def read(file) res = nil open(file, 'r') {|f| res = f.read } res end |