Module: OllamaChat::FavouritesManagement

Included in:
Chat
Defined in:
lib/ollama_chat/favourites_management.rb

Overview

A module that handles favourite operations for OllamaChat.

This module allows users to mark specific entities as favourites and retrieve those favourites for easy access and display in the UI.

Instance Method Summary collapse

Instance Method Details

#add_favourite(type) ⇒ Object (private)

Note:

This method uses a chooser to present options and handles user input for adding new favourites to the database.

The add_favourite method adds a favourite item of a specified type. It iterates through available favourites and allows the user to select from items that haven't been favourited yet.

Parameters:

  • type (String)

    the context type for the favourite



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ollama_chat/favourites_management.rb', line 43

def add_favourite(type)
  all_things = favourite_all_things(type)
  choose_with_state do
    loop do
      selected = models::Favourite.where(context: type).map(&:name)
      to_select = all_things - selected
      if to_select.empty?
        STDOUT.puts "All items are already favourited."
        return
      end
      to_select.unshift('[EXIT]')
      case chosen = choose_entry(to_select)
      when '[EXIT]', nil
        STDOUT.puts "Cancelled."
        return
      when SearchUI::Wrapper
        models::Favourite.create(context: type, name: chosen.value)
      end
    end
  end
end

#all_favourited(type) ⇒ Hash (private)

The all_favourited method retrieves all favourited items of a specified type.

Parameters:

  • type (String)

    the type of favourited items to retrieve

Returns:

  • (Hash)

    a hash with favourited item names as keys and true as values



97
98
99
100
# File 'lib/ollama_chat/favourites_management.rb', line 97

def all_favourited(type)
  models::Favourite.where(context: type).
    each_with_object(Hash.new(false)) { |fav, h| h[fav.name] = true }
end

#delete_favourite(type) ⇒ Object (private)

Note:

This method uses a chooser to present options and handles user input for removing favourites from the database.

The delete_favourite method removes favourite items from the database. It iterates through current favourites and allows the user to select items for removal in a loop, maintaining search state between selections.

Parameters:

  • type (String)

    the context type of the favourite to delete



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ollama_chat/favourites_management.rb', line 73

def delete_favourite(type)
  all_things = favourite_all_things(type)
  choose_with_state do
    loop do
      to_select = models::Favourite.where(context: type).map(&:name)
      to_select = all_things.select { to_select.member?(_1.value) }
      to_select = [ '[EXIT]' ] + to_select
      case chosen = choose_entry(to_select)
      when '[EXIT]', nil
        STDOUT.puts "Cancelled."
        return
      when SearchUI::Wrapper
        models::Favourite.where(context: type, name: chosen.value).destroy
      end
    end
  end
end

#favourite_all_things(type) ⇒ Array<SearchUI::Wrapper> (private)

Retrieves a UI-ready list of all available entities of a given type, decorated with a heart icon if they are marked as favourites.

Parameters:

  • type (String)

    the context type (e.g., 'model', 'prompt', 'system_prompt', 'persona')

Returns:

  • (Array<SearchUI::Wrapper>)

    a list of wrappers containing the original value and the decorated display string.



24
25
26
27
28
29
30
31
32
33
# File 'lib/ollama_chat/favourites_management.rb', line 24

def favourite_all_things(type)
  case type
  when 'model'         then all_models
  when 'prompt'        then all_prompts
  when 'system_prompt' then all_system_prompts
  when 'persona'       then available_personae_names
  else
    raise ArgumentError, "not all things defined for type #{type.inspect}"
  end.to_a
end

#prefix_favourite(string, favourited) ⇒ String (private)

Prepend a heart icon to a string if it is marked as a favourite.

Parameters:

  • string (String)

    the string to decorate

  • favourited (Boolean)

    whether the item is a favourite

Returns:

  • (String)

    the decorated string



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

def prefix_favourite(string, favourited)
  fav = favourited ? '❤️' : '🩶'
  "%s %s" % [ fav, string ]
end