Class: Worklog::PeopleStorage
- Inherits:
-
Object
- Object
- Worklog::PeopleStorage
- Defined in:
- lib/people_storage.rb
Overview
Finding, reading, and writing 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> # Unique handle used to reference this person (e.g., ~jdoe) # github_username: <github_username> # GitHub username of the person, used to link GitHub events to this person. # This can be omitted if the person does not have a GitHub account and can # be different from the handle. # 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.
33 34 35 |
# File 'lib/people_storage.rb', line 33 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
93 94 95 96 97 98 99 100 |
# File 'lib/people_storage.rb', line 93 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
113 114 115 116 |
# File 'lib/people_storage.rb', line 113 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
105 106 107 108 |
# File 'lib/people_storage.rb', line 105 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
52 53 54 55 56 57 58 |
# File 'lib/people_storage.rb', line 52 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/people_storage.rb', line 63 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
45 46 47 |
# File 'lib/people_storage.rb', line 45 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
39 40 41 |
# File 'lib/people_storage.rb', line 39 def people_filepath File.join(@config.storage_path, PEOPLE_FILE) end |
#write_people!(people) ⇒ Object
Write people to the people file
82 83 84 85 86 87 88 89 |
# File 'lib/people_storage.rb', line 82 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 |