Class: Fontist::InstallLocations::BaseLocation

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/install_locations/base_location.rb

Overview

Abstract base class for font installation locations

This class provides the foundation for all installation location types, implementing the core logic for:

  • Managed vs non-managed location detection

  • Font installation with duplicate prevention

  • Unique filename generation for non-managed locations

  • Educational warning messages

  • Index management integration

## Managed vs Non-Managed Locations

**Fontist-Managed Locations** (safe to replace fonts):

  • Fontist library: ~/.fontist/fonts/formula-key/

  • User default: ~/Library/Fonts/fontist/

  • System default: /Library/Fonts/fontist/

**Non-Managed Locations** (never replace existing fonts):

  • Custom user root: ~/Library/Fonts/ (when FONTIST_USER_FONTS_PATH=~/Library/Fonts)

  • Custom system root: /Library/Fonts/ (when FONTIST_SYSTEM_FONTS_PATH=/Library/Fonts)

## Subclass Requirements

Subclasses must implement:

  • base_path: Returns Pathname for installation directory

  • location_type: Returns Symbol (:fontist, :user, :system)

  • index: Returns index instance for this location

Direct Known Subclasses

FontistLocation, SystemLocation, UserLocation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formula) ⇒ BaseLocation

Returns a new instance of BaseLocation.



37
38
39
# File 'lib/fontist/install_locations/base_location.rb', line 37

def initialize(formula)
  @formula = formula
end

Instance Attribute Details

#formulaObject (readonly)

Returns the value of attribute formula.



35
36
37
# File 'lib/fontist/install_locations/base_location.rb', line 35

def formula
  @formula
end

Instance Method Details

#base_pathPathname

Returns the base installation path for this location

Returns:

  • (Pathname)

    Base installation directory

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/fontist/install_locations/base_location.rb', line 45

def base_path
  raise NotImplementedError, "#{self.class} must implement #base_path"
end

#find_fonts(font_name, style = nil) ⇒ Array<SystemIndexFont>?

Finds fonts by name and optional style at this location

Parameters:

  • font_name (String)

    Font family name

  • style (String, nil) (defaults to: nil)

    Optional style (e.g., “Regular”, “Bold”)

Returns:



123
124
125
# File 'lib/fontist/install_locations/base_location.rb', line 123

def find_fonts(font_name, style = nil)
  index.find(font_name, style)
end

#font_exists?(filename) ⇒ Boolean

Checks if a font exists at this location

Parameters:

  • filename (String)

    Font filename to check

Returns:

  • (Boolean)

    true if font exists



113
114
115
116
# File 'lib/fontist/install_locations/base_location.rb', line 113

def font_exists?(filename)
  path = font_path(filename).to_s
  index.font_exists?(path)
end

#font_path(filename) ⇒ Pathname

Returns full path for a font file

Parameters:

  • filename (String)

    The font filename

Returns:

  • (Pathname)

    Full path where font should be installed



60
61
62
# File 'lib/fontist/install_locations/base_location.rb', line 60

def font_path(filename)
  base_path.join(filename)
end

#install_font(source_path, target_filename) ⇒ String?

Installs a font file to this location

Handles managed vs non-managed logic:

  • Managed locations: Replace existing font if present

  • Non-managed locations: Generate unique name to avoid conflicts

Parameters:

  • source_path (String)

    Path to source font file

  • target_filename (String)

    Desired filename for font

Returns:

  • (String, nil)

    Installed path or nil if skipped



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fontist/install_locations/base_location.rb', line 73

def install_font(source_path, target_filename)
  target = font_path(target_filename)

  # Check if font already exists at this location
  if font_exists?(target_filename)
    if managed_location?
      # Safe to replace in managed locations
      replace_font(source_path, target)
    else
      # Non-managed: use unique name to avoid overwriting user/system fonts
      unique_filename = generate_unique_filename(target_filename)
      install_with_warning(source_path, unique_filename,
                           original_path: target)
    end
  else
    # New installation - simple case
    simple_install(source_path, target)
  end
end

#location_typeSymbol

Returns the location type identifier

Returns:

  • (Symbol)

    :fontist, :user, or :system

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/fontist/install_locations/base_location.rb', line 51

def location_type
  raise NotImplementedError, "#{self.class} must implement #location_type"
end

#permission_warningString?

Returns warning message for installations requiring permissions

Returns:

  • (String, nil)

    Warning message or nil



137
138
139
# File 'lib/fontist/install_locations/base_location.rb', line 137

def permission_warning
  nil
end

#requires_elevated_permissions?Boolean

Checks if this location requires elevated permissions

Returns:

  • (Boolean)

    true if sudo/admin rights needed



130
131
132
# File 'lib/fontist/install_locations/base_location.rb', line 130

def requires_elevated_permissions?
  false
end

#uninstall_font(filename) ⇒ String?

Uninstalls a font file from this location

Parameters:

  • filename (String)

    Font filename to remove

Returns:

  • (String, nil)

    Deleted path or nil if not found



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fontist/install_locations/base_location.rb', line 97

def uninstall_font(filename)
  target = font_path(filename)
  return nil unless File.exist?(target)

  File.delete(target)

  # Update this location's index
  index.remove_font(target.to_s)

  target.to_s
end