Class: Gemirro::MirrorDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/gemirro/mirror_directory.rb

Overview

The MirrorDirectory is used for dealing with files and directories that are mirrored from an external source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MirrorDirectory

Returns a new instance of MirrorDirectory.

Parameters:

  • path (String)


16
17
18
# File 'lib/gemirro/mirror_directory.rb', line 16

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

Returns:

  • (String)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gemirro/mirror_directory.rb', line 10

class MirrorDirectory
  attr_reader :path

  ##
  # @param [String] path
  #
  def initialize(path)
    @path = path
  end

  ##
  # Creates directory or directories with the given path.
  #
  # @param [String] dir_path
  # @return [Gemirro::MirrorDirectory]
  #
  def add_directory(dir_path)
    full_path = File.join(@path, dir_path)
    FileUtils.mkdir_p(full_path) unless File.directory?(full_path)

    self.class.new(full_path)
  end

  ##
  # Creates a new file with the given name and content.
  #
  # @param [String] name
  # @param [String] content
  # @return [Gem::MirrorFile]
  #
  def add_file(name, content)
    full_path = File.join(@path, name)
    file      = MirrorFile.new(full_path)

    file.write(content)

    file
  end

  ##
  # Checks if a given file exists in the current directory.
  #
  # @param [String] name
  # @return [TrueClass|FalseClass]
  #
  def file_exists?(name)
    File.file?(File.join(@path, name))
  end
end

Instance Method Details

#add_directory(dir_path) ⇒ Gemirro::MirrorDirectory

Creates directory or directories with the given path.

Parameters:

  • dir_path (String)

Returns:



26
27
28
29
30
31
# File 'lib/gemirro/mirror_directory.rb', line 26

def add_directory(dir_path)
  full_path = File.join(@path, dir_path)
  FileUtils.mkdir_p(full_path) unless File.directory?(full_path)

  self.class.new(full_path)
end

#add_file(name, content) ⇒ Gem::MirrorFile

Creates a new file with the given name and content.

Parameters:

  • name (String)
  • content (String)

Returns:

  • (Gem::MirrorFile)


40
41
42
43
44
45
46
47
# File 'lib/gemirro/mirror_directory.rb', line 40

def add_file(name, content)
  full_path = File.join(@path, name)
  file      = MirrorFile.new(full_path)

  file.write(content)

  file
end

#file_exists?(name) ⇒ TrueClass|FalseClass

Checks if a given file exists in the current directory.

Parameters:

  • name (String)

Returns:

  • (TrueClass|FalseClass)


55
56
57
# File 'lib/gemirro/mirror_directory.rb', line 55

def file_exists?(name)
  File.file?(File.join(@path, name))
end