Class: CSVPlusPlus::SourceCode

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/csv_plus_plus/source_code.rb

Overview

Information about the unparsed source code

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, filename: nil) ⇒ SourceCode

Returns a new instance of SourceCode.

Parameters:

  • input (::String)

    The source code being parsed

  • filename (::String, nil) (defaults to: nil)

    The name of the file the source came from. If not set we assume it came from stdin



28
29
30
31
32
33
34
35
36
# File 'lib/csv_plus_plus/source_code.rb', line 28

def initialize(input:, filename: nil)
  @input = input
  @filename = ::T.let(filename || 'stdin', ::String)

  lines = input.split(/[\r\n]/)
  @length_of_file = ::T.let(lines.length, ::Integer)
  @length_of_code_section = ::T.let(count_code_section_lines(lines), ::Integer)
  @length_of_csv_section = ::T.let(@length_of_file - @length_of_code_section, ::Integer)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



13
14
15
# File 'lib/csv_plus_plus/source_code.rb', line 13

def filename
  @filename
end

#inputObject (readonly)

Returns the value of attribute input.



10
11
12
# File 'lib/csv_plus_plus/source_code.rb', line 10

def input
  @input
end

#length_of_code_sectionObject (readonly)

Returns the value of attribute length_of_code_section.



19
20
21
# File 'lib/csv_plus_plus/source_code.rb', line 19

def length_of_code_section
  @length_of_code_section
end

#length_of_csv_sectionObject (readonly)

Returns the value of attribute length_of_csv_section.



16
17
18
# File 'lib/csv_plus_plus/source_code.rb', line 16

def length_of_csv_section
  @length_of_csv_section
end

#length_of_fileObject (readonly)

Returns the value of attribute length_of_file.



22
23
24
# File 'lib/csv_plus_plus/source_code.rb', line 22

def length_of_file
  @length_of_file
end

Instance Method Details

#in_code_section?(line_number) ⇒ T::Boolean

Does the given line_number land in the code section of the file? (which includes the — separator)

Parameters:

  • line_number (Integer)

Returns:

  • (T::Boolean)


44
45
46
# File 'lib/csv_plus_plus/source_code.rb', line 44

def in_code_section?(line_number)
  line_number <= @length_of_code_section
end

#in_csv_section?(line_number) ⇒ T::Boolean

Does the given line_number land in the CSV section of the file?

Parameters:

  • line_number (Integer)

Returns:

  • (T::Boolean)


54
55
56
# File 'lib/csv_plus_plus/source_code.rb', line 54

def in_csv_section?(line_number)
  line_number > @length_of_code_section
end