Class: Ace::Support::Models::Atoms::FileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/models/atoms/file_writer.rb

Overview

Writes files to the cache

Class Method Summary collapse

Class Method Details

.delete(path) ⇒ Boolean

Delete file

Parameters:

  • path (String)

    File path

Returns:

  • (Boolean)

    true on success



40
41
42
43
44
45
# File 'lib/ace/support/models/atoms/file_writer.rb', line 40

def delete(path)
  File.delete(path) if File.exist?(path)
  true
rescue Errno::EACCES => e
  raise CacheError, "Permission denied deleting #{path}: #{e.message}"
end

.ensure_directory(dir) ⇒ Boolean

Ensure directory exists

Parameters:

  • dir (String)

    Directory path

Returns:

  • (Boolean)

    true if created or exists



30
31
32
33
34
35
# File 'lib/ace/support/models/atoms/file_writer.rb', line 30

def ensure_directory(dir)
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  true
rescue Errno::EACCES => e
  raise CacheError, "Permission denied creating directory #{dir}: #{e.message}"
end

.rename(from, to) ⇒ Boolean

Rename/move file

Parameters:

  • from (String)

    Source path

  • to (String)

    Destination path

Returns:

  • (Boolean)

    true on success



51
52
53
54
55
56
57
# File 'lib/ace/support/models/atoms/file_writer.rb', line 51

def rename(from, to)
  ensure_directory(File.dirname(to))
  File.rename(from, to)
  true
rescue Errno::EACCES => e
  raise CacheError, "Permission denied moving file: #{e.message}"
end

.write(path, content) ⇒ Boolean

Write content to file

Parameters:

  • path (String)

    File path

  • content (String)

    Content to write

Returns:

  • (Boolean)

    true on success

Raises:



17
18
19
20
21
22
23
24
25
# File 'lib/ace/support/models/atoms/file_writer.rb', line 17

def write(path, content)
  ensure_directory(File.dirname(path))
  File.write(path, content)
  true
rescue Errno::EACCES => e
  raise CacheError, "Permission denied writing #{path}: #{e.message}"
rescue Errno::ENOSPC => e
  raise CacheError, "No space left writing #{path}: #{e.message}"
end