class Code

Code data object

Attributes

dirname[R]
features[RW]
filename[R]
lines[R]
process[RW]
questions[R]
type[R]

Public Class Methods

new(dirname, filename, type) click to toggle source

Initialize Code object @param dirname (String) @param filename (String) @param type (String)

# File lib/asker/data/code.rb, line 18
def initialize(dirname, filename, type)
  @dirname = dirname
  @filename = filename
  @type = type
  @filepath = File.join(@dirname, @filename)
  @process = false
  @features = []
  @lines = load(@filepath)
  @questions = []
end

Public Instance Methods

debug() click to toggle source
# File lib/asker/data/code.rb, line 41
def debug
  Logger.verbose CodeStringFormatter.to_s(self)
end
lines_to_s(lines) click to toggle source
# File lib/asker/data/code.rb, line 33
def lines_to_s(lines)
  out = ''
  lines.each_with_index do |line, index|
    out << format("%2d| #{line}\n", (index + 1))
  end
  out
end
process?() click to toggle source
# File lib/asker/data/code.rb, line 29
def process?
  @process
end

Private Instance Methods

encode_and_split(text, encoding = :default) click to toggle source
# File lib/asker/data/code.rb, line 57
def encode_and_split(text, encoding = :default)
  # Convert text to UTF-8 deleting unknown chars
  text = text || '' # Ensure text is not nil
  flag = [:default, 'UTF-8'].include? encoding
  return text.encode('UTF-8', invalid: :replace).split("\n") if flag

  # Convert text from input ENCODING to UTF-8
  ec = Encoding::Converter.new(encoding.to_s, 'UTF-8')
  begin
    text = ec.convert(text)
  rescue StandardError => e
    puts "[ERROR] Encoding: #{e}"
  end

  text.split("\n")
end
load(filepath) click to toggle source
# File lib/asker/data/code.rb, line 47
def load(filepath)
  return if filepath.nil?
  unless File.exist? filepath
    Logger.verboseln Rainbow("[ERROR] Unkown file #{filepath}").red.bright
    return
  end
  content = File.read(filepath)
  encode_and_split(content)
end