Class: SkadateGems::Dev::Remote::Directory

Inherits:
File
  • Object
show all
Defined in:
lib/skadategems/dev/remote/directory.rb

Overview

Represents a remote directory.

Instance Attribute Summary collapse

Attributes inherited from File

#path, #size

Instance Method Summary collapse

Methods inherited from File

#basename, #friendly_size, #initialize, #request, #save_as

Constructor Details

This class inherits a constructor from SkadateGems::Dev::Remote::File

Instance Attribute Details

#list_hiddenObject



13
14
15
# File 'lib/skadategems/dev/remote/directory.rb', line 13

def list_hidden
  @list_hidden
end

Instance Method Details

#directory?boolean

Returns always true.

Returns:

  • (boolean)

    always true



16
17
18
# File 'lib/skadategems/dev/remote/directory.rb', line 16

def directory?
  true
end

#extnameString

Returns always ‘/’.

Returns:

  • (String)

    always ‘/’



21
22
23
# File 'lib/skadategems/dev/remote/directory.rb', line 21

def extname
  '/'
end

#listArray < Directory | File >

List remote directory files.

Returns:

  • (Array < Directory | File >)

    where each entry may be either a ‘Remote::Directory` or a `Remote::File` accessor object.



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/skadategems/dev/remote/directory.rb', line 28

def list
  path = self.path.dup
  path[0] = '' if path[0] == '/'
  path[-1] = '' if path[-1] == '/'

  response = @remote.exec do |batch|
    batch << <<-PHPSCRIPT
$dir = dir(DIR_SITE_ROOT . '#{path.gsub(/['\\]/, '\\\\\0')}');

$list = array();
while (false !== ($file = $dir->read())) {
  if (#{if list_hidden
  '$file === \'.\' || $file === \'..\''
else
  '$file{0} === \'.\' && $file !== \'.htaccess\''
end}) {
    continue;
  }

  $path = "$dir->path/$file";

  if ($path === $_SERVER['SCRIPT_FILENAME']) {
    continue;
  }

  if (is_file($path)) {
    $type = 'f';
  } elseif (is_dir($path)) {
    $type = 'd';
  } else {
    continue;
  }

  $list[] = array($file, $type, filesize($path));
}

echo json_encode($list);
    PHPSCRIPT
  end

  result = []

  JSON.parse(response.body).each do |entry|
    name, type, size = entry
    _path = ::File.join(self.path, name)

    result << if type == 'd'
      Directory.new(@remote, _path, size)
    else
      File.new(@remote, _path, size)
    end
  end

  result.sort! { |a, b| a.basename <=> b.basename }
end