Class: SeeingIsBelieving::Binary::AlignChunk

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/binary/align_chunk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, start_line, end_line) ⇒ AlignChunk

Returns a new instance of AlignChunk.



6
7
8
# File 'lib/seeing_is_believing/binary/align_chunk.rb', line 6

def initialize(body, start_line, end_line)
  self.body, self.start_line, self.end_line = body, start_line, end_line
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/seeing_is_believing/binary/align_chunk.rb', line 4

def body
  @body
end

#end_lineObject

Returns the value of attribute end_line.



4
5
6
# File 'lib/seeing_is_believing/binary/align_chunk.rb', line 4

def end_line
  @end_line
end

#start_lineObject

Returns the value of attribute start_line.



4
5
6
# File 'lib/seeing_is_believing/binary/align_chunk.rb', line 4

def start_line
  @start_line
end

Instance Method Details

#line_length_for(line_number) ⇒ Object

max line length of the the chunk (newline separated sections of code exempting comments) + 2 spaces for padding



11
12
13
# File 'lib/seeing_is_believing/binary/align_chunk.rb', line 11

def line_length_for(line_number)
  line_lengths[line_number]
end

#line_lengthsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/seeing_is_believing/binary/align_chunk.rb', line 15

def line_lengths
  @line_lengths ||= Hash[
    body.each_line
        .map(&:chomp)
        .map.with_index(1) { |line, index| [line, index] }
        .take_while        { |line, index| not start_of_data_segment? line }
        .select            { |line, index| not SyntaxAnalyzer.begins_multiline_comment?(line) .. SyntaxAnalyzer.ends_multiline_comment?(line ) }
        .reject            { |line, index| SyntaxAnalyzer.ends_in_comment? line }
        .slice_before      { |line, index| line == '' }
        .map { |slice|
          max_chunk_length = 2 + slice.select { |line, index| start_line <= index && index <= end_line }
                                      .map { |line, index| line.length }
                                      .max
          slice.map { |line, index| [index, max_chunk_length] }
        }
        .flatten(1)
  ]
end

#start_of_data_segment?(line) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/seeing_is_believing/binary/align_chunk.rb', line 34

def start_of_data_segment?(line)
  SyntaxAnalyzer.begins_data_segment?(line.chomp)
end