class Work::Md::Parser::Engine

rubocop:disable Metrics/ClassLength

Constants

IS_FROZEN_ERROR_MESSAGE
IS_NOT_FROZEN_ERROR_MESSAGE

Public Class Methods

new() click to toggle source
# File lib/work/md/parser/engine.rb, line 23
def initialize
  @t = Work::Md::Config.translations
  @parsed_files = []
  @frozen = false
end

Public Instance Methods

add_file(file) click to toggle source
# File lib/work/md/parser/engine.rb, line 29
def add_file(file)
  raise IS_FROZEN_ERROR_MESSAGE if @frozen

  begin
    file_content = ::File.read(file)
  rescue Errno::ENOENT
    return
  end

  return unless file_content.start_with?('# ')

  @parsed_files.push(parse_file_content(file_content))
end
average_pomodoros() click to toggle source
# File lib/work/md/parser/engine.rb, line 80
def average_pomodoros
  if @parsed_files.size.positive? && pomodoros_sum.positive?
    return (pomodoros_sum.to_f / @parsed_files.size).round(1)
  end

  0
end
days_bars() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/work/md/parser/engine.rb, line 106
def days_bars
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  return @days_bars if @days_bars

  @days_bars ||=
    @parsed_files.map do |f|
      pom = (1..f.pomodoros).map { '⬛' }.join
      mee = f.meetings.map { '📅' }.join
      int = f.interruptions.map { '⚠️' }.join
      dif = f.difficulties.map { '😓' }.join
      obs = f.observations.map { '📝' }.join
      tas = f.tasks.map { '✔️' }.join

      "(#{f.date}) #{pom}#{mee}#{int}#{dif}#{obs}#{tas}"
    end
end
difficulties() click to toggle source
# File lib/work/md/parser/engine.rb, line 68
def difficulties
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @difficulties ||= @parsed_files.map(&:difficulties).flatten
end
done_tasks() click to toggle source
# File lib/work/md/parser/engine.rb, line 43
def done_tasks
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @done_tasks ||=
    tasks.select { |t| t.start_with?('x]') || t.start_with?('X]') }
end
freeze() click to toggle source

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity

# File lib/work/md/parser/engine.rb, line 126
def freeze
  @frozen = true
end
interruptions() click to toggle source
# File lib/work/md/parser/engine.rb, line 62
def interruptions
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @interruptions ||= @parsed_files.map(&:interruptions).flatten
end
meetings() click to toggle source
# File lib/work/md/parser/engine.rb, line 56
def meetings
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @meetings ||= @parsed_files.map(&:meetings).flatten
end
observations() click to toggle source
# File lib/work/md/parser/engine.rb, line 74
def observations
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @observations ||= @parsed_files.map(&:observations).flatten
end
pomodoros_bars(_file = nil) click to toggle source
# File lib/work/md/parser/engine.rb, line 95
def pomodoros_bars(_file = nil)
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @pomodoros_bars ||=
    @parsed_files.map do |f|
      "(#{f.date}) #{(1..f.pomodoros).map { '⬛' }.join}"
    end
end
pomodoros_sum() click to toggle source
# File lib/work/md/parser/engine.rb, line 88
def pomodoros_sum
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @pomodoros_sum ||=
    @parsed_files.reduce(0) { |sum, f| sum + f.pomodoros || 0 }
end
tasks() click to toggle source
# File lib/work/md/parser/engine.rb, line 50
def tasks
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @tasks ||= @parsed_files.map(&:tasks).flatten
end

Private Instance Methods

basic_parse(content, start_with: nil) click to toggle source
# File lib/work/md/parser/engine.rb, line 191
def basic_parse(content, start_with: nil)
  return content.split("#{start_with}:\n")[1] unless start_with.nil?

  content.split(":\n\n")[1]
end
clear_list(list) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/work/md/parser/engine.rb, line 198
def clear_list(list)
  return list unless list.is_a?(Array)

  list
    .map { |s| s.gsub('---', '') unless s.nil? }
    .select { |s| (s != "\n\n") && (s != "\n\n\n") }
    .map(&:strip)
    .reject { |s| (s == '') }
end
parse_check_list(content, start_with: nil) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity

# File lib/work/md/parser/engine.rb, line 179
def parse_check_list(content, start_with: nil)
  clear_list(basic_parse(content, start_with: start_with).split('- ['))
end
parse_content(parsed_file, content) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/work/md/parser/engine.rb, line 144
def parse_content(parsed_file, content)
  if content.start_with?('# ')
    parsed_file.date =
      content.split(' - ')[0].gsub('# ', '').gsub("\n\n", '')
  elsif content.start_with?(@t[:tasks])
    parsed_file.tasks = parse_check_list(content, start_with: @t[:tasks])
  elsif content.start_with?(@t[:meetings])
    parsed_file.meetings = parse_check_list(content, start_with: @t[:meetings])
  elsif content.start_with?(@t[:interruptions])
    parsed_file.interruptions =
      parse_list(content, start_with: @t[:interruptions]).map do |interruption|
        "(#{parsed_file.date}) #{interruption}"
      end
  elsif content.start_with?(@t[:difficulties])
    parsed_file.difficulties =
      parse_list(
        content, start_with: @t[:difficulties]
      ).map do |difficulty|
        "(#{parsed_file.date}) #{difficulty}"
      end
  elsif content.start_with?(@t[:observations])
    parsed_file.observations =
      parse_list(
        content, start_with: @t[:observations]
      ).map do |observations|
        "(#{parsed_file.date}) #{observations}"
      end
  elsif content.start_with?(@t[:pomodoros])
    parsed_file.pomodoros =
      parse_pomodoro(content, start_with: @t[:pomodoros])
  end
end
parse_file_content(file_content) click to toggle source
# File lib/work/md/parser/engine.rb, line 132
def parse_file_content(file_content)
  parsed_file = ParsedFile.new

  file_content
    .split('### ')
    .each { |content| parse_content(parsed_file, content) }

  parsed_file
end
parse_list(content, start_with: nil) click to toggle source
# File lib/work/md/parser/engine.rb, line 183
def parse_list(content, start_with: nil)
  clear_list(basic_parse(content, start_with: start_with).split('- '))
end
parse_pomodoro(content, start_with: nil) click to toggle source
# File lib/work/md/parser/engine.rb, line 187
def parse_pomodoro(content, start_with: nil)
  basic_parse(content, start_with: start_with).scan(/\d+/).first.to_i
end