Class: LogManager

Inherits:
Object
  • Object
show all
Includes:
TimeHelper
Defined in:
lib/devlogs/repository/log_manager.rb

Overview

LogManager is an abstraction class to orchestrate the internals of issue management and creation for a repository

Constant Summary collapse

LOG_FILE_SUFFIX =
"log.md"
VALID_DIRECTION =
%i[asc desc].freeze

Constants included from TimeHelper

TimeHelper::TIME_FORMAT_FILE_PREFIX

Instance Method Summary collapse

Methods included from TimeHelper

#current_time

Constructor Details

#initialize(repo_config_store) ⇒ LogManager

Returns a new instance of LogManager.



15
16
17
# File 'lib/devlogs/repository/log_manager.rb', line 15

def initialize(repo_config_store)
  @config_store = repo_config_store
end

Instance Method Details

#createObject

Adds a new entry to the repository



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/devlogs/repository/log_manager.rb', line 48

def create
  entry_file_name = "#{current_time}_#{LOG_FILE_SUFFIX}"

  entry_file_path = File.join(@config_store.dir, entry_file_name)

  template = LogTemplateRenderer.new(@config_store.template_file_path)

  unless File.exist?(entry_file_path)
    # Add default boiler plate if the file does not exist yet
    File.open(entry_file_path, "w") do |f|
      f.write template.render
    end
  end

  entry_file_path
end

#list(direction = :desc) ⇒ Object

Lists the log entries present in the repository

Parameters:

  • direction (Symbol) (defaults to: :desc)

    ascending or descending

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/devlogs/repository/log_manager.rb', line 23

def list(direction = :desc)
  raise ArgumentError, "Must be one of: " + VALID_DIRECTION unless VALID_DIRECTION.include?(direction.to_sym)

  # Anything with the _log.md suffix
  glob_pattern = File.join(@config_store.dir, "*_#{LOG_FILE_SUFFIX}")

  Dir.glob(glob_pattern).sort_by do |fpath|
    # The date is joined by two underscores to the suffix
    date, = File.basename(fpath).split("_#{LOG_FILE_SUFFIX}")

    time_ms = Time.strptime(date, TimeHelper::TIME_FORMAT_FILE_PREFIX).to_i

    if direction == :asc
      time_ms
    else
      -time_ms
    end
  end
end