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, #each_line, #read

Constructor Details

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

Instance Methods


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'opal/stdlib/nodejs/file.rb', line 158

def initialize(path, flags = 'r')
  binary_flag_regexp = /b/
  encoding_flag_regexp = /:(.*)/
  # binary flag is unsupported
  `handle_unsupported_feature("Binary flag (b) is unsupported by Node.js openSync method, removing flag.")` if flags.match(binary_flag_regexp)
  flags = flags.gsub(binary_flag_regexp, '')
  # encoding flag is unsupported
  `handle_unsupported_feature("Encoding flag (:encoding) is unsupported by Node.js openSync method, removing flag.")` if flags.match(encoding_flag_regexp)
  flags = flags.gsub(encoding_flag_regexp, '')
  @path = path
  @flags = flags
  @fd = `executeIOAction(function(){return __fs__.openSync(path, flags)})`
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path


172
173
174
# File 'opal/stdlib/nodejs/file.rb', line 172

def path
  @path
end

Class Method Details

.directory?(path) ⇒ Boolean

Returns:


94
95
96
97
98
99
100
101
102
103
104
# File 'opal/stdlib/nodejs/file.rb', line 94

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:


71
72
73
74
# File 'opal/stdlib/nodejs/file.rb', line 71

def self.exist? path
  path = path.path if path.respond_to? :path
  `return executeIOAction(function(){return __fs__.existsSync(#{path})})`
end

.file?(path) ⇒ Boolean

Returns:


106
107
108
109
110
111
112
113
114
115
116
# File 'opal/stdlib/nodejs/file.rb', line 106

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


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

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

.mtime(path) ⇒ Object


152
153
154
# File 'opal/stdlib/nodejs/file.rb', line 152

def self.mtime path
  `return executeIOAction(function(){return __fs__.statSync(#{path}).mtime})`
end

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


134
135
136
137
138
139
140
141
142
143
144
145
# File 'opal/stdlib/nodejs/file.rb', line 134

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


62
63
64
# File 'opal/stdlib/nodejs/file.rb', line 62

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

.readable?(path) ⇒ Boolean

Returns:


118
119
120
121
122
123
124
125
126
127
128
# File 'opal/stdlib/nodejs/file.rb', line 118

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


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'opal/stdlib/nodejs/file.rb', line 76

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


130
131
132
# File 'opal/stdlib/nodejs/file.rb', line 130

def self.size path
  `return executeIOAction(function(){return __fs__.lstatSync(path).size})`
end

.stat(path) ⇒ Object


147
148
149
150
# File 'opal/stdlib/nodejs/file.rb', line 147

def self.stat path
  path = path.path if path.respond_to? :path
  File::Stat.new(path)
end

.write(path, data) ⇒ Object


66
67
68
69
# File 'opal/stdlib/nodejs/file.rb', line 66

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

Instance Method Details

#closeObject


182
183
184
# File 'opal/stdlib/nodejs/file.rb', line 182

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

#flushObject


178
179
180
# File 'opal/stdlib/nodejs/file.rb', line 178

def flush
  `executeIOAction(function(){return __fs__.fsyncSync(#@fd)})`
end

#mtimeObject


186
187
188
# File 'opal/stdlib/nodejs/file.rb', line 186

def mtime
  `return executeIOAction(function(){return __fs__.statSync(#{@path}).mtime})`
end

#write(string) ⇒ Object


174
175
176
# File 'opal/stdlib/nodejs/file.rb', line 174

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