Class: Uniword::Validation::Checkers::FileReferenceChecker
- Inherits:
-
LinkChecker
- Object
- LinkChecker
- Uniword::Validation::Checkers::FileReferenceChecker
- 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
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
Instance Method Summary collapse
-
#can_check?(link) ⇒ Boolean
Check if this checker can validate the given link.
-
#check(link, document = nil) ⇒ ValidationResult
Validate the file reference.
Methods inherited from LinkChecker
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.
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.
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.}", metadata: { error: e.class.name }, ) end |