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
- 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
-
#exist?(handle) ⇒ Boolean
Check if a project with a given handle 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.
33 34 35 |
# File 'lib/project_storage.rb', line 33 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
#projects ⇒ Object
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.
60 61 62 63 |
# File 'lib/project_storage.rb', line 60 def exist?(handle) projects = load_projects projects.key?(handle) end |
#load_projects ⇒ Hash<String, Project>
Loads all projects from disk. If the file does not exist, it creates a template.
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 |