Class: Jambots::Conversation
- Inherits:
-
Object
- Object
- Jambots::Conversation
- Defined in:
- lib/jambots/conversation.rb
Instance Attribute Summary collapse
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#key ⇒ Object
Returns the value of attribute key.
-
#messages ⇒ Object
Returns the value of attribute messages.
Instance Method Summary collapse
- #add_message(role, content) ⇒ Object
- #delete ⇒ Object
-
#initialize(file_path) ⇒ Conversation
constructor
A new instance of Conversation.
- #load_messages ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(file_path) ⇒ Conversation
Returns a new instance of Conversation.
7 8 9 10 11 12 |
# File 'lib/jambots/conversation.rb', line 7 def initialize(file_path) @file_path = file_path @file_name = File.basename(file_path) @key = File.basename(file_name, File.extname(file_name)) @messages = end |
Instance Attribute Details
#file_name ⇒ Object
Returns the value of attribute file_name.
5 6 7 |
# File 'lib/jambots/conversation.rb', line 5 def file_name @file_name end |
#file_path ⇒ Object
Returns the value of attribute file_path.
5 6 7 |
# File 'lib/jambots/conversation.rb', line 5 def file_path @file_path end |
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'lib/jambots/conversation.rb', line 5 def key @key end |
#messages ⇒ Object
Returns the value of attribute messages.
5 6 7 |
# File 'lib/jambots/conversation.rb', line 5 def @messages end |
Instance Method Details
#add_message(role, content) ⇒ Object
14 15 16 |
# File 'lib/jambots/conversation.rb', line 14 def (role, content) @messages << {role: role, content: content} end |
#delete ⇒ Object
31 32 33 |
# File 'lib/jambots/conversation.rb', line 31 def delete File.delete(@file_path) if File.exist?(@file_path) end |
#load_messages ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/jambots/conversation.rb', line 23 def return [] unless File.exist?(@file_path) file_content = File.read(@file_path) YAML.safe_load(file_content, permitted_classes: [Symbol], symbolize_names: true) end |
#save ⇒ Object
18 19 20 21 |
# File 'lib/jambots/conversation.rb', line 18 def save = @messages.map { || .transform_keys(&:to_s) } File.write(@file_path, .to_yaml) end |