Class: NRSER::Rash::Writer

Inherits:
IO
  • Object
show all
Defined in:
lib/nrser/rash/util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.



211
212
213
214
215
216
217
218
# File 'lib/nrser/rash/util.rb', line 211

def initialize(options = {})
  options = {:out => $stdout, :spacer => '  '}.merge options
  @spacer = options[:spacer]
  @level = 0
  @new_line = true
  @out = options[:out]
  super options[:out].fileno
end

Instance Attribute Details

#outObject

Returns the value of attribute out.



220
221
222
# File 'lib/nrser/rash/util.rb', line 220

def out
  @out
end

Instance Method Details

#block(start = nil, finish = nil, &body) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/nrser/rash/util.rb', line 234

def block(start = nil, finish = nil, &body)
  [start].flatten.each {|line| puts line } if start
  indent do
    yield
  end
  [finish].flatten.each {|line| puts line} if finish
end

#dedentObject



230
231
232
# File 'lib/nrser/rash/util.rb', line 230

def dedent
  @level -= 1
end

#indent(&b) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/nrser/rash/util.rb', line 222

def indent(&b)
  @level += 1
  if b
    yield
    dedent
  end
end

#write(obj) ⇒ Object

add the indent, unless it's called with a newline (puts calls write twice for every line, with the second call being "\n")



244
245
246
247
248
249
250
251
# File 'lib/nrser/rash/util.rb', line 244

def write(obj)
  if @new_line && @level > 0
    super indent_string
  end
  str = obj.to_s
  @new_line = (str[-1] == "\n")
  super str.lines.to_a.join(indent_string)
end