class WorkMd::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 20
def initialize
  @t = WorkMd::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 26
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 77
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 103
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 65
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 40
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 123
def freeze
  @frozen = true
end
interruptions() click to toggle source
# File lib/work_md/parser/engine.rb, line 59
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 53
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 71
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 92
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 85
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 47
def tasks
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

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

Private Instance Methods

basic_parse(content) click to toggle source
# File lib/work_md/parser/engine.rb, line 180
def basic_parse(content)
  content.split(":\n\n")[1]
end
clear_list(list) click to toggle source
# File lib/work_md/parser/engine.rb, line 184
def clear_list(list)
  return list unless list.is_a?(Array)

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

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

# File lib/work_md/parser/engine.rb, line 168
def parse_check_list(content)
  clear_list(basic_parse(content).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 141
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)
  elsif content.start_with?(@t[:meetings])
    parsed_file.meetings = parse_check_list(content)
  elsif content.start_with?(@t[:interruptions])
    parsed_file.interruptions = parse_list(content).map do |interruption|
      "(#{parsed_file.date}) #{interruption}"
    end
  elsif content.start_with?(@t[:difficulties])
    parsed_file.difficulties = parse_list(content).map do |difficulty|
      "(#{parsed_file.date}) #{difficulty}"
    end
  elsif content.start_with?(@t[:observations])
    parsed_file.observations = parse_list(content).map do |observations|
      "(#{parsed_file.date}) #{observations}"
    end
  elsif content.start_with?(@t[:pomodoros])
    parsed_file.pomodoros = parse_pomodoro(content)
  end
end
parse_file_content(file_content) click to toggle source
# File lib/work_md/parser/engine.rb, line 129
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) click to toggle source
# File lib/work_md/parser/engine.rb, line 172
def parse_list(content)
  clear_list(basic_parse(content).split('- '))
end
parse_pomodoro(content) click to toggle source
# File lib/work_md/parser/engine.rb, line 176
def parse_pomodoro(content)
  basic_parse(content).scan(/\d+/).first.to_i
end