Class: Expressir::Model::Indexes::TypeIndex

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(schemas = []) ⇒ TypeIndex

Initialize a new type index

Parameters:



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_categoryObject (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_nameObject (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_schemaObject (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

Parameters:



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

Parameters:

Returns:



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.underlying_type

  underlying = type.underlying_type

  # Use explicit is_a? checks for test double compatibility
  return "select" if underlying.is_a?(DataTypes::Select)
  return "enumeration" if underlying.is_a?(DataTypes::Enumeration)
  return "aggregate" if underlying.is_a?(DataTypes::Array) ||
    underlying.is_a?(DataTypes::Bag) ||
    underlying.is_a?(DataTypes::List) ||
    underlying.is_a?(DataTypes::Set)

  "defined"
end

#category_types(category) ⇒ Array<Declarations::Type>

Get types for a specific category

Parameters:

  • category (String)

    Category name

Returns:



114
115
116
# File 'lib/expressir/model/indexes/type_index.rb', line 114

def category_types(category)
  @by_category[category] || []
end

#countInteger

Count total types

Returns:

  • (Integer)

    Total number of 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

Returns:

  • (Boolean)

    True if no types indexed



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

Parameters:

  • qualified_name (String)

    Type qualified name (e.g., “schema.type”)

Returns:



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

Parameters:

  • schema (String, nil) (defaults to: nil)

    Filter by schema name

  • category (String, nil) (defaults to: nil)

    Filter by category

Returns:



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

Parameters:

  • schema (String)

    Schema name

Returns:



107
108
109
# File 'lib/expressir/model/indexes/type_index.rb', line 107

def schema_types(schema)
  @by_schema[schema.safe_downcase] || {}
end