Class: Rhales::SchemaExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rhales/utils/schema_extractor.rb

Overview

Extracts schema definitions from .rue files

This class scans template directories for .rue files containing sections and extracts the schema code along with metadata (attributes).

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

Instance Method Summary collapse

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.expand_path(templates_dir)
  validate_directory!
end

Instance Attribute Details

#templates_dirObject (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_allArray<Hash>

Extract all schemas from .rue files in the templates directory

Examples:

[
  {
    template_name: 'dashboard',
    template_path: '/path/to/dashboard.rue',
    schema_code: 'const schema = z.object({...});',
    lang: 'ts-zod',
    version: '2',
    envelope: 'SuccessEnvelope',
    window: 'appData',
    merge: 'deep',
    layout: 'layouts/main',
    extends: nil
  }
]

Returns:

  • (Array<Hash>)

    Array of schema information hashes



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.message}"
    end
  end

  schemas
end

#extract_from_file(file_path) ⇒ Hash?

Extract schema from a single .rue file

Parameters:

  • file_path (String)

    Path to the .rue file

Returns:

  • (Hash, nil)

    Schema information hash or nil if no schema section



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_filesArray<String>

Find all .rue files in the templates directory (recursive)

Returns:

  • (Array<String>)

    Array of absolute file paths



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_statsHash

Count how many .rue files have schema sections

Returns:

  • (Hash)

    Count information



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