Class: Expressir::Model::Indexes::TypeIndex
- Inherits:
-
Object
- Object
- Expressir::Model::Indexes::TypeIndex
- Defined in:
- lib/expressir/model/indexes/type_index.rb
Overview
Index for fast type lookup across schemas Maintains indexes by qualified name, by schema, and by category
Instance Attribute Summary collapse
-
#by_category ⇒ Object
readonly
Returns the value of attribute by_category.
-
#by_qualified_name ⇒ Object
readonly
Returns the value of attribute by_qualified_name.
-
#by_schema ⇒ Object
readonly
Returns the value of attribute by_schema.
Instance Method Summary collapse
-
#build(schemas) ⇒ void
Build indexes from schemas.
-
#categorize(type) ⇒ String
Categorize type by its underlying type.
-
#category_types(category) ⇒ Array<Declarations::Type>
Get types for a specific category.
-
#count ⇒ Integer
Count total types.
-
#empty? ⇒ Boolean
Check if index is empty.
-
#find(qualified_name) ⇒ Declarations::Type?
Find type by qualified name.
-
#initialize(schemas = []) ⇒ TypeIndex
constructor
Initialize a new type index.
-
#list(schema: nil, category: nil) ⇒ Array<Declarations::Type>
List all types.
-
#schema_types(schema) ⇒ Hash<String, Declarations::Type>
Get types for a specific schema.
Constructor Details
#initialize(schemas = []) ⇒ TypeIndex
Initialize a new type index
13 14 15 16 17 18 |
# File 'lib/expressir/model/indexes/type_index.rb', line 13 def initialize(schemas = []) @by_schema = {} @by_qualified_name = {} @by_category = {} build(schemas) unless schemas.empty? end |
Instance Attribute Details
#by_category ⇒ Object (readonly)
Returns the value of attribute by_category.
9 10 11 |
# File 'lib/expressir/model/indexes/type_index.rb', line 9 def by_category @by_category end |
#by_qualified_name ⇒ Object (readonly)
Returns the value of attribute by_qualified_name.
9 10 11 |
# File 'lib/expressir/model/indexes/type_index.rb', line 9 def by_qualified_name @by_qualified_name end |
#by_schema ⇒ Object (readonly)
Returns the value of attribute by_schema.
9 10 11 |
# File 'lib/expressir/model/indexes/type_index.rb', line 9 def by_schema @by_schema end |
Instance Method Details
#build(schemas) ⇒ void
This method returns an undefined value.
Build indexes from schemas
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/expressir/model/indexes/type_index.rb', line 23 def build(schemas) @by_schema = {} @by_qualified_name = {} @by_category = {} schemas.each do |schema| schema_id = schema.id.safe_downcase @by_schema[schema_id] = {} next unless schema.types schema.types.each do |type| type_id = type.id.safe_downcase qualified_name = "#{schema_id}.#{type_id}" category = categorize(type) @by_schema[schema_id][type_id] = type @by_qualified_name[qualified_name] = type @by_category[category] ||= [] @by_category[category] << type end end end |
#categorize(type) ⇒ String
Categorize type by its underlying type
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/expressir/model/indexes/type_index.rb', line 121 def categorize(type) return "defined" unless type. = type. # Use explicit is_a? checks for test double compatibility return "select" if .is_a?(DataTypes::Select) return "enumeration" if .is_a?(DataTypes::Enumeration) return "aggregate" if .is_a?(DataTypes::Array) || .is_a?(DataTypes::Bag) || .is_a?(DataTypes::List) || .is_a?(DataTypes::Set) "defined" end |
#category_types(category) ⇒ Array<Declarations::Type>
Get types for a specific category
114 115 116 |
# File 'lib/expressir/model/indexes/type_index.rb', line 114 def category_types(category) @by_category[category] || [] end |
#count ⇒ Integer
Count total types
100 101 102 |
# File 'lib/expressir/model/indexes/type_index.rb', line 100 def count @by_qualified_name.size end |
#empty? ⇒ Boolean
Check if index is empty
94 95 96 |
# File 'lib/expressir/model/indexes/type_index.rb', line 94 def empty? @by_qualified_name.empty? end |
#find(qualified_name) ⇒ Declarations::Type?
Find type by qualified name
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/expressir/model/indexes/type_index.rb', line 50 def find(qualified_name) return nil if qualified_name.nil? || qualified_name.empty? normalized_name = qualified_name.safe_downcase # Try qualified name first type = @by_qualified_name[normalized_name] return type if type # Try as simple name across all schemas schema_name, type_name = normalized_name.split(".", 2) if type_name.nil? # Search all schemas for simple name @by_schema.each_value do |types| type = types[schema_name] return type if type end else # Look in specific schema schema_types = @by_schema[schema_name] return schema_types[type_name] if schema_types end nil end |
#list(schema: nil, category: nil) ⇒ Array<Declarations::Type>
List all types
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/expressir/model/indexes/type_index.rb', line 80 def list(schema: nil, category: nil) types = if schema @by_schema[schema.safe_downcase]&.values || [] else @by_qualified_name.values end types = filter_by_category(types, category) if category types end |
#schema_types(schema) ⇒ Hash<String, Declarations::Type>
Get types for a specific schema
107 108 109 |
# File 'lib/expressir/model/indexes/type_index.rb', line 107 def schema_types(schema) @by_schema[schema.safe_downcase] || {} end |