Class: Worklog::ProjectStorage
- Inherits:
-
Object
- Object
- Worklog::ProjectStorage
- 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
- FILE_NAME =
The name of the YAML file where projects are stored.
'projects.yaml'
- PROJECT_TEMPLATE =
The template for the projects YAML file. This template is used to create a new projects file if it does not exist.
<<~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
-
#exist?(key) ⇒ Boolean
Check if a project with a given key exists.
-
#initialize(configuration) ⇒ ProjectStorage
constructor
Constructs a new ProjectStorage instance.
-
#load_projects ⇒ Hash<String, Project>
Loads all projects from disk.
Constructor Details
#initialize(configuration) ⇒ ProjectStorage
Constructs a new ProjectStorage instance.
40 41 42 |
# File 'lib/project_storage.rb', line 40 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
#projects ⇒ Object
44 45 46 |
# File 'lib/project_storage.rb', line 44 def projects @projects ||= load_projects end |
Instance Method Details
#exist?(key) ⇒ Boolean
Note:
This method loads the projects from disk if they are not already loaded.
Check if a project with a given key exists.
69 70 71 72 |
# File 'lib/project_storage.rb', line 69 def exist?(key) projects = load_projects projects.key?(key) end |
#load_projects ⇒ Hash<String, Project>
Loads all projects from disk. If the file does not exist, it creates a template.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/project_storage.rb', line 51 def load_projects create_template unless file_exist? file_path = File.join(@configuration.storage_path, FILE_NAME) 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 |