Class: Viking::Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/viking/dir.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Dir

Returns a new instance of Dir.



92
93
94
95
# File 'lib/viking/dir.rb', line 92

def initialize(path)
  @path   = path
  @closed = false
end

Class Method Details

.chdir(dirname = home, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/viking/dir.rb', line 8

def self.chdir(dirname=home, &block)
  if block
    main_dirname = pwd
    Viking.client.set_working_directory(Path.new(dirname))

    result = yield dirname

    Viking.client.set_working_directory(Path.new(main_dirname))
    result
  else
    Viking.client.set_working_directory(Path.new(dirname))
    0
  end
end

.delete(dirname) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/viking/dir.rb', line 23

def self.delete(dirname)
  if exist?(dirname)
    Viking.client.delete(Path.new(dirname), false)
    0
  else
    raise Errno::ENOENT.new("No such file or directory - No such directory: #{dirname}")
  end
end

.entries(dirname) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/viking/dir.rb', line 32

def self.entries(dirname)
  if exist?(dirname)
    iterator = Viking.client.list_located_status(Path.new(dirname))
    entries  = []

    while(iterator.has_next) do
      entry = format(iterator.next.path)
      entries << Viking::File.basename(entry)
    end

    entries
  else
    raise Errno::ENOENT.new("No such file or directory - No such directory: #{dirname}")
  end
end

.exist?(dirname) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/viking/dir.rb', line 48

def self.exist?(dirname)
  Viking::File.exist?(dirname) && Viking::File.directory?(dirname)
end

.exists?(dirname) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/viking/dir.rb', line 52

def self.exists?(dirname)
  exist?(dirname)
end

.foreach(dirname) ⇒ Object



56
57
58
# File 'lib/viking/dir.rb', line 56

def self.foreach(dirname)
  entries(dirname).each { |entry| yield entry }
end

.format(dirname) ⇒ Object



4
5
6
# File 'lib/viking/dir.rb', line 4

def self.format(dirname)
  dirname.to_s.sub(/^[^:]*:\/*\//, '/')
end

.getwdObject



60
61
62
# File 'lib/viking/dir.rb', line 60

def self.getwd
  format(Viking.client.get_working_directory)
end

.homeObject



64
65
66
# File 'lib/viking/dir.rb', line 64

def self.home
  format(Viking.client.get_home_directory)
end

.mkdir(dirname) ⇒ Object



68
69
70
71
# File 'lib/viking/dir.rb', line 68

def self.mkdir(dirname)
  Viking.client.mkdirs(Path.new(dirname))
  0
end

.open(dirname, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/viking/dir.rb', line 73

def self.open(dirname, &block)
  dir = Viking::Dir.new(dirname)
  if block
    value = yield dir
    dir.close
    value
  else
    dir
  end
end

.pwdObject



84
85
86
# File 'lib/viking/dir.rb', line 84

def self.pwd
  getwd
end

.rmdir(dirname) ⇒ Object



88
89
90
# File 'lib/viking/dir.rb', line 88

def self.rmdir(dirname)
  delete(dirname)
end

Instance Method Details

#closeObject



97
98
99
100
# File 'lib/viking/dir.rb', line 97

def close
  check_closed
  @closed = true
end

#eachObject



102
103
104
105
106
107
# File 'lib/viking/dir.rb', line 102

def each
  check_closed
  entries.each do |file_name|
    yield file_name
  end
end

#pathObject



109
110
111
# File 'lib/viking/dir.rb', line 109

def path
  @path
end

#readObject



113
114
115
116
117
118
119
# File 'lib/viking/dir.rb', line 113

def read
  check_closed
  rewind if @entries.nil?
  entry = @entries[@pos]
  @pos += 1 unless entry.nil?
  entry
end

#rewindObject



121
122
123
124
125
# File 'lib/viking/dir.rb', line 121

def rewind
  check_closed
  @pos     = 0
  @entries = entries
end