Class: IO

Inherits:
Object show all
Defined in:
opal/stdlib/nodejs/io.rb

Direct Known Subclasses

File, StringIO

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIO

Returns a new instance of IO



26
27
28
29
# File 'opal/stdlib/nodejs/io.rb', line 26

def initialize
  @eof = false
  @lineno = 0
end

Instance Attribute Details

#eofObject (readonly)

Returns the value of attribute eof



23
24
25
# File 'opal/stdlib/nodejs/io.rb', line 23

def eof
  @eof
end

#linenoObject (readonly)

Returns the value of attribute lineno



24
25
26
# File 'opal/stdlib/nodejs/io.rb', line 24

def lineno
  @lineno
end

Class Method Details

.binread(path) ⇒ Object



80
81
82
# File 'opal/stdlib/nodejs/io.rb', line 80

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

.read(path) ⇒ Object



35
36
37
# File 'opal/stdlib/nodejs/io.rb', line 35

def self.read(path)
  File.read(path)
end

.write(path, data) ⇒ Object



31
32
33
# File 'opal/stdlib/nodejs/io.rb', line 31

def self.write(path, data)
  File.write(path, data)
end

Instance Method Details

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'opal/stdlib/nodejs/io.rb', line 50

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
  end
end

#readObject



39
40
41
42
43
44
45
46
47
48
# File 'opal/stdlib/nodejs/io.rb', line 39

def read
  if @eof
    ''
  else
    res = `executeIOAction(function(){return __fs__.readFileSync(#{@path}).toString()})`
    @eof = true
    @lineno = res.size
    res
  end
end