Class: Worklog::PeopleStorage
- Inherits:
-
Object
- Object
- Worklog::PeopleStorage
- Defined in:
- lib/people_storage.rb
Overview
Handles storage of people
Constant Summary collapse
- PEOPLE_FILE =
'people.yaml'- PERSON_TEMPLATE =
The template for the people YAML file. This template is used to create a new people file if it does not exist.
<<~YAML --- # Each person is defined by the following attributes: # - handle: <unique_handle> # github_username: <github_username> # name: <full_name> # team: <team_name> # email: <email_address> # title: <title_or_role> # inactive: <true_or_false> # --- Define your people below this line --- YAML
Instance Method Summary collapse
-
#create_default_file ⇒ void
Create the default people file if it does not exist.
-
#find_by_github_username(github_username) ⇒ Person?
Find a person by their GitHub username.
-
#find_by_handle(handle) ⇒ Person?
Find a person by their handle.
-
#initialize(config) ⇒ PeopleStorage
constructor
A new instance of PeopleStorage.
-
#load_people ⇒ Array<Person>
Load all people from the people YAML file Return empty array if file does not exist.
-
#load_people! ⇒ Array<Person>
Load all people from the people YAML file.
-
#load_people_hash ⇒ Hash<String, Person>
Load all people from the people file and return them as a hash with handle as key.
-
#people_filepath ⇒ String
Return the full absolute filepath for the people.yaml file.
-
#write_people!(people) ⇒ Object
Write people to the people file.
Constructor Details
#initialize(config) ⇒ PeopleStorage
Returns a new instance of PeopleStorage.
26 27 28 |
# File 'lib/people_storage.rb', line 26 def initialize(config) @config = config end |
Instance Method Details
#create_default_file ⇒ void
This method returns an undefined value.
Create the default people file if it does not exist
86 87 88 89 90 91 92 93 |
# File 'lib/people_storage.rb', line 86 def create_default_file if File.exist?(people_filepath) WorkLogger.info 'people.yaml already exists, skipping creation.' else WorkLogger.info 'Creating default people.yaml file.' File.write(people_filepath, PERSON_TEMPLATE) end end |
#find_by_github_username(github_username) ⇒ Person?
Find a person by their GitHub username
106 107 108 109 |
# File 'lib/people_storage.rb', line 106 def find_by_github_username(github_username) @people ||= load_people @people.find { |person| person.github_username == github_username } end |
#find_by_handle(handle) ⇒ Person?
Find a person by their handle
98 99 100 101 |
# File 'lib/people_storage.rb', line 98 def find_by_handle(handle) @people ||= load_people @people.find { |person| person.handle == handle } end |
#load_people ⇒ Array<Person>
Load all people from the people YAML file Return empty array if file does not exist
45 46 47 48 49 50 51 |
# File 'lib/people_storage.rb', line 45 def load_people load_people! rescue Errno::ENOENT # If the file does not exist, create it with the template File.write(people_filepath, PERSON_TEMPLATE) [] end |
#load_people! ⇒ Array<Person>
Load all people from the people YAML file
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/people_storage.rb', line 56 def load_people! # TODO: Remove this migration code in future versions # This was introduced in v0.2.26 (Oct 2 2025) to fix deprecated YAML syntax yamltext = File.read(people_filepath) if yamltext != yamltext.gsub(/^- !.*$/, '-') WorkLogger.debug 'The people.yaml file contains deprecated syntax. Migrating now.' yamltext.gsub!(/^- !.*$/, '-') File.write(people_filepath, yamltext) end # End TODO data = YAML.load(yamltext, permitted_classes: []) return [] unless data.is_a?(Array) data.map { |person_hash| Person.from_hash(person_hash) } end |
#load_people_hash ⇒ Hash<String, Person>
Load all people from the people file and return them as a hash with handle as key
38 39 40 |
# File 'lib/people_storage.rb', line 38 def load_people_hash load_people.to_h { |person| [person.handle, person] } end |
#people_filepath ⇒ String
Return the full absolute filepath for the people.yaml file
32 33 34 |
# File 'lib/people_storage.rb', line 32 def people_filepath File.join(@config.storage_path, PEOPLE_FILE) end |
#write_people!(people) ⇒ Object
Write people to the people file
75 76 77 78 79 80 81 82 |
# File 'lib/people_storage.rb', line 75 def write_people!(people) raise ArgumentError, 'people must be an array of Person objects' if people.nil? || !people.is_a?(Array) File.open(people_filepath, 'w') do |f| f.puts people.to_yaml end @people = people end |