Class: File

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, flags) ⇒ File

Instance Methods



77
78
79
80
81
82
# File 'opal/stdlib/nodejs/file.rb', line 77

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



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

def path
  @path
end

Class Method Details

.basename(path, ext = undefined) ⇒ Object



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

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

.directory?(path) ⇒ Boolean

Returns:



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

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

.dirname(path) ⇒ Object



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

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

.exist?(path) ⇒ Boolean

Returns:



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

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

.file?(path) ⇒ Boolean

Returns:



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

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

.join(*paths) ⇒ Object



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

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

.open(path, flags) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'opal/stdlib/nodejs/file.rb', line 59

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



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'opal/stdlib/nodejs/file.rb', line 18

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



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

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

Instance Method Details

#closeObject



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

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

#flushObject



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

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

#write(string) ⇒ Object



86
87
88
# File 'opal/stdlib/nodejs/file.rb', line 86

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