[フレーム]

Class: File

Inherits:
IO show all
Includes:
IO::Readable, IO::Writable
Defined in:
opal/stdlib/nodejs/file.rb,
opal/stdlib/nashorn/file.rb

Defined Under Namespace

Classes: Stat

Constant Summary

ALT_SEPARATOR =
`__path__.sep`

Instance Attribute Summary collapse

Attributes inherited from IO

#eof , #lineno

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IO

binread

Constructor Details

#initialize(path, flags = 'r') ⇒ File

Instance Methods

249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'opal/stdlib/nodejs/file.rb', line 249
def initialize(path, flags = 'r')
 @binary_flag = flags.include?('b')
 # Node does not recognize this flag
 flags = flags.delete('b')
 # encoding flag is unsupported
 encoding_option_rx = /:(.*)/
 if encoding_option_rx.match?(flags)
 `handle_unsupported_feature("Encoding option (:encoding) is unsupported by Node.js openSync method and will be removed.")`
 flags = flags.sub(encoding_option_rx, '')
 end
 @path = path
 @flags = flags
 @fd = `executeIOAction(function(){return __fs__.openSync(path, flags)})`
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path

264
265
266
# File 'opal/stdlib/nodejs/file.rb', line 264
def path
 @path
end

Class Method Details

.absolute_path(path, basedir = nil) ⇒ Object

241
242
243
244
245
# File 'opal/stdlib/nodejs/file.rb', line 241
def self.absolute_path(path, basedir = nil)
 path = path.respond_to?(:to_path) ? path.to_path : path
 basedir ||= Dir .pwd 
 `return __path__.normalize(__path__.resolve(#{basedir.to_str}, #{path.to_str})).split(__path__.sep).join(__path__.posix.sep)`
end

.directory?(path) ⇒ Boolean

Returns:

175
176
177
178
179
180
181
182
183
184
185
# File 'opal/stdlib/nodejs/file.rb', line 175
def self.directory?(path)
 return false unless exist? path
 result = `executeIOAction(function(){return !!__fs__.lstatSync(path).isDirectory()})`
 unless result
 realpath = realpath(path)
 if realpath != path
 result = `executeIOAction(function(){return !!__fs__.lstatSync(realpath).isDirectory()})`
 end
 end
 result
end

.exist?(path) ⇒ Boolean

Returns:

152
153
154
155
# File 'opal/stdlib/nodejs/file.rb', line 152
def self.exist?(path)
 path = path.path if path.respond_to? :path
 `return executeIOAction(function(){return __fs__.existsSync(#{path})})`
end

.file?(path) ⇒ Boolean

Returns:

187
188
189
190
191
192
193
194
195
196
197
# File 'opal/stdlib/nodejs/file.rb', line 187
def self.file?(path)
 return false unless exist? path
 result = `executeIOAction(function(){return !!__fs__.lstatSync(path).isFile()})`
 unless result
 realpath = realpath(path)
 if realpath != path
 result = `executeIOAction(function(){return !!__fs__.lstatSync(realpath).isFile()})`
 end
 end
 result
end

.join(*paths) ⇒ Object

171
172
173
# File 'opal/stdlib/nodejs/file.rb', line 171
def self.join(*paths)
 `__path__.posix.join.apply(__path__, #{paths})`
end

.mtime(path) ⇒ Object

233
234
235
# File 'opal/stdlib/nodejs/file.rb', line 233
def self.mtime(path)
 `return executeIOAction(function(){return __fs__.statSync(#{path}).mtime})`
end

.open(path, mode = 'r') ⇒ Object

215
216
217
218
219
220
221
222
223
224
225
226
# File 'opal/stdlib/nodejs/file.rb', line 215
def self.open(path, mode = 'r')
 file = new(path, mode)
 if block_given?
 begin
 yield(file)
 ensure
 file.close
 end
 else
 file
 end
end

.read(path) ⇒ Object

143
144
145
# File 'opal/stdlib/nodejs/file.rb', line 143
def self.read(path)
 `return executeIOAction(function(){return __fs__.readFileSync(#{path}).toString()})`
end

.readable?(path) ⇒ Boolean

Returns:

199
200
201
202
203
204
205
206
207
208
209
# File 'opal/stdlib/nodejs/file.rb', line 199
def self.readable?(path)
 return false unless exist? path
 %{
 try {
 __fs__.accessSync(path, __fs__.R_OK);
 return true;
 } catch (error) {
 return false;
 }
 }
end

.realpath(pathname, dir_string = nil, cache = nil, &block) ⇒ Object

157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'opal/stdlib/nodejs/file.rb', line 157
def self.realpath(pathname, dir_string = nil, cache = nil, &block)
 pathname = join(dir_string, pathname) if dir_string
 if block_given?
 `
 __fs__.realpath(#{pathname}, #{cache}, function(error, realpath){
 if (error) Opal.IOError.$new(error.message)
 else #{block.call(`realpath`)}
 })
 `
 else
 `return executeIOAction(function(){return __fs__.realpathSync(#{pathname}, #{cache})})`
 end
end

.size(path) ⇒ Object

211
212
213
# File 'opal/stdlib/nodejs/file.rb', line 211
def self.size(path)
 `return executeIOAction(function(){return __fs__.lstatSync(path).size})`
end

.stat(path) ⇒ Object

228
229
230
231
# File 'opal/stdlib/nodejs/file.rb', line 228
def self.stat(path)
 path = path.path if path.respond_to? :path
 File ::Stat .new (path)
end

.symlink?(path) ⇒ Boolean

Returns:

237
238
239
# File 'opal/stdlib/nodejs/file.rb', line 237
def self.symlink?(path)
 `return executeIOAction(function(){return __fs__.lstatSync(#{path}).isSymbolicLink()})`
end

.write(path, data) ⇒ Object

147
148
149
150
# File 'opal/stdlib/nodejs/file.rb', line 147
def self.write(path, data)
 `executeIOAction(function(){return __fs__.writeFileSync(#{path}, #{data})})`
 data.size
end

Instance Method Details

#closeObject

333
334
335
# File 'opal/stdlib/nodejs/file.rb', line 333
def close
 `executeIOAction(function(){return __fs__.closeSync(#{@fd})})`
end

#each_line(separator = $/, &block) ⇒ Object

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'opal/stdlib/nodejs/file.rb', line 295
def each_line(separator = $/, &block)
 if @eof
 return block_given? ? self : [].to_enum
 end
 if block_given?
 lines = File .read (@path)
 %x{
 self.eof = false;
 self.lineno = 0;
 var chomped = #{lines.chomp},
 trailing = lines.length != chomped.length,
 splitted = chomped.split(separator);
 for (var i = 0, length = splitted.length; i < length; i++) {
 self.lineno += 1;
 if (i < length - 1 || trailing) {
 #{yield `splitted[i] + separator`};
 }
 else {
 #{yield `splitted[i]`};
 }
 }
 self.eof = true;
 }
 self
 else
 read.each_line separator
 end
end

#flushObject

329
330
331
# File 'opal/stdlib/nodejs/file.rb', line 329
def flush
 `executeIOAction(function(){return __fs__.fsyncSync(#{@fd})})`
end

#mtimeObject

337
338
339
# File 'opal/stdlib/nodejs/file.rb', line 337
def mtime
 `return executeIOAction(function(){return __fs__.statSync(#{@path}).mtime})`
end

#readObject

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'opal/stdlib/nodejs/file.rb', line 266
def read
 if @eof
 ''
 else
 if @binary_flag
 %x{
 var buf = executeIOAction(function(){return __fs__.readFileSync(#{@path})})
 var content
 if (is_utf8(buf)) {
 content = buf.toString('utf8')
 } else {
 // coerce to utf8
 content = __utf8TextDecoder__.decode(__textEncoder__.encode(buf.toString('binary')))
 }
 }
 res = `content`
 else
 res = `executeIOAction(function(){return __fs__.readFileSync(#{@path}).toString('utf8')})`
 end
 @eof = true
 @lineno = res.size
 res
 end
end

#readlines(separator = $/) ⇒ Object

291
292
293
# File 'opal/stdlib/nodejs/file.rb', line 291
def readlines(separator = $/)
 each_line(separator).to_a
end

#write(string) ⇒ Object

325
326
327
# File 'opal/stdlib/nodejs/file.rb', line 325
def write(string)
 `executeIOAction(function(){return __fs__.writeSync(#{@fd}, #{string})})`
end

AltStyle によって変換されたページ (->オリジナル) /