Class: Rhales::SchemaExtractor
- Inherits:
-
Object
- Object
- Rhales::SchemaExtractor
- Defined in:
- lib/rhales/utils/schema_extractor.rb
Overview
Extracts schema definitions from .rue files
This class scans template directories for .rue files containing
Usage: extractor = SchemaExtractor.new(‘./templates’) schemas = extractor.extract_all schemas.each do |schema_info| puts “#schema_info[:template_name]: #schema_info[:lang]” end
Defined Under Namespace
Classes: ExtractionError
Instance Attribute Summary collapse
-
#templates_dir ⇒ Object
readonly
Returns the value of attribute templates_dir.
Instance Method Summary collapse
-
#derive_template_name(file_path) ⇒ Object
private
Derive template name from file path Examples: /path/to/templates/dashboard.rue => ‘dashboard’ /path/to/templates/pages/user/profile.rue => ‘pages/user/profile’.
-
#extract_all ⇒ Array<Hash>
Extract all schemas from .rue files in the templates directory.
-
#extract_from_file(file_path) ⇒ Hash?
Extract schema from a single .rue file.
-
#find_rue_files ⇒ Array<String>
Find all .rue files in the templates directory (recursive).
-
#initialize(templates_dir) ⇒ SchemaExtractor
constructor
A new instance of SchemaExtractor.
-
#schema_stats ⇒ Hash
Count how many .rue files have schema sections.
-
#validate_directory! ⇒ Object
private
Constructor Details
#initialize(templates_dir) ⇒ SchemaExtractor
Returns a new instance of SchemaExtractor.
23 24 25 26 |
# File 'lib/rhales/utils/schema_extractor.rb', line 23 def initialize(templates_dir) @templates_dir = File.(templates_dir) validate_directory! end |
Instance Attribute Details
#templates_dir ⇒ Object (readonly)
Returns the value of attribute templates_dir.
21 22 23 |
# File 'lib/rhales/utils/schema_extractor.rb', line 21 def templates_dir @templates_dir end |
Instance Method Details
#derive_template_name(file_path) ⇒ Object (private)
Derive template name from file path Examples: /path/to/templates/dashboard.rue => ‘dashboard’ /path/to/templates/pages/user/profile.rue => ‘pages/user/profile’
123 124 125 126 127 128 |
# File 'lib/rhales/utils/schema_extractor.rb', line 123 def derive_template_name(file_path) templates_pathname = Pathname.new(@templates_dir) file_pathname = Pathname.new(file_path) relative_path = file_pathname.relative_path_from(templates_pathname) relative_path.to_s.sub(/\.rue$/, '') end |
#extract_all ⇒ Array<Hash>
Extract all schemas from .rue files in the templates directory
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rhales/utils/schema_extractor.rb', line 46 def extract_all rue_files = find_rue_files schemas = [] rue_files.each do |file_path| begin schema_info = extract_from_file(file_path) schemas << schema_info if schema_info rescue => e warn "Warning: Failed to extract schema from #{file_path}: #{e.}" end end schemas end |
#extract_from_file(file_path) ⇒ Hash?
Extract schema from a single .rue file
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rhales/utils/schema_extractor.rb', line 66 def extract_from_file(file_path) doc = RueDocument.parse_file(file_path) return nil unless doc.section?('schema') template_name = derive_template_name(file_path) schema_code = doc.section('schema') { template_name: template_name, template_path: file_path, schema_code: schema_code.strip, lang: doc.schema_lang, version: doc.schema_version, envelope: doc.schema_envelope, window: doc.schema_window, merge: doc.schema_merge_strategy, layout: doc.schema_layout, extends: doc.schema_extends } end |
#find_rue_files ⇒ Array<String>
Find all .rue files in the templates directory (recursive)
91 92 93 94 |
# File 'lib/rhales/utils/schema_extractor.rb', line 91 def find_rue_files pattern = File.join(@templates_dir, '**', '*.rue') Dir.glob(pattern).sort end |
#schema_stats ⇒ Hash
Count how many .rue files have schema sections
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rhales/utils/schema_extractor.rb', line 99 def schema_stats all_files = find_rue_files schemas = extract_all { total_files: all_files.count, files_with_schemas: schemas.count, files_without_schemas: all_files.count - schemas.count, schemas_by_lang: schemas.group_by { |s| s[:lang] }.transform_values(&:count) } end |
#validate_directory! ⇒ Object (private)
113 114 115 116 117 |
# File 'lib/rhales/utils/schema_extractor.rb', line 113 def validate_directory! unless File.directory?(@templates_dir) raise ExtractionError, "Templates directory does not exist: #{@templates_dir}" end end |