Skip to content

Commit

Permalink
Move IO methods that rely on path to File (#1800)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored and iliabylich committed Apr 9, 2018
1 parent e6be7fe commit ceda559
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
41 changes: 41 additions & 0 deletions stdlib/nodejs/file.rb
Expand Up @@ -175,6 +175,47 @@ def initialize(path, flags = 'r')

attr_reader :path

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

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

def write(string)
`executeIOAction(function(){return __fs__.writeSync(#{@fd}, #{string})})`
end
Expand Down
41 changes: 0 additions & 41 deletions stdlib/nodejs/io.rb
Expand Up @@ -35,47 +35,6 @@ def self.read(path)
File.read(path)
end

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

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

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

0 comments on commit ceda559

Please sign in to comment.