Class: Lutaml::Xsd::SchemaRepository::TypeIndex
- Inherits:
-
Object
- Object
- Lutaml::Xsd::SchemaRepository::TypeIndex
- Defined in:
- lib/lutaml/xsd/schema_repository/type_index.rb
Overview
Internal helper for indexing and looking up type definitions across multiple schemas
Instance Method Summary collapse
-
#all ⇒ Hash
Get all indexed types.
-
#build_from_schemas(schemas) ⇒ Object
Build index from parsed schemas.
-
#clear ⇒ Object
Clear the index.
-
#find(clark_key) ⇒ Hash?
Find a type by Clark notation key.
-
#find_all_in_namespace(namespace) ⇒ Array<Hash>
Get all types in a namespace.
-
#find_by_namespace_and_name(namespace, local_name) ⇒ Hash?
Find a type by namespace and local name.
-
#index_schema(schema, file_path) ⇒ Object
Index a single schema.
-
#initialize ⇒ TypeIndex
constructor
A new instance of TypeIndex.
-
#statistics ⇒ Hash
Get statistics about indexed types.
-
#suggest_similar(namespace, local_name, limit: 5) ⇒ Array<String>
Get suggestions for similar type names (for error messages).
Constructor Details
#initialize ⇒ TypeIndex
Returns a new instance of TypeIndex.
9 10 11 12 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 9 def initialize @index = {} @schema_files = {} end |
Instance Method Details
#all ⇒ Hash
Get all indexed types
104 105 106 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 104 def all @index.dup end |
#build_from_schemas(schemas) ⇒ Object
Build index from parsed schemas
16 17 18 19 20 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 16 def build_from_schemas(schemas) schemas.each do |file_path, schema| index_schema(schema, file_path) end end |
#clear ⇒ Object
Clear the index
124 125 126 127 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 124 def clear @index.clear @schema_files.clear end |
#find(clark_key) ⇒ Hash?
Find a type by Clark notation key
59 60 61 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 59 def find(clark_key) @index[clark_key] end |
#find_all_in_namespace(namespace) ⇒ Array<Hash>
Get all types in a namespace
77 78 79 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 77 def find_all_in_namespace(namespace) @index.select { |key, _| key.start_with?("{#{namespace}}") }.values end |
#find_by_namespace_and_name(namespace, local_name) ⇒ Hash?
Find a type by namespace and local name
67 68 69 70 71 72 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 67 def find_by_namespace_and_name(namespace, local_name) return nil if local_name.nil? clark_key = build_clark_key(namespace, local_name) find(clark_key) end |
#index_schema(schema, file_path) ⇒ Object
Index a single schema
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 25 def index_schema(schema, file_path) return unless schema namespace = schema.target_namespace # Index simple types index_collection(schema.simple_type, namespace, file_path, :simple_type) # Index complex types index_collection(schema.complex_type, namespace, file_path, :complex_type) # Index top-level elements index_collection(schema.element, namespace, file_path, :element) # Index attribute groups index_collection(schema.attribute_group, namespace, file_path, :attribute_group) # Index groups index_collection(schema.group, namespace, file_path, :group) # Index top-level attributes index_collection(schema.attribute, namespace, file_path, :attribute) # NOTE: imported and included schemas are already parsed and available # in the processed_schemas cache. We'll index them when build_from_schemas # iterates over all schemas in the cache. end |
#statistics ⇒ Hash
Get statistics about indexed types
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 110 def statistics type_counts = Hash.new(0) @index.each_value do |info| type_counts[info[:type]] += 1 end { total_types: @index.size, by_type: type_counts, namespaces: namespace_count, } end |
#suggest_similar(namespace, local_name, limit: 5) ⇒ Array<String>
Get suggestions for similar type names (for error messages)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lutaml/xsd/schema_repository/type_index.rb', line 86 def suggest_similar(namespace, local_name, limit: 5) types_in_namespace = find_all_in_namespace(namespace) return [] if types_in_namespace.empty? # Simple similarity: check if name is contained or contains the search term similar = types_in_namespace.select do |type_info| name = type_info[:definition]&.name next false unless name name.downcase.include?(local_name.downcase) || local_name.downcase.include?(name.downcase) end similar.filter_map { |info| info[:definition]&.name }.take(limit) end |