Class: OllamaChat::LinksSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ollama_chat/links_set.rb

Overview

A collection of links associated with a chat session, providing automatic synchronization to the session's persistent storage.

This class acts as a wrapper around a Set, ensuring that any mutations trigger a sync operation to save the current state to the database.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(chat) ⇒ LinksSet

Initializes a new LinksSet instance and loads existing links from the session.

Parameters:

  • chat (Object) —

    The chat instance used for persistence.



13
14
15
16
# File 'lib/ollama_chat/links_set.rb', line 13

def initialize(chat)
  @chat = chat
  @set  = Set.new(@chat.load_links_from_session)
end

Instance Method Details

#add(x) ⇒ OllamaChat::LinksSet Also known as: <<

Adds a link to the set and synchronizes it with the session.

Parameters:

  • x (String) —

    The link to add.

Returns:



22
23
24
25
# File 'lib/ollama_chat/links_set.rb', line 22

def add(x)
  @set.add(x)
  sync
end

#clear ⇒ OllamaChat::LinksSet

Removes all links from the set and synchronizes it with the session.

Returns:



31
32
33
34
# File 'lib/ollama_chat/links_set.rb', line 31

def clear
  @set.clear
  sync
end

#delete(x) ⇒ String?

Removes a specific link from the set and synchronizes it with the session.

Parameters:

  • x (String) —

    The link to delete.

Returns:

  • (String, nil) —

    The deleted link, or nil if it was not found.



40
41
42
# File 'lib/ollama_chat/links_set.rb', line 40

def delete(x)
  @set.delete(x).tap { sync }
end

#each {|String| ... } ⇒ OllamaChat::LinksSet

Iterates over the links in the set.

Yields:

  • (String) —

    The current link in the iteration.

Returns:



69
70
71
72
# File 'lib/ollama_chat/links_set.rb', line 69

def each(&block)
  @set.each(&block)
  self
end

#empty? ⇒ Boolean

Checks if the set of elements is empty.

Returns:

  • (Boolean) —

    true if the set contains no elements, false otherwise.



55
56
57
# File 'lib/ollama_chat/links_set.rb', line 55

def empty?
  @set.empty?
end

#size ⇒ Integer

Returns the number of elements in the set.

Returns:

  • (Integer) —

    the size of the set



61
62
63
# File 'lib/ollama_chat/links_set.rb', line 61

def size
  @set.size
end

#sync ⇒ OllamaChat::LinksSet

Synchronizes the current in-memory set of links with the chat session.

Returns:



47
48
49
50
# File 'lib/ollama_chat/links_set.rb', line 47

def sync
  @chat.store_links_in_session(@set)
  self
end