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

Instance Attribute Summary collapse

Attributes inherited from IO

#eof, #lineno

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IO

#each_line, #read

Constructor Details

#initialize(path, flags) ⇒ File

Instance Methods



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

def initialize(path, flags)
  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 = `__fs__.openSync(path, flags)`
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path



118
119
120
# File 'opal/stdlib/nodejs/file.rb', line 118

def path
  @path
end

Class Method Details

.directory?(path) ⇒ Boolean

Returns:



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

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

.exist?(path) ⇒ Boolean

Returns:



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

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

.file?(path) ⇒ Boolean

Returns:



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

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

.join(*paths) ⇒ Object



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

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

.mtime(path) ⇒ Object



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

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

.open(path, flags) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'opal/stdlib/nodejs/file.rb', line 80

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



33
34
35
# File 'opal/stdlib/nodejs/file.rb', line 33

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'opal/stdlib/nodejs/file.rb', line 47

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



75
76
77
78
# File 'opal/stdlib/nodejs/file.rb', line 75

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

.stat(path) ⇒ Object



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

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

.write(path, data) ⇒ Object



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

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

Instance Method Details

#closeObject



128
129
130
# File 'opal/stdlib/nodejs/file.rb', line 128

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

#flushObject



124
125
126
# File 'opal/stdlib/nodejs/file.rb', line 124

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

#mtimeObject



132
133
134
# File 'opal/stdlib/nodejs/file.rb', line 132

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

#write(string) ⇒ Object



120
121
122
# File 'opal/stdlib/nodejs/file.rb', line 120

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