Class: Odin::Resolver::TypeRegistry
- Inherits:
-
Object
- Object
- Odin::Resolver::TypeRegistry
- Defined in:
- lib/odin/resolver/type_registry.rb
Overview
Namespaced type registry for import resolution.
Instance Method Summary collapse
-
#all_type_names ⇒ Object
All type names, namespaced names rendered as “alias.name”.
- #has?(name) ⇒ Boolean
-
#initialize ⇒ TypeRegistry
constructor
A new instance of TypeRegistry.
-
#lookup(name) ⇒ Object
Look up a type by name: local first, then dot-namespaced “alias.name”, then any namespace for an unqualified name.
-
#register_all(types, namespace = nil) ⇒ Object
Register all types from a schema under an optional namespace (import alias).
Constructor Details
#initialize ⇒ TypeRegistry
Returns a new instance of TypeRegistry.
7 8 9 10 |
# File 'lib/odin/resolver/type_registry.rb', line 7 def initialize @local_types = {} @namespaces = {} end |
Instance Method Details
#all_type_names ⇒ Object
All type names, namespaced names rendered as “alias.name”.
45 46 47 48 49 50 51 |
# File 'lib/odin/resolver/type_registry.rb', line 45 def all_type_names names = @local_types.keys.dup @namespaces.each do |ns, ns_map| ns_map.each_key { |n| names << "#{ns}.#{n}" } end names end |
#has?(name) ⇒ Boolean
40 41 42 |
# File 'lib/odin/resolver/type_registry.rb', line 40 def has?(name) !lookup(name).nil? end |
#lookup(name) ⇒ Object
Look up a type by name: local first, then dot-namespaced “alias.name”, then any namespace for an unqualified name.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/odin/resolver/type_registry.rb', line 24 def lookup(name) return @local_types[name] if @local_types.key?(name) if (dot = name.index(".")) ns = name[0...dot] type_name = name[(dot + 1)..] ns_map = @namespaces[ns] return ns_map[type_name] if ns_map&.key?(type_name) end @namespaces.each_value do |ns_map| return ns_map[name] if ns_map.key?(name) end nil end |
#register_all(types, namespace = nil) ⇒ Object
Register all types from a schema under an optional namespace (import alias).
13 14 15 16 17 18 19 20 |
# File 'lib/odin/resolver/type_registry.rb', line 13 def register_all(types, namespace = nil) if namespace ns_map = (@namespaces[namespace] ||= {}) types.each { |name, type_def| ns_map[name] = type_def } else types.each { |name, type_def| @local_types[name] = type_def } end end |