Class: Work::Md::Parser::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/work/md/parser/engine.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: ParsedFile

Constant Summary collapse

IS_FROZEN_ERROR_MESSAGE =
'Work::Md::Parser::Engine is frozen'
IS_NOT_FROZEN_ERROR_MESSAGE =
'Work::Md::Parser::Engine is not frozen'

Instance Method Summary collapse

Constructor Details

#initializeEngine

Returns a new instance of Engine.



25
26
27
28
29
# File 'lib/work/md/parser/engine.rb', line 25

def initialize
  @t = Work::Md::Config.translations
  @parsed_files = []
  @frozen = false
end

Instance Method Details

#add_file(file) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/work/md/parser/engine.rb', line 31

def add_file(file)
  raise IS_FROZEN_ERROR_MESSAGE if @frozen

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

  return unless file_content.start_with?('# ')

  @parsed_files.push(parse_file_content(file_content))
end

#average_pomodorosObject



82
83
84
85
86
87
88
# File 'lib/work/md/parser/engine.rb', line 82

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_barsObject

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/work/md/parser/engine.rb', line 108

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 if f.pomodoros
      mee = f.meetings.map { '📅' }.join if f.meetings
      int = f.interruptions.map { '⚠️' }.join if f.interruptions
      dif = f.difficulties.map { '😓' }.join if f.difficulties
      obs = f.observations.map { '📝' }.join if f.observations
      tas = f.tasks.map { '✔️' }.join if f.tasks

      "(#{f.date}) #{pom}#{mee}#{int}#{dif}#{obs}#{tas}"
    end
end

#difficultiesObject



70
71
72
73
74
# File 'lib/work/md/parser/engine.rb', line 70

def difficulties
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

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

#done_tasksObject



45
46
47
48
49
50
# File 'lib/work/md/parser/engine.rb', line 45

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

#freezeObject

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



128
129
130
# File 'lib/work/md/parser/engine.rb', line 128

def freeze
  @frozen = true
end

#interruptionsObject



64
65
66
67
68
# File 'lib/work/md/parser/engine.rb', line 64

def interruptions
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

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

#meetingsObject



58
59
60
61
62
# File 'lib/work/md/parser/engine.rb', line 58

def meetings
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

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

#observationsObject



76
77
78
79
80
# File 'lib/work/md/parser/engine.rb', line 76

def observations
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

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

#pomodoros_bars(_file = nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/work/md/parser/engine.rb', line 97

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_sumObject



90
91
92
93
94
95
# File 'lib/work/md/parser/engine.rb', line 90

def pomodoros_sum
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

  @pomodoros_sum ||=
    @parsed_files.reduce(0) { |sum, f| sum + f.pomodoros || 0 }
end

#tasksObject



52
53
54
55
56
# File 'lib/work/md/parser/engine.rb', line 52

def tasks
  raise IS_NOT_FROZEN_ERROR_MESSAGE unless @frozen

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