avromatic 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3edc684e5b6a637fa71fa717b8ea30e6503cc0a5
4
- data.tar.gz: 1e32d812904379da626158fbb27b486a3a3eb465
3
+ metadata.gz: ce7da1b2c3c2ec8741eba98e50a294aa8db18bb1
4
+ data.tar.gz: 688813aeb07b275550fda92b30cc97f339ddf142
5
5
  SHA512:
6
- metadata.gz: 5c08d19c73780f6421126098c325852db05a828905ea2fb4f87c71fe4ffdf83460a373c67430ddf71a21d138f655f899d87dde3891adcc2e03d45ead4f1dde1a
7
- data.tar.gz: bd398cea6c28603b3975c8f893d67f8362154c061ed8105c29bb2d625e71332ad51fda763f89b613ceb476ce2e8104d2c9f5331e32163187112c13c41a95abdb
6
+ metadata.gz: 18c3b5f0a4aa093e9ed2868722f103fcd780361c417ab1a72b5f7e5a6a5c94b658e4100bfd42bbcf14fd30a2d6c0d9a154ee9bb8de4e5823a6ea4d76487d072a
7
+ data.tar.gz: 0988f486a37745ddf3e46d027d3b9976665807804c3f0355d05e99ceefa73abdeab881e6bab109f65220c60ee698d944017f9b487931fe75c5e19d6e7a60a9d0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # avromatic changelog
2
2
 
3
+ ## v0.11.0
4
+ - Replace `Avromatic.on_initialize` proc with `Avromatic.eager_load_models`
5
+ array. The models listed by this configuration are added to the registry
6
+ at the end of `.configure` and prior to code reloading in Rails applications.
7
+ This is a compatibility breaking change.
8
+
3
9
  ## v0.10.0
4
10
  - Add `Avromatic.on_initialize` proc that is called at the end of `.configure`
5
11
  and on code reloading in Rails applications.
data/README.md CHANGED
@@ -41,10 +41,11 @@ Or install it yourself as:
41
41
  that is used to store, by full schema name, the generated models that are
42
42
  embedded within top-level models. By default a new `Avromatic::ModelRegistry`
43
43
  is created.
44
- * **on_initialize**: An optional Proc that runs at the end of
44
+ * **eager_load_models**: An optional array of models, or strings with class
45
+ names for models, that are added to `nested_models` at the end of
45
46
  `Avromatic.configure` and during code reloading in Rails applications. This
46
- option is useful for defining models that will be extended when the load
47
- order is important.
47
+ option is useful for defining models that will be extended when the load order
48
+ is important.
48
49
 
49
50
  #### Using a Schema Registry/Messaging API
50
51
 
@@ -173,18 +174,18 @@ it can be used as a nested model.
173
174
  To extend a model that will be used as a nested model, you must ensure that it
174
175
  is defined, which will register it, prior it being referenced by another model.
175
176
 
176
- Using the `Avromatic.on_initialize` option allows models that are extended and
177
- will be used as nested models to be defined at the end of the `.configure`
178
- block. In Rails applications, this Proc will also be executed after
177
+ Using the `Avromatic.eager_load_models` option allows models that are extended
178
+ and will be used as nested models to be defined at the end of the `.configure`
179
+ block. In Rails applications, these models are also re-registered after
179
180
  `nested_models` is cleared when code reloads to ensure that classes load in the
180
181
  correct order:
181
182
 
182
183
  ```ruby
183
184
  Avromatic.configure do |config|
184
- config.on_initialize do
185
+ config.eager_load_models = [
185
186
  # reference any extended models that should be defined first
186
187
  MyNestedModel
187
- end
188
+ ]
188
189
  end
189
190
 
190
191
  #### Custom Types
@@ -21,6 +21,8 @@ module Avromatic
21
21
 
22
22
  module ClassMethods
23
23
  def add_avro_fields
24
+ # models are registered in Avromatic.nested_models at this point to
25
+ # ensure that they are available as fields for recursive models.
24
26
  register!
25
27
 
26
28
  if key_avro_schema
@@ -18,11 +18,11 @@ module Avromatic
18
18
  def [](fullname)
19
19
  @hash.fetch(fullname)
20
20
  end
21
+ alias_method :fetch, :[]
21
22
 
22
23
  def register(model)
23
24
  raise 'models with a key schema are not supported' if model.key_avro_schema
24
- name = model.avro_schema.fullname
25
- name = remove_prefix(name)
25
+ name = model_fullname(model)
26
26
  raise "'#{name}' has already been registered" if registered?(name)
27
27
  @hash[name] = model
28
28
  end
@@ -31,6 +31,22 @@ module Avromatic
31
31
  @hash.key?(fullname)
32
32
  end
33
33
 
34
+ def model_fullname(model)
35
+ name = model.avro_schema.fullname
36
+ remove_prefix(name)
37
+ end
38
+
39
+ def ensure_registered_model(model)
40
+ name = model_fullname(model)
41
+ if registered?(name)
42
+ unless fetch(name).equal?(model)
43
+ raise "attempted to replace existing model #{fetch(name)} with new model #{model} as '#{name}'"
44
+ end
45
+ else
46
+ register(model)
47
+ end
48
+ end
49
+
34
50
  def remove_prefix(name)
35
51
  return name if @prefix.nil?
36
52
 
@@ -1,3 +1,3 @@
1
1
  module Avromatic
2
- VERSION = '0.10.0'.freeze
2
+ VERSION = '0.11.0'.freeze
3
3
  end
data/lib/avromatic.rb CHANGED
@@ -3,11 +3,12 @@ require 'avromatic/model'
3
3
  require 'avromatic/model_registry'
4
4
  require 'avro_turf'
5
5
  require 'avro_turf/messaging'
6
+ require 'active_support/core_ext/string/inflections'
6
7
 
7
8
  module Avromatic
8
9
  class << self
9
10
  attr_accessor :schema_registry, :registry_url, :schema_store, :logger,
10
- :messaging, :type_registry, :nested_models, :on_initialize
11
+ :messaging, :type_registry, :nested_models
11
12
 
12
13
  delegate :register_type, to: :type_registry
13
14
  end
@@ -18,7 +19,7 @@ module Avromatic
18
19
 
19
20
  def self.configure
20
21
  yield self
21
- on_initialize.call if on_initialize
22
+ eager_load_models!
22
23
  end
23
24
 
24
25
  def self.build_schema_registry
@@ -41,10 +42,23 @@ module Avromatic
41
42
  self.messaging = build_messaging
42
43
  end
43
44
 
45
+ # This method is called as a Rails to_prepare block after the application
46
+ # first initializes and prior to each code reloading.
44
47
  def self.prepare!
45
48
  nested_models.clear
46
- on_initialize.call if on_initialize
49
+ eager_load_models!
47
50
  end
51
+
52
+ def self.eager_load_models=(models)
53
+ @eager_load_model_names = Array(models).map { |model| model.is_a?(Class) ? model.name : model }
54
+ end
55
+
56
+ def self.eager_load_models!
57
+ (@eager_load_model_names || []).each do |model_name|
58
+ nested_models.ensure_registered_model(model_name.constantize)
59
+ end
60
+ end
61
+ private_class_method :eager_load_models!
48
62
  end
49
63
 
50
64
  require 'avromatic/railtie' if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avromatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify Engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-05 00:00:00.000000000 Z
11
+ date: 2016-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avro