lib/avromatic/model_registry.rb in avromatic-0.10.0 vs lib/avromatic/model_registry.rb in avromatic-0.11.0

- old
+ new

@@ -16,20 +16,36 @@ end def [](fullname) @hash.fetch(fullname) end + alias_method :fetch, :[] def register(model) raise 'models with a key schema are not supported' if model.key_avro_schema - name = model.avro_schema.fullname - name = remove_prefix(name) + name = model_fullname(model) raise "'#{name}' has already been registered" if registered?(name) @hash[name] = model end def registered?(fullname) @hash.key?(fullname) + end + + def model_fullname(model) + name = model.avro_schema.fullname + remove_prefix(name) + end + + def ensure_registered_model(model) + name = model_fullname(model) + if registered?(name) + unless fetch(name).equal?(model) + raise "attempted to replace existing model #{fetch(name)} with new model #{model} as '#{name}'" + end + else + register(model) + end end def remove_prefix(name) return name if @prefix.nil?