Class: File
  
  
  
  
  
    - Inherits:
- 
      IO
      
        
        show all
      
    
    - Defined in:
- opal/opal/corelib/file.rb
 
  
    
      Constant Summary
      collapse
    
    
      
        - 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
    
      
  
  
    .absolute_path(path, basedir = nil)  ⇒ Object 
  
  
  
  
    | 
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 | # File 'opal/opal/corelib/file.rb', line 10
def absolute_path(path, basedir = nil)
  sep = SEPARATOR
  sep_chars = `$sep_chars()`
  new_parts = []
  path = path.respond_to?(:to_path) ? path.to_path : path
  basedir ||= Dir.pwd
  path_abs    = `path.substr(0, sep.length) === sep || windows_root_rx.test(path)`
  basedir_abs = `basedir.substr(0, sep.length) === sep || windows_root_rx.test(basedir)`
  if path_abs
    parts       = path.split(/[#{sep_chars}]/)
    leading_sep = `windows_root_rx.test(path) ? '' : #{path.sub(/^([#{sep_chars}]+).*$/, '\1')}`
    abs         = true
  else
    parts       = basedir.split(/[#{sep_chars}]/) + path.split(/[#{sep_chars}]/)
    leading_sep = `windows_root_rx.test(basedir) ? '' : #{basedir.sub(/^([#{sep_chars}]+).*$/, '\1')}`
    abs         = basedir_abs
  end
  %x{
    var part;
    for (var i = 0, ii = parts.length; i < ii; i++) {
      part = parts[i];
      if (
        (part === nil) ||
        (part === ''  && ((new_parts.length === 0) || abs)) ||
        (part === '.' && ((new_parts.length === 0) || abs))
      ) {
        continue;
      }
      if (part === '..') {
        new_parts.pop();
      } else {
        new_parts.push(part);
      }
    }
    if (!abs && parts[0] !== '.') {
      #{new_parts.unshift '.'}
    }
  }
  new_path = new_parts.join(sep)
  new_path = leading_sep + new_path if abs
  new_path
end | 
 
    
      
  
  
    .basename(name, suffix = nil)  ⇒ Object 
  
  
  
  
    | 
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 | # File 'opal/opal/corelib/file.rb', line 117
def basename(name, suffix = nil)
  sep_chars = `$sep_chars()`
  name = `$coerce_to_path(name)`
  %x{
    if (name.length == 0) {
      return name;
    }
    if (suffix !== nil) {
      suffix = #{Opal.coerce_to!(suffix, String, :to_str)}
    } else {
      suffix = null;
    }
    name = name.replace(new RegExp(#{"(.)[#{sep_chars}]*$"}), '$1');
    name = name.replace(new RegExp(#{"^(?:.*[#{sep_chars}])?([^#{sep_chars}]+)$"}), '$1');
    if (suffix === ".*") {
      name = name.replace(/\.[^\.]+$/, '');
    } else if(suffix !== null) {
      suffix = Opal.escape_regexp(suffix);
      name = name.replace(new RegExp(#{"#{suffix}$"}), '');
    }
    return name;
  }
end | 
 
    
      
  
  
    .directory?(path)  ⇒ Boolean 
  
  
  
  
    | 
159
160
161
162
163
164
165
166
167
168
169 | # File 'opal/opal/corelib/file.rb', line 159
def directory?(path)
  files = []
  %x{
    for (var key in Opal.modules) {
      #{files}.push(key)
    }
  }
  path = path.gsub(/(^.#{SEPARATOR}+|#{SEPARATOR}+$)/)
  file = files.find { |f| f =~ /^#{path}/ }
  file
end | 
 
    
      
  
  
    .dirname(path)  ⇒ Object 
  
  
  
  
    | 
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 | # File 'opal/opal/corelib/file.rb', line 99
def dirname(path)
  sep_chars = `$sep_chars()`
  path = `$coerce_to_path(path)`
  %x{
    var absolute = path.match(new RegExp(#{"^[#{sep_chars}]"}));
    path = path.replace(new RegExp(#{"[#{sep_chars}]+$"}), ''); // remove trailing separators
    path = path.replace(new RegExp(#{"[^#{sep_chars}]+$"}), ''); // remove trailing basename
    path = path.replace(new RegExp(#{"[#{sep_chars}]+$"}), ''); // remove final trailing separators
    if (path === '') {
      return absolute ? '/' : '.';
    }
    return path;
  }
end | 
 
    
      
  
  
    .exist?(path)  ⇒ Boolean 
  
  
    Also known as:
    exists?
    
  
  
  
    | 
154
155
156 | # File 'opal/opal/corelib/file.rb', line 154
def exist?(path)
  `Opal.modules[#{path}] != null`
end | 
 
    
      
  
  
    .expand_path(path, basedir = nil)  ⇒ Object 
  
  
    Also known as:
    realpath
    
  
  
  
    | 
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 | # File 'opal/opal/corelib/file.rb', line 59
def expand_path(path, basedir = nil)
  sep = SEPARATOR
  sep_chars = `$sep_chars()`
  if `path[0] === '~' || (basedir && basedir[0] === '~')`
    home = Dir.home
    raise(ArgumentError, "couldn't find HOME environment -- expanding `~'") unless home
    leading_sep = `windows_root_rx.test(home) ? '' : #{home.sub(/^([#{sep_chars}]+).*$/, '\1')}`
    raise(ArgumentError, 'non-absolute home') unless home.start_with?(leading_sep)
    home            += sep
    home_path_regexp = /^\~(?:#{sep}|$)/
    path             = path.sub(home_path_regexp, home)
    basedir          = basedir.sub(home_path_regexp, home) if basedir
  end
  absolute_path(path, basedir)
end | 
 
    
      
  
  
    .extname(path)  ⇒ Object 
  
  
  
  
    | 
145
146
147
148
149
150
151
152 | # File 'opal/opal/corelib/file.rb', line 145
def extname(path)
  `path = $coerce_to_path(path)`
  filename = basename(path)
  return '' if filename.empty?
  last_dot_idx = filename[1..-1].rindex('.')
    last_dot_idx.nil? || last_dot_idx + 1 == filename.length - 1 ? '' : filename[(last_dot_idx + 1)..-1]
end | 
 
    
      
  
  
    .join(*paths)  ⇒ Object 
  
  
  
  
    | 
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 | # File 'opal/opal/corelib/file.rb', line 171
def join(*paths)
  if paths.empty?
    return ''
  end
  result = ''
  paths = paths.flatten.each_with_index.map do |item, index|
    if index == 0 && item.empty?
      SEPARATOR
    elsif paths.length == index + 1 && item.empty?
      SEPARATOR
    else
      item
    end
  end
  paths = paths.reject(&:empty?)
  paths.each_with_index do |item, index|
    next_item = paths[index + 1]
    if next_item.nil?
      result = "#{result}#{item}"
    else
      if item.end_with?(SEPARATOR) && next_item.start_with?(SEPARATOR)
        item = item.sub(/#{SEPARATOR}+$/, '')
      end
      result = if item.end_with?(SEPARATOR) || next_item.start_with?(SEPARATOR)
                 "#{result}#{item}"
               else
                 "#{result}#{item}#{SEPARATOR}"
               end
    end
  end
  result
end | 
 
    
      
  
  
    .split(path)  ⇒ Object 
  
  
  
  
    | 
204
205
206 | # File 'opal/opal/corelib/file.rb', line 204
def split(path)
  path.split(SEPARATOR)
end |