Class: NanDoc::CreateNanDocSite::StdErrListener

Inherits:
Object
  • Object
show all
Defined in:
lib/nandoc/commands/create-nandoc-site.rb

Overview

Somehow legitimize these awful hacks with an entire class

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ StdErrListener

Returns a new instance of StdErrListener.



219
220
221
222
223
224
225
# File 'lib/nandoc/commands/create-nandoc-site.rb', line 219

def initialize &block
  @prev = $stderr
  $stderr = self
  @expired = false
  @whens = []
  block.call(self)
end

Instance Method Details

#puts(*args) ⇒ Object Also known as: write



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/nandoc/commands/create-nandoc-site.rb', line 229

def puts *args
  if @expired
    fail("hack failed when trying to write: #{str}")
  elsif args.size != 1
    expire!
    $stderr.puts(*args)
  else
    found = @whens.detect{|x| x[:regex] =~ args.first}
    if found
      expire!
      found[:block].call
    else
      expire!
      $stderr.puts(*args)
    end
  end
end

#when(regex, &block) ⇒ Object



226
227
228
# File 'lib/nandoc/commands/create-nandoc-site.rb', line 226

def when regex, &block
  @whens.push(:regex=>regex, :block=>block)
end