Sha256: 5b1129c6d5869be704ed1ea176f225b8c293625b8424caa67168631d24f3e96a

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require_relative '../ai/code/code_ai_factory'
require_relative '../project'
require_relative '../logger'
require_relative '../formatter/code_string_formatter'

##
# Code data object
class Code
  attr_reader :dirname, :filename, :type
  attr_accessor :process, :features
  attr_reader :lines, :questions

  ##
  # Initialize Code object
  # @param dirname (String)
  # @param filename (String)
  # @param type (String)
  def initialize(dirname, filename, type)
    @dirname = dirname
    @filename = filename
    @type = type
    @filepath = File.join(@dirname, @filename)
    @process = false
    @features = []
    @lines = load(@filepath)
    @questions = []
  end

  def process?
    @process
  end

  def lines_to_s(lines)
    out = ''
    lines.each_with_index do |line, index|
      out << format("%2d| #{line}\n", (index + 1))
    end
    out
  end

  def debug
    Logger.verbose CodeStringFormatter.to_s(self)
  end

  private

  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

  def encode_and_split(text, encoding = :default)
    # Convert text to UTF-8 deleting unknown chars
    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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asker-tool-2.2.0 lib/asker/data/code.rb