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> # repositories: # - <repository_url_1> # - <repository_url_2> # --- Define your projects below this line --- YAML
Instance Attribute Summary collapse
-
#projects ⇒ Hash<String, Project>
Returns all loaded projects.
Instance Method Summary collapse
-
#exist?(key) ⇒ Boolean
Check if a project with a given key exists.
-
#find_by(filters = {}) ⇒ Array<Project>
Find project by given filters.
-
#initialize(configuration) ⇒ ProjectStorage
constructor
Constructs a new ProjectStorage instance.
-
#key?(key) ⇒ Boolean
Alias for exist? method.
-
#load_projects ⇒ Hash<String, Project>
Loads all projects from disk.
Constructor Details
#initialize(configuration) ⇒ ProjectStorage
Constructs a new ProjectStorage instance.
43 44 45 |
# File 'lib/project_storage.rb', line 43 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
Instance Method Details
#exist?(key) ⇒ Boolean
This method loads the projects from disk if they are not already loaded.
Check if a project with a given key exists.
75 76 77 78 |
# File 'lib/project_storage.rb', line 75 def exist?(key) projects = load_projects projects.key?(key) end |
#find_by(filters = {}) ⇒ Array<Project>
Find project by given filters. Filters can be repository or
83 84 85 86 87 |
# File 'lib/project_storage.rb', line 83 def find_by(filters = {}) projects.values.select do |project| filters.all? { |k, v| project.respond_to?(k) && project.public_send(k) == v } end end |
#key?(key) ⇒ Boolean
Alias for exist? method.
92 |
# File 'lib/project_storage.rb', line 92 def key?(key) = exist?(key) |
#load_projects ⇒ Hash<String, Project>
Loads all projects from disk. If the file does not exist, it creates a template.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/project_storage.rb', line 57 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 |