Class: Uniword::Validation::Checkers::FileReferenceChecker

Inherits:
LinkChecker
  • Object
show all
Defined in:
lib/uniword/validation/checkers/file_reference_checker.rb

Overview

Validates file reference links.

Responsibility: Validate referenced files exist on filesystem. Single Responsibility: File reference validation only.

Verifies:

  • Referenced files exist

  • File paths are valid

  • Relative and absolute paths (configurable)

Configuration options:

  • base_path: Base directory for relative paths

  • check_relative_paths: Whether to check relative file paths

  • check_absolute_paths: Whether to check absolute file paths

Examples:

Create and use checker

checker = FileReferenceChecker.new(config: {
  base_path: '/documents'
})
result = checker.check(file_link, document)

Constant Summary collapse

DEFAULTS =

Default configuration values

{
  base_path: ".",
  check_relative_paths: true,
  check_absolute_paths: true,
}.freeze

Instance Attribute Summary

Attributes inherited from LinkChecker

#config

Instance Method Summary collapse

Methods inherited from LinkChecker

#initialize

Constructor Details

This class inherits a constructor from Uniword::Validation::LinkChecker

Instance Method Details

#can_check?(link) ⇒ Boolean

Check if this checker can validate the given link.

Examples:

checker.can_check?(file_link) # => true

Parameters:

  • link (Object)

    The link to check

Returns:

  • (Boolean)

    true if link is a file reference



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/uniword/validation/checkers/file_reference_checker.rb', line 44

def can_check?(link)
  return false unless enabled?

  # Check if link is a file path (string or has path attribute)
  if link.is_a?(String)
    looks_like_file_path?(link)
  elsif link.respond_to?(:url)
    url = link.url
    url && looks_like_file_path?(url)
  elsif link.respond_to?(:path)
    true
  else
    false
  end
end

#check(link, document = nil) ⇒ ValidationResult

Validate the file reference.

Examples:

result = checker.check(file_link)

Parameters:

  • link (Object)

    The link to validate

  • document (Object) (defaults to: nil)

    The document (used for context)

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/uniword/validation/checkers/file_reference_checker.rb', line 68

def check(link, document = nil)
  unless enabled?
    return ValidationResult.unknown(link,
                                    "Checker disabled")
  end

  file_path = extract_file_path(link)
  unless file_path
    return ValidationResult.failure(link,
                                    "No file path specified")
  end

  # Resolve the file path
  resolved_path = resolve_path(file_path, document)

  # Check if path type is enabled
  if resolved_path.absolute?
    unless config_value(:check_absolute_paths,
                        DEFAULTS[:check_absolute_paths])
      return ValidationResult.warning(
        link,
        "Absolute path checking disabled",
      )
    end
  else
    unless config_value(:check_relative_paths,
                        DEFAULTS[:check_relative_paths])
      return ValidationResult.warning(
        link,
        "Relative path checking disabled",
      )
    end
  end

  # Check if file exists
  if File.exist?(resolved_path)
     = {
      path: file_path,
      resolved_path: resolved_path.to_s,
      type: File.directory?(resolved_path) ? "directory" : "file",
    }

    ValidationResult.success(link, metadata: )
  else
    ValidationResult.failure(
      link,
      "File not found: #{file_path}",
      metadata: {
        path: file_path,
        resolved_path: resolved_path.to_s,
      },
    )
  end
rescue StandardError => e
  ValidationResult.failure(
    link,
    "Error checking file: #{e.message}",
    metadata: { error: e.class.name },
  )
end