Class: TurnKit::Skill
- Inherits:
-
Object
- Object
- TurnKit::Skill
- Defined in:
- lib/turnkit/skill.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .from_directory(path, pattern: "*.md") ⇒ Object
- .from_file(path, key: nil, name: nil, description: "") ⇒ Object
- .parse_file(content) ⇒ Object
Instance Method Summary collapse
-
#initialize(key:, name:, content:, description: "") ⇒ Skill
constructor
A new instance of Skill.
Constructor Details
#initialize(key:, name:, content:, description: "") ⇒ Skill
Returns a new instance of Skill.
19 20 21 22 23 24 25 26 27 |
# File 'lib/turnkit/skill.rb', line 19 def initialize(key:, name:, content:, description: "") @key = key.to_s @name = name.to_s @description = description.to_s @content = content.to_s raise ArgumentError, "key is required" if @key.empty? raise ArgumentError, "name is required" if @name.empty? raise ArgumentError, "content is required" if @content.empty? end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
7 8 9 |
# File 'lib/turnkit/skill.rb', line 7 def content @content end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
7 8 9 |
# File 'lib/turnkit/skill.rb', line 7 def description @description end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/turnkit/skill.rb', line 7 def key @key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/turnkit/skill.rb', line 7 def name @name end |
Class Method Details
.from_directory(path, pattern: "*.md") ⇒ Object
15 16 17 |
# File 'lib/turnkit/skill.rb', line 15 def self.from_directory(path, pattern: "*.md") Dir.glob(File.join(path, pattern)).sort.map { |file| from_file(file) } end |
.from_file(path, key: nil, name: nil, description: "") ⇒ Object
9 10 11 12 13 |
# File 'lib/turnkit/skill.rb', line 9 def self.from_file(path, key: nil, name: nil, description: "") content, = parse_file(File.read(path)) base = File.basename(path, File.extname(path)) new(key: key || base, name: name || ["name"] || base.tr("_-", " ").split.map(&:capitalize).join(" "), description: description.to_s.empty? ? ["description"].to_s : description, content: content) end |
.parse_file(content) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/turnkit/skill.rb', line 29 def self.parse_file(content) text = content.to_s return [ text, {} ] unless text.start_with?("---\n") _, frontmatter, body = text.split(/^---\s*$/, 3) return [ text, {} ] unless body [ body.sub(/\A\n/, ""), YAML.safe_load(frontmatter, permitted_classes: [ Symbol ], aliases: false) || {} ] rescue Psych::SyntaxError [ text, {} ] end |