Module: Ucode::Cache

Defined in:
lib/ucode/cache.rb

Overview

On-disk cache layout for fetched UCD sources and derived indices.

Pure filesystem module. No network access, no parsing. Reads Ucode.configuration.cache_root for the root path so tests can swap roots without touching ENV.

Layout per version:

<root>/<version>/
  ucd/              # extracted UCD.zip
  unihan/           # extracted Unihan.zip
  pdfs/             # per-block PDFs
  index/            # blocks.yml, scripts.yml (YAML bsearch index)
  sqlite/           # ucode.sqlite3 (primary lookup)

Class Method Summary collapse

Class Method Details

.blocks_index_path(version) ⇒ Object



70
71
72
# File 'lib/ucode/cache.rb', line 70

def blocks_index_path(version)
  index_dir(version).join(BLOCKS_INDEX_FILENAME)
end

.cached?(version) ⇒ Boolean

True if any extracted content exists for ‘version`.

Parameters:

  • version (String)

Returns:

  • (Boolean)


81
82
83
# File 'lib/ucode/cache.rb', line 81

def cached?(version)
  version_dir(version).directory?
end

.cached_versionsArray<String>

All versions present in the cache, sorted ascending.

Returns:

  • (Array<String>)


87
88
89
90
91
# File 'lib/ucode/cache.rb', line 87

def cached_versions
  return [] unless root.directory?

  root.children.select(&:directory?).map { |p| p.basename.to_s }.sort
end

.ensure_version_dir!(version) ⇒ void

This method returns an undefined value.

Idempotent: create the per-version subdirectory tree.

Parameters:

  • version (String)


96
97
98
99
100
101
102
# File 'lib/ucode/cache.rb', line 96

def ensure_version_dir!(version)
  ucd_dir(version).mkpath
  unihan_dir(version).mkpath
  pdfs_dir(version).mkpath
  index_dir(version).mkpath
  sqlite_dir(version).mkpath
end

.index_dir(version) ⇒ Object



58
59
60
# File 'lib/ucode/cache.rb', line 58

def index_dir(version)
  version_dir(version).join(INDEX_DIR)
end

.pdfs_dir(version) ⇒ Object



54
55
56
# File 'lib/ucode/cache.rb', line 54

def pdfs_dir(version)
  version_dir(version).join(PDFS_DIR)
end

.remove_version(version) ⇒ void

This method returns an undefined value.

Remove a version from the cache. No-op if absent.

Parameters:

  • version (String)


107
108
109
110
# File 'lib/ucode/cache.rb', line 107

def remove_version(version)
  dir = version_dir(version)
  dir.rmtree if dir.exist?
end

.rootPathname

Returns:

  • (Pathname)


36
37
38
# File 'lib/ucode/cache.rb', line 36

def root
  Ucode.configuration.cache_root
end

.scripts_index_path(version) ⇒ Object



74
75
76
# File 'lib/ucode/cache.rb', line 74

def scripts_index_path(version)
  index_dir(version).join(SCRIPTS_INDEX_FILENAME)
end

.sqlite_dir(version) ⇒ Object



62
63
64
# File 'lib/ucode/cache.rb', line 62

def sqlite_dir(version)
  version_dir(version).join(SQLITE_DIR)
end

.sqlite_path(version) ⇒ Object



66
67
68
# File 'lib/ucode/cache.rb', line 66

def sqlite_path(version)
  sqlite_dir(version).join(SQLITE_FILENAME)
end

.ucd_dir(version) ⇒ Object



46
47
48
# File 'lib/ucode/cache.rb', line 46

def ucd_dir(version)
  version_dir(version).join(UCD_DIR)
end

.unihan_dir(version) ⇒ Object



50
51
52
# File 'lib/ucode/cache.rb', line 50

def unihan_dir(version)
  version_dir(version).join(UNIHAN_DIR)
end

.version_dir(version) ⇒ Pathname

Parameters:

  • version (String)

    e.g. “17.0.0”

Returns:

  • (Pathname)


42
43
44
# File 'lib/ucode/cache.rb', line 42

def version_dir(version)
  root.join(version)
end