Class: Cline::Skill

Inherits:
Object
  • Object
show all
Includes:
Cline::Serializable::Dir
Defined in:
lib/cline/skill.rb

Overview

A skill defined in a directory

Instance Attribute Summary

Attributes included from Cline::Serializable::Dir

#create, #dir

Internal collapse

Methods included from Cline::Serializable::Dir

included, #initialize_from_dir, #subpath

Instance Method Summary collapse

Constructor Details

#initializeSkill

Constructor



89
90
91
92
# File 'lib/cline/skill.rb', line 89

def initialize
  @files = {}
  @files_discovered = false
end

Instance Method Details

#==(other) ⇒ Boolean

Equality check

Parameters:

  • other (Object)

    The other to check equality with

Returns:

  • (Boolean)

    True if objects are equal



31
32
33
34
35
# File 'lib/cline/skill.rb', line 31

def ==(other)
  other.is_a?(Skill) &&
    other.name == name &&
    other.files.compact == files.compact
end

#disableObject

Disable the skill



78
79
80
81
82
83
84
# File 'lib/cline/skill.rb', line 78

def disable
  return unless enabled?

  modify_skill_front_matter do |front_matter|
    front_matter.merge('disabled' => true)
  end
end

#enableObject

Enable the skill



69
70
71
72
73
74
75
# File 'lib/cline/skill.rb', line 69

def enable
  return if enabled?

  modify_skill_front_matter do |front_matter|
    front_matter.except('disabled')
  end
end

#enabled?Boolean

Returns Is the skill enabled?.

Returns:

  • (Boolean)

    Is the skill enabled?



64
65
66
# File 'lib/cline/skill.rb', line 64

def enabled?
  !yaml_front_matter || yaml_front_matter[:disabled] != true
end

#filesHash{String => FileContent, nil}

Get the files of this skill

Returns:



40
41
42
43
# File 'lib/cline/skill.rb', line 40

def files
  discover_files
  @files
end

#nameString

Get the skill's name

Returns:

  • (String)

    Skill name



15
16
17
# File 'lib/cline/skill.rb', line 15

def name
  File.basename(dir)
end

#saveObject

Save the skill files



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cline/skill.rb', line 46

def save
  raise 'This instance has not been initialized from a Skill directory' unless dir

  # First create/update all known files
  files.each do |file_path, file_content|
    next unless file_content

    file_full_path = File.join(dir, file_path)
    FileUtils.mkdir_p(File.dirname(file_full_path))
    File.write(file_full_path, file_content.content)
  end
  # Then delete any file that is not known
  each_file do |file_path|
    File.unlink(File.join(dir, file_path)) unless files[file_path]
  end
end

#yaml_front_matterHash{Symbol => Object}?

Get the skill's YAML front matter

Returns:

  • (Hash{Symbol => Object}, nil)

    The skill's front matter, or nil if none



22
23
24
25
# File 'lib/cline/skill.rb', line 22

def yaml_front_matter
  content = skill_file_content('SKILL.md')&.content
  JSON.parse(JSON[FrontMatterParser::Parser.new(:md).call(content).front_matter], symbolize_names: true) if content
end