Class: RubynCode::Skills::Document

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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 = tags
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/rubyn_code/skills/document.rb', line 10

def body
  @body
end

#descriptionObject (readonly)

Returns the value of attribute description.



10
11
12
# File 'lib/rubyn_code/skills/document.rb', line 10

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/rubyn_code/skills/document.rb', line 10

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



10
11
12
# File 'lib/rubyn_code/skills/document.rb', line 10

def tags
  @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

Raises:



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: derive_tags(derived_name, body),
    body: body
  )
end