Class: JustRun::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/justrun.rb

Instance Method Summary collapse

Constructor Details

#initialize(stdin) ⇒ Writer

Returns a new instance of Writer.



102
103
104
105
# File 'lib/justrun.rb', line 102

def initialize stdin
  @buffer = ''
  @stdin = stdin
end

Instance Method Details

#end(str = '') ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/justrun.rb', line 120

def end str = ''
  if str.length > 0
    write str
  end
  if @buffer.length > 0
    on_empty -> {
      self.end
    }
  else
    @stdin.close_write
  end
end

#on_empty(callback) ⇒ Object



116
117
118
# File 'lib/justrun.rb', line 116

def on_empty callback
  @on_empty = callback
end

#processObject



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/justrun.rb', line 133

def process
  return unless @buffer.length > 0
  loop do
    written = @stdin.write_nonblock @buffer
    @buffer = @buffer[written..-1]
    if @buffer.length == 0 and @on_empty
      @on_empty.call
      return
    end
    return true if written == 0
  end
rescue IO::WaitWritable, Errno::EINTR
end

#puts(str) ⇒ Object



107
108
109
# File 'lib/justrun.rb', line 107

def puts str
  write "#{str}\n"
end

#write(str) ⇒ Object



111
112
113
114
# File 'lib/justrun.rb', line 111

def write str
  @buffer << str
  process
end