Class: Amp::Opener

Inherits:
Object show all
Defined in:
lib/amp/support/openers.rb

Overview

opens files

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • base (String)

    the root directory of the repository this opener will be used on



19
20
21
22
23
# File 'lib/amp/support/openers.rb', line 19

def initialize(base)
  @root        = File.expand_path base
  @create_mode = nil
  @default     = nil
end

Instance Attribute Details

#create_modeObject

Returns the value of attribute create_mode.



7
8
9
# File 'lib/amp/support/openers.rb', line 7

def create_mode
  @create_mode
end

#defaultObject

Returns the value of attribute default.



8
9
10
# File 'lib/amp/support/openers.rb', line 8

def default
  @default
end

#rootObject (readonly) Also known as: base

Returns the value of attribute root.



5
6
7
# File 'lib/amp/support/openers.rb', line 5

def root
  @root
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.

Parameters:

  • file (String)

    the path to the file to open

  • mode (String) (defaults to: 'r')

    the read/write mode to open with (standard C choices here)

Yields:

  • Can yield the opened file if the block form is used



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!!!

Parameters:

  • file (String)

    the file to open

  • mode (String) (defaults to: 'w')

    the mode with which to open the file (“w”, “r”, “a”, …)

Yields:

  • (file)

    code to run on the file

Yield Parameters:

  • file (File)

    the opened 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.

Parameters:

  • file (String)

    the file to open

  • mode (String) (defaults to: 'w')

    the mode with which to open the file (“w”, “r”, “a”, …)

Yields:

  • (file)

    code to run on the file

Yield Parameters:

  • file (File)

    the opened file



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.

Parameters:

  • dir (String)

    This dir is where the temp file is made, but ALSO the parent dir of file

  • file (String)

    Just the file name. It must exist at “#dir/#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

#pathObject

Returns the path to the opener’s root.

Returns:

  • path to the opener’s root, as an absolute path.



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)

Parameters:

  • file (String)

    the relative path to the file we’re opening

Returns:

  • (String)

    the contents of the 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