Class: Avromatic::ModelRegistry
- Inherits:
-
Object
- Object
- Avromatic::ModelRegistry
- Defined in:
- lib/avromatic/model_registry.rb
Overview
The ModelRegistry class is used to store and fetch nested models by their fullname. An optional namespace prefix can be removed from the full name that is used to store and fetch models.
Instance Method Summary collapse
- #[](fullname) ⇒ Object (also: #fetch)
- #clear ⇒ Object
- #ensure_registered_model(model) ⇒ Object
-
#initialize(remove_namespace_prefix: nil) ⇒ ModelRegistry
constructor
A new instance of ModelRegistry.
- #model_fullname(model) ⇒ Object
- #register(model) ⇒ Object
- #registered?(fullname_or_model) ⇒ Boolean
- #remove_prefix(name) ⇒ Object
Constructor Details
#initialize(remove_namespace_prefix: nil) ⇒ ModelRegistry
Returns a new instance of ModelRegistry.
11 12 13 14 |
# File 'lib/avromatic/model_registry.rb', line 11 def initialize(remove_namespace_prefix: nil) @prefix = remove_namespace_prefix @hash = Hash.new end |
Instance Method Details
#[](fullname) ⇒ Object Also known as: fetch
20 21 22 |
# File 'lib/avromatic/model_registry.rb', line 20 def [](fullname) @hash.fetch(fullname) end |
#clear ⇒ Object
16 17 18 |
# File 'lib/avromatic/model_registry.rb', line 16 def clear @hash.clear end |
#ensure_registered_model(model) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/avromatic/model_registry.rb', line 44 def ensure_registered_model(model) name = model_fullname(model) if registered?(name) existing_model = fetch(name) unless existing_model.equal?(model) raise "Attempted to replace existing Avromatic model #{model_debug_name(existing_model)} with new model " \ "#{model_debug_name(model)} as '#{name}'. Perhaps '#{model_debug_name(model)}' needs to be eager loaded " \ 'via the Avromatic eager_load_models setting?' end else register(model) end end |
#model_fullname(model) ⇒ Object
39 40 41 42 |
# File 'lib/avromatic/model_registry.rb', line 39 def model_fullname(model) name = model.avro_schema.fullname remove_prefix(name) end |
#register(model) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/avromatic/model_registry.rb', line 25 def register(model) raise 'models with a key schema are not supported' if model.key_avro_schema name = model_fullname(model) raise "'#{name}' has already been registered" if registered?(name) @hash[name] = model end |
#registered?(fullname_or_model) ⇒ Boolean
34 35 36 37 |
# File 'lib/avromatic/model_registry.rb', line 34 def registered?(fullname_or_model) fullname = fullname_or_model.is_a?(String) ? fullname_or_model : model_fullname(fullname_or_model) @hash.key?(fullname) end |
#remove_prefix(name) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/avromatic/model_registry.rb', line 58 def remove_prefix(name) return name if @prefix.nil? value = case @prefix when String name.start_with?(@prefix) ? name.from(@prefix.length) : name when Regexp name.sub(@prefix, '') else raise "unsupported `remove_namespace_prefix` value: #{@prefix}" end value.start_with?('.') ? value.from(1) : value end |