Class: RosettAi::Thor::Tasks::BehaviourManage

Inherits:
Thor
  • Object
show all
Defined in:
lib/rosett_ai/thor/tasks/behaviour.rb

Overview

Subcommand for managing behaviour files Must be defined before Behaviour class that references it

Instance Method Summary collapse

Instance Method Details

#add(name) ⇒ void

This method returns an undefined value.

Add a new entry.

Parameters:

  • name (Object)

    the name



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rosett_ai/thor/tasks/behaviour.rb', line 44

def add(name)
  name = RosettAi::TextSanitizer.normalize_nfc(name)
  return if behaviour_exists?(name)

  description = options[:description] || RosettAi::Ui::TtyHelper.ask('Description:', option_hint: 'description')
  temp_file, temp_dir = create_temp_behaviour(name, description, options[:author])

  open_in_editor(temp_file)
  return cleanup_temp(temp_dir) unless valid_behaviour?(temp_file)

  save_behaviour(temp_file, name, temp_dir)
end

#delete(name) ⇒ Object

Raises:

  • (::Thor::Error)


98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rosett_ai/thor/tasks/behaviour.rb', line 98

def delete(name)
  name = RosettAi::TextSanitizer.normalize_nfc(name)
  file_path = find_behaviour_file(name)
  raise ::Thor::Error, Rainbow(t('not_found', name: name)).red unless file_path

  display_usages(name)
  return unless deletion_confirmed?(name)

  File.delete(file_path)
  cleanup_compiled_rules(name)
  puts Rainbow(t('deleted', name: name)).green
end

#modify(name) ⇒ Object

Raises:

  • (::Thor::Error)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rosett_ai/thor/tasks/behaviour.rb', line 67

def modify(name)
  name = RosettAi::TextSanitizer.normalize_nfc(name)
  file_path = find_behaviour_file(name)
  raise ::Thor::Error, Rainbow(t('not_found', name: name)).red unless file_path

  temp_file, temp_dir = create_temp_copy(file_path)
  (temp_file)

  open_in_editor(temp_file)
  return cleanup_temp(temp_dir) unless valid_behaviour?(temp_file)

  FileUtils.cp(temp_file, file_path)
  cleanup_temp(temp_dir)
  puts Rainbow(t('updated', name: name)).green
end