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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'opal/opal/corelib/file.rb', line 110
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
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'opal/opal/corelib/file.rb', line 152
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'opal/opal/corelib/file.rb', line 92
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?
147
148
149
|
# File 'opal/opal/corelib/file.rb', line 147
def exist? path
`Opal.modules[#{path}] != null`
end
|
.expand_path(path, basedir = nil) ⇒ Object
Also known as:
realpath
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
59
60
61
62
63
64
65
66
67
|
# File 'opal/opal/corelib/file.rb', line 10
def expand_path(path, basedir = nil)
sep = SEPARATOR
sep_chars = `$sep_chars()`
new_parts = []
if `path[0] === '~' || (basedir && basedir[0] === '~')`
home = Dir.home
raise(ArgumentError, "couldn't find HOME environment -- expanding `~'") unless home
raise(ArgumentError, "non-absolute home") unless home.start_with?(sep)
home += sep
home_path_regexp = /^\~(?:#{sep}|$)/
path = path.sub(home_path_regexp, home)
basedir = basedir.sub(home_path_regexp, home) if basedir
end
basedir = Dir.pwd unless basedir
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
|
.extname(path) ⇒ Object
138
139
140
141
142
143
144
145
|
# File 'opal/opal/corelib/file.rb', line 138
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
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
|
# File 'opal/opal/corelib/file.rb', line 166
def join(*paths)
if paths.length == 0
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 { |path| path.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(%r{#{SEPARATOR}+$}, '')
end
if item.end_with?(SEPARATOR) || next_item.start_with?(SEPARATOR)
result = "#{result}#{item}"
else
result = "#{result}#{item}#{SEPARATOR}"
end
end
end
result
end
|
.split(path) ⇒ Object
199
200
201
|
# File 'opal/opal/corelib/file.rb', line 199
def split(path)
path.split(SEPARATOR)
end
|