Class: File

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, flags) ⇒ File

Instance Methods



82
83
84
85
86
87
# File 'opal/stdlib/nodejs/file.rb', line 82

def initialize(path, flags)
  flags = flags.gsub(/b/, '')
  @path = path
  @flags = flags
  @fd = `__fs__.openSync(path, flags)`
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path



89
90
91
# File 'opal/stdlib/nodejs/file.rb', line 89

def path
  @path
end

Class Method Details

.basename(path, ext = undefined) ⇒ Object



37
38
39
# File 'opal/stdlib/nodejs/file.rb', line 37

def self.basename(path, ext = undefined)
  `__path__.basename(#{path}, #{ext})`
end

.directory?(path) ⇒ Boolean

Returns:



49
50
51
52
# File 'opal/stdlib/nodejs/file.rb', line 49

def self.directory? path
  return nil unless exist? path
  `!!__fs__.lstatSync(path).isDirectory()`
end

.dirname(path) ⇒ Object



41
42
43
# File 'opal/stdlib/nodejs/file.rb', line 41

def self.dirname(path)
  `__path__.dirname(#{path})`
end

.exist?(path) ⇒ Boolean

Returns:



19
20
21
# File 'opal/stdlib/nodejs/file.rb', line 19

def self.exist? path
  `__fs__.existsSync(#{path})`
end

.file?(path) ⇒ Boolean

Returns:



54
55
56
57
# File 'opal/stdlib/nodejs/file.rb', line 54

def self.file? path
  return nil unless exist? path
  `!!__fs__.lstatSync(path).isFile()`
end

.join(*paths) ⇒ Object



45
46
47
# File 'opal/stdlib/nodejs/file.rb', line 45

def self.join(*paths)
  `__path__.join.apply(__path__, #{paths})`
end

.open(path, flags) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'opal/stdlib/nodejs/file.rb', line 64

def self.open path, flags
  file = new(path, flags)
  if block_given?
    begin
      yield(file)
    ensure
      file.close
    end
  else
    file
  end
end

.read(path) ⇒ Object



10
11
12
# File 'opal/stdlib/nodejs/file.rb', line 10

def self.read path
  `__fs__.readFileSync(#{path}).toString()`
end

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



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'opal/stdlib/nodejs/file.rb', line 23

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) #{raise error.message}
      else #{block.call(`realpath`)}
    })
    `
  else
    `__fs__.realpathSync(#{pathname}, #{cache})`
  end
end

.size(path) ⇒ Object



59
60
61
62
# File 'opal/stdlib/nodejs/file.rb', line 59

def self.size path
  return nil unless exist? path
  `__fs__.lstatSync(path).size`
end

.write(path, data) ⇒ Object



14
15
16
17
# File 'opal/stdlib/nodejs/file.rb', line 14

def self.write path, data
  `__fs__.writeFileSync(#{path}, #{data})`
  data.size
end

Instance Method Details

#closeObject



99
100
101
# File 'opal/stdlib/nodejs/file.rb', line 99

def close
  `__fs__.closeSync(#{@fd})`
end

#flushObject



95
96
97
# File 'opal/stdlib/nodejs/file.rb', line 95

def flush
  `__fs__.fsyncSync(#@fd)`
end

#write(string) ⇒ Object



91
92
93
# File 'opal/stdlib/nodejs/file.rb', line 91

def write string
  `__fs__.writeSync(#{@fd}, #{string})`
end