Class: Worklog::PeopleStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/people_storage.rb

Overview

Finding, reading, and writing of people

See Also:

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

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_filevoid

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

Parameters:

  • github_username (String)

    The GitHub username of the person

Returns:

  • (Person, nil)

    The person if found, nil otherwise



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

Parameters:

  • handle (String)

    The handle of the person

Returns:

  • (Person, nil)

    The person if found, nil otherwise



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_peopleArray<Person>

Load all people from the people YAML file Return empty array if file does not exist

Returns:

  • (Array<Person>)

    List of all people



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

Returns:

  • (Array<Person>)

    List of all people

Raises:

  • (Errno::ENOENT)

    if the people file does not exist



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_hashHash<String, Person>

Load all people from the people file and return them as a hash with handle as key

Returns:

  • (Hash<String, Person>)

    Hash of people 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_filepathString

Return the full absolute filepath for the people.yaml file

Returns:

  • (String)

    The filepath



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

Parameters:

  • people (Array<Person>)

    List of people

Raises:

  • (ArgumentError)


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