Class: Worklog::ProjectStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/project_storage.rb

Overview

ProjectStorage is responsible for loading and managing project data. It provides methods to load projects from a YAML file and check if a project exists.

Handles storage operations for projects.

Constant Summary collapse

PROJECT_TEMPLATE =
<<~YAML
  # Each project is defined by the following attributes:
  # - key: <project_key>
  #   name: <project_name>
  #   description: <project_description>
  #   start_date: <start_date>
  #   end_date: <end_date>
  #   status: <status>
  #   --- Define your projects below this line ---
YAML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ ProjectStorage

Constructs a new ProjectStorage instance.

Parameters:



33
34
35
# File 'lib/project_storage.rb', line 33

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#projectsObject



37
38
39
# File 'lib/project_storage.rb', line 37

def projects
  @projects ||= load_projects
end

Instance Method Details

#exist?(handle) ⇒ Boolean

Check if a project with a given handle exists.

Parameters:

  • handle (String)

    The handle of the project to check.

Returns:

  • (Boolean)

    Returns true if the project exists, false otherwise.



60
61
62
63
# File 'lib/project_storage.rb', line 60

def exist?(handle)
  projects = load_projects
  projects.key?(handle)
end

#load_projectsHash<String, Project>

Loads all projects from disk. If the file does not exist, it creates a template.

Returns:

  • (Hash<String, Project>)

    A hash of project objects keyed by their project keys.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/project_storage.rb', line 44

def load_projects
  create_template unless file_exist?

  file_path = File.join(@configuration.storage_path, 'projects.yml')
  projects = {}
  YAML.load_file(file_path, permitted_classes: [Date])&.each do |project_hash|
    project = Project.from_hash(project_hash)
    projects[project.key] = project if project
  end

  projects
end