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

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

#clearOllamaChat::LinksSet

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

Returns:



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

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.



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

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:



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

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.



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

def empty?
  @set.empty?
end

#sizeInteger

Returns the number of elements in the set.

Returns:

  • (Integer)

    the size of the set



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

def size
  @set.size
end

#syncOllamaChat::LinksSet

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

Returns:



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

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