Class: Rubydex::SkillRegistry
- Inherits:
-
Object
- Object
- Rubydex::SkillRegistry
- Defined in:
- lib/rubydex/skill_registry.rb
Overview
The skills available on disk, keyed by id. load scans a directory for <id>/SKILL.md entries
and records their paths.
A skill file is only read when #fetch asks for it. Listing every description reads all skill files.
The directory name is the id: it is what rdx skill <id> takes as a parameter.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch(id) ⇒ Object
- #ids ⇒ Object
-
#initialize(paths) ⇒ SkillRegistry
constructor
A new instance of SkillRegistry.
Constructor Details
#initialize(paths) ⇒ SkillRegistry
Returns a new instance of SkillRegistry.
34 35 36 37 38 39 40 |
# File 'lib/rubydex/skill_registry.rb', line 34 def initialize(paths) @paths = paths.freeze # Deliberately mutable inside a frozen registry: `freeze` protects the set of skills, while # the cache only remembers what was already read from those files. @cache = {} #: Hash[String, Skill] freeze end |
Class Method Details
.load(directory) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubydex/skill_registry.rb', line 14 def load(directory) unless File.directory?(directory) raise UnknownSkillDirectoryError, "Skill directory does not exist: #{directory}" end paths = {} Dir.foreach(directory) do |entry| next if entry == "." || entry == ".." skill_path = File.join(directory, entry, "SKILL.md") next unless File.file?(skill_path) paths[entry] = skill_path end new(paths) end |
Instance Method Details
#fetch(id) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubydex/skill_registry.rb', line 48 def fetch(id) @cache[id] ||= begin path = @paths[id] raise UnknownSkillError, "Unknown skill: #{id}" unless path skill = Skill.load(path) unless skill.name == id raise SkillError, "Skill in #{path} declares `name: #{skill.name}` but lives in `#{id}`" end skill end end |
#ids ⇒ Object
43 44 45 |
# File 'lib/rubydex/skill_registry.rb', line 43 def ids @paths.keys.sort end |