Class: CodeBlock::Block

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

Overview

This is the class that does the work of filtering which lines should be displayed and which shouldn’t. Generally you won’t have to work directly with this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_code, opts = {}) ⇒ Block


initialize



385
386
387
388
389
390
391
392
393
394
# File 'lib/codeblock.rb', line 385

def initialize(p_code, opts={})
  @code = p_code
  @collapse = opts['collapse']
  @tab = opts['tab'] || '    '
  @start = opts['start']
  @end = opts['end']
  
  # build lines array
  build_lines()
end

Instance Attribute Details

#linesObject (readonly)

The array of CodeBlock::Line objects.



405
406
407
# File 'lib/codeblock.rb', line 405

def lines
  @lines
end

#namedObject (readonly)

A hash of named lines.



408
409
410
# File 'lib/codeblock.rb', line 408

def named
  @named
end

Instance Method Details

#line_nums_to_sObject

Builds the string that is returned by CodeBlock#line_nums_to_s.



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/codeblock.rb', line 467

def line_nums_to_s
  # $tm.hrm
  rv = []
  
  # get maximum width
  width = @lines[-1].number.to_s.length
  
  # loop through lines
  @lines.each do |line|
    rv.push line.number.to_s.rjust(width) + '.'
  end
  
  # return
  return rv.join("\n")
end

#notes_to_sObject

Builds the string that is returned by CodeBlock#notes_to_s.



492
493
494
495
496
497
498
499
500
501
502
# File 'lib/codeblock.rb', line 492

def notes_to_s
  rv = []
  
  # loop through lines
  @lines.each do |line|
    rv.push line.notes || ''
  end
  
  # return
  return rv.join("\n")
end

#to_sObject

Builds the string that is returned by CodeBlock#to_s.



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/codeblock.rb', line 421

def to_s()
  # $tm.hrm
  rv = []
  indent = @code.indent || ''
  line_nums = @code.line_nums
  
  # get maximum width
  if line_nums
    max_width = @lines[-1].number.to_s.length
  end
  
  # build return array
  @lines.each do |line|
    # get string
    v = line.to_s
    
    # substitute tabs
    if @tab
      v = v.gsub(/\t/mu, @tab)
    end
    
    # line numbers
    if line_nums
      v = line.number.to_s.rjust(max_width) + '.  ' + v
    end
    
    # indent
    v = indent + v
    
    # add to return array
    rv.push v
  end
  
  # return
  return rv.join("\n")
end