Class: Ukiryu::Definition::Sources::FileSource

Inherits:
Ukiryu::Definition::Source show all
Defined in:
lib/ukiryu/definition/sources/file.rb

Overview

Load tool definitions from a file

This source reads YAML tool definitions from the filesystem. It tracks file metadata for cache invalidation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ukiryu::Definition::Source

#==, #hash, #inspect, #to_s

Constructor Details

#initialize(path) ⇒ FileSource

Create a new file-based definition source

Parameters:

  • path (String)

    path to the definition file

Raises:



24
25
26
27
28
29
30
31
# File 'lib/ukiryu/definition/sources/file.rb', line 24

def initialize(path)
  @path = expand_path(path)
  @mtime = nil # Will be set on first load
  @cached_content = nil

  validate_file_exists!
  validate_file_readable!
end

Instance Attribute Details

#mtimeTime (readonly)

The file modification time

Returns:

  • (Time)

    file mtime



17
18
19
# File 'lib/ukiryu/definition/sources/file.rb', line 17

def mtime
  @mtime
end

#pathString (readonly)

The path to the definition file

Returns:

  • (String)

    absolute path to the file



13
14
15
# File 'lib/ukiryu/definition/sources/file.rb', line 13

def path
  @path
end

Instance Method Details

#cache_keyString

Get a unique cache key for this file source

The cache key includes a hash of the path and the mtime, ensuring that file changes invalidate the cache.

Returns:

  • (String)

    unique cache key



66
67
68
# File 'lib/ukiryu/definition/sources/file.rb', line 66

def cache_key
  "file:#{sha256(path)}:#{mtime || get_mtime}"
end

#loadString

Load the YAML definition content from the file

Returns:

  • (String)

    the YAML content

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ukiryu/definition/sources/file.rb', line 37

def load
  current_mtime = get_mtime

  # Check if file has changed since init
  if @mtime && @mtime != current_mtime
    raise Ukiryu::Errors::DefinitionLoadError,
          "File #{@path} has been modified since it was loaded. " \
          "Original mtime: #{@mtime}, Current mtime: #{current_mtime}"
  end

  @mtime = current_mtime

  # Read and cache content
  @load ||= read_file
end

#real_pathString

Get the real path (resolves symlinks)

Returns:

  • (String)

    real path to the file



73
74
75
76
77
# File 'lib/ukiryu/definition/sources/file.rb', line 73

def real_path
  @real_path ||= File.realpath(path)
rescue Errno::ENOENT
  path
end

#source_typeSymbol

Get the source type

Returns:

  • (Symbol)

    :file



56
57
58
# File 'lib/ukiryu/definition/sources/file.rb', line 56

def source_type
  :file
end