Class: PrettyPrint::Breakable

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

Overview

The Breakable class is used for breaking up object information

This class is intended for internal use of the PrettyPrint buffers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sep, width, q) ⇒ Breakable

Create a new Breakable object.

Arguments:

  • sep String of the separator

  • width Fixnum width of the sep

  • q parent PrettyPrint object, to base from



365
366
367
368
369
370
371
372
# File 'lib/prettyprint.rb', line 365

def initialize(sep, width, q)
  @obj = sep
  @width = width
  @pp = q
  @indent = q.indent
  @group = q.current_group
  @group.breakables.push self
end

Instance Attribute Details

#indentObject (readonly)

The number of spaces to indent.

This is inferred from q within PrettyPrint, passed in ::new



385
386
387
# File 'lib/prettyprint.rb', line 385

def indent
  @indent
end

#objObject (readonly)

Holds the separator String

The sep argument from ::new



377
378
379
# File 'lib/prettyprint.rb', line 377

def obj
  @obj
end

#widthObject (readonly)

The width of obj / sep



380
381
382
# File 'lib/prettyprint.rb', line 380

def width
  @width
end

Instance Method Details

#output(out, output_width) ⇒ Object

Render the String text of the objects that have been added to this Breakable object.

Output the text to out, and increment the width to output_width



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/prettyprint.rb', line 391

def output(out, output_width)
  @group.breakables.shift
  if @group.break?
    out << @pp.newline
    out << @pp.genspace.call(@indent)
    @indent
  else
    @pp.group_queue.delete @group if @group.breakables.empty?
    out << @obj
    output_width + @width
  end
end