Class: File

Inherits:
IO show all
Defined in:
opal/opal/corelib/file.rb

Constant Summary

Separator =
SEPARATOR = '/'
ALT_SEPARATOR =
nil
PATH_SEPARATOR =
':'
FNM_SYSCASE =

Assuming case insenstive filesystem

0

Constants inherited from IO

IO::SEEK_CUR, IO::SEEK_END, IO::SEEK_SET

Instance Attribute Summary

Attributes inherited from IO

#sync, #tty, #write_proc

Class Method Summary collapse

Methods inherited from IO

#closed?, #flush, #tty?, #write

Class Method Details

.basename(name, suffix = nil) ⇒ Object


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'opal/opal/corelib/file.rb', line 161

def basename(name, suffix=nil)
  %x{
    var p, q, e, f = 0, n = -1, tmp, pointerMath, origName;

    if (name === nil) {
      #{raise TypeError, 'no implicit conversion of nil into String'}
    }
    if (#{name.respond_to?(:to_path)}) {
      name = #{name.to_path};
    }
    if (!name.$$is_string) {
      #{raise TypeError, "no implicit conversion of #{name.class} into String"}
    }
    if (suffix !== nil && !suffix.$$is_string) {
      #{raise TypeError, "no implicit conversion of #{suffix.class} into String"}
    }

    if (name.length == 0) {
      return name;
    }

    origName = name;
    name = skipprefix(name);

    while (isDirSep(name)) {
      tmp = name;
      name = inc(name);
    }

    if (!name) {
      p = tmp;
      f = 1;
    }
    else {
      if (!(p = lastSeparator(name))) {
        p = name;
      }
      else {
        while (isDirSep(p)) {
          p = inc(p);
        }
      }

      n = pointerSubtract(chompdirsep(p), p);

      for (q = p; pointerSubtract(q, p) < n && q.charAt(0) === '.'; q = inc(q)) {
      }

      for (e = null; pointerSubtract(q, p) < n; q = inc(q)) {
        if (q.charAt(0) === '.') {
          e = q;
        }
      }

      if (e) {
        f = pointerSubtract(e, p);
      }
      else {
        f = n;
      }
    }

    return handleSuffix(n, f, p, suffix, name, origName);
  }
end

.directory?(path) ⇒ Boolean

Returns:


243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'opal/opal/corelib/file.rb', line 243

def directory?(path)
  files = []
  %x{
    for (var key in Opal.modules) {
      #{files}.push(key)
    }
  }
  path = path.gsub(%r{(^.#{SEPARATOR}+|#{SEPARATOR}+$)})
  file = files.find do |file|
    file =~ /^#{path}/
  end
  file
end

.dirname(path) ⇒ Object


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'opal/opal/corelib/file.rb', line 126

def dirname(path)
  %x{
    if (path === nil) {
      #{raise TypeError, 'no implicit conversion of nil into String'}
    }
    if (#{path.respond_to?(:to_path)}) {
      path = #{path.to_path};
    }
    if (!path.$$is_string) {
      #{raise TypeError, "no implicit conversion of #{path.class} into String"}
    }

    var root, p;

    root = skipRoot(path);

    // if (root > name + 1) in the C code
    if (root.length == 0) {
      path = path.substring(path.length - 1, path.length);
    }
    else if (root.length - path.length < 0) {
      path = path.substring(path.indexOf(root)-1, path.length);
    }

    p = lastSeparator(root);
    if (!p) {
      p = root;
    }
    if (p === path) {
      return '.';
    }
    return path.substring(0, path.length - p.length);
  }
end

.exist?(path) ⇒ Boolean Also known as: exists?

Returns:


238
239
240
# File 'opal/opal/corelib/file.rb', line 238

def exist? path
  `Opal.modules[#{path}] != null`
end

.expand_path(path, basedir = nil) ⇒ Object Also known as: realpath


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'opal/opal/corelib/file.rb', line 9

def expand_path(path, basedir = nil)
  path = [basedir, path].compact.join(SEPARATOR)
  parts = path.split(SEPARATOR)
  new_parts = []
  parts[0] = Dir.home if parts.first == '~'
  parts[0] = Dir.pwd if parts.first == '.'

  parts.each do |part|
    if part == '..'
      new_parts.pop
    else
      new_parts << part
    end
  end
  new_parts.join(SEPARATOR)
end

.extname(path) ⇒ Object

Raises:


227
228
229
230
231
232
233
234
235
236
# File 'opal/opal/corelib/file.rb', line 227

def extname(path)
  raise TypeError, 'no implicit conversion of nil into String' if path.nil?
  path = path.to_path if path.respond_to?(:to_path)
  raise TypeError, "no implicit conversion of #{path.class} into String" unless path.is_a?(String)
  filename = basename(path)
  return '' if filename.empty?
  last_dot_idx = filename[1..-1].rindex('.')
  # extension name must contains at least one character .(something)
  (last_dot_idx.nil? || last_dot_idx + 1 == filename.length - 1) ? '' : filename[(last_dot_idx + 1)..-1]
end

.join(*paths) ⇒ Object


257
258
259
# File 'opal/opal/corelib/file.rb', line 257

def join(*paths)
  paths.join(SEPARATOR).gsub(%r{#{SEPARATOR}+}, SEPARATOR)
end

.split(path) ⇒ Object


261
262
263
# File 'opal/opal/corelib/file.rb', line 261

def split(path)
  path.split(SEPARATOR)
end