Class: RubynCode::Skills::Document
- Inherits:
-
Object
- Object
- RubynCode::Skills::Document
- Defined in:
- lib/rubyn_code/skills/document.rb
Constant Summary collapse
- FRONTMATTER_PATTERN =
/\A---\s*\n(.+?\n)---\s*\n(.*)\z/m- TAG_RULES =
[ ['ruby', /\bruby\b/i], ['rails', /\brails\b/i], ['rspec', /\brspec\b/i], ['testing', /\b(?:test|spec|minitest)\b/i], ['patterns', /\b(?:pattern|design|solid)\b/i], ['refactoring', /\brefactor/i] ].freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
- .parse(content, filename: nil) ⇒ Object
- .parse_file(path) ⇒ Object
- .parse_with_frontmatter(match) ⇒ Object
- .parse_without_frontmatter(content, filename) ⇒ Object
Instance Method Summary collapse
-
#initialize(name:, description:, tags:, body:) ⇒ Document
constructor
A new instance of Document.
Constructor Details
#initialize(name:, description:, tags:, body:) ⇒ Document
Returns a new instance of Document.
12 13 14 15 16 17 |
# File 'lib/rubyn_code/skills/document.rb', line 12 def initialize(name:, description:, tags:, body:) @name = name @description = description @tags = @body = body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
10 11 12 |
# File 'lib/rubyn_code/skills/document.rb', line 10 def body @body end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
10 11 12 |
# File 'lib/rubyn_code/skills/document.rb', line 10 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/rubyn_code/skills/document.rb', line 10 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
10 11 12 |
# File 'lib/rubyn_code/skills/document.rb', line 10 def @tags end |
Class Method Details
.parse(content, filename: nil) ⇒ Object
20 21 22 23 |
# File 'lib/rubyn_code/skills/document.rb', line 20 def parse(content, filename: nil) match = FRONTMATTER_PATTERN.match(content) match ? parse_with_frontmatter(match) : parse_without_frontmatter(content, filename) end |
.parse_file(path) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/rubyn_code/skills/document.rb', line 48 def parse_file(path) raise Error, "Skill file not found: #{path}" unless File.exist?(path) raise Error, "Not a file: #{path}" unless File.file?(path) content = File.read(path, encoding: 'UTF-8') parse(content, filename: path) end |
.parse_with_frontmatter(match) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/rubyn_code/skills/document.rb', line 25 def parse_with_frontmatter(match) frontmatter = YAML.safe_load(match[1], permitted_classes: [Symbol]) || {} new( name: frontmatter['name'].to_s, description: frontmatter['description'].to_s, tags: Array(frontmatter['tags']), body: match[2].to_s.strip ) end |
.parse_without_frontmatter(content, filename) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rubyn_code/skills/document.rb', line 35 def parse_without_frontmatter(content, filename) body = content.to_s.strip title = extract_title(body) derived_name = filename ? File.basename(filename, '.*').tr('_', '-') : title_to_name(title) new( name: derived_name, description: title, tags: (derived_name, body), body: body ) end |