tdc 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea6a4c15535d29f9f7ab47c6ec12f4c20cc59f588543b19869324f5901097c24
4
- data.tar.gz: c5d57e1578f9550a725f20f6ef396bd2bb3d5791773929afd9c6605d8686f2b3
3
+ metadata.gz: 6d7775cd9909b7d1aef1f59def823777cc24d6bbfc50ddfd41eda202f5f5f387
4
+ data.tar.gz: 11b7ddadec9d97e1953f34efdee8b66d6f0cb08213dd8d9a9e9d4ae8372687fa
5
5
  SHA512:
6
- metadata.gz: 7829e509a7c5ef3c7db2b947ee109af96a442c662ccb15e0f5ab50a04e70f91eef039944b65a4b6dfcf9eae232e249146062067c79a2ca955cff2027b0aee649
7
- data.tar.gz: 20eefb49010d15905073f5f823bdc171cc428e5c2ef1217d61ec17210d4426f4dd7de4312c56167cd4b67b37d43c2049b3cfc90b0a8b51f4577eb5c884b94841
6
+ metadata.gz: 1f396da321ee096251062cd35ff6add1d19292591dc8fa019509fe432f2fd7faea74844ee4583fbb30bc03e9f8106fbeabdb403732ec5098d70c40b69e2c216c
7
+ data.tar.gz: 69cfdaa1a302ae2d458f6775db22134953db4f6f584ba9af36970f4865d308de9a44d9c5c6ec10e7db0a4ec5c05d340254a4458dfc843d6676225459e2542660
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.3.0] - 2020-07-19
10
+
11
+ - Introduce the DefinitionResolver abstraction
12
+
9
13
  ## [0.2.4] - 2020-07-19
10
14
 
11
15
  - Improve Tdc::MissingOverrideError diagnostics
data/README.md CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  Define your own test data generators by inheriting from ```StandardGenerator``` or ```SingularGenerator```. Best practice is to define an ```ApplicationStandardGenerator``` and an ```ApplicationSingularGenerator``` and have all other generators inherit from them.
8
8
 
9
+ Define your own definition resolvers by inheriting from ```DefinitionResolver```. Best practice is to define an ```ApplicationDefinitionResolver``` and all other definition resolvers inherit from it.
10
+
9
11
  During generation the test data catalog will be represented by ```CatalogEntries``` that are populated by reading from ```YAML``` files with a ```DataDefinitionReader``` or provided directly by an ```InMemoryDataDefinition```.
10
12
 
11
13
  **Data Definition DSL**
Binary file
data/lib/tdc.rb CHANGED
@@ -33,6 +33,11 @@ require "tdc/generators/configurable_generator"
33
33
  require "tdc/generators/singular_generator"
34
34
  require "tdc/generators/standard_generator"
35
35
 
36
+ # Definition Resolvers
37
+ require "tdc/definition_resolvers"
38
+ require "tdc/definition_resolvers/definition_resolver"
39
+ require "tdc/definition_resolvers/tag_resolver"
40
+
36
41
  #
37
42
  # A framework for building a Test Data Catalog
38
43
  #
@@ -0,0 +1,7 @@
1
+ module Tdc
2
+ #
3
+ # Namespace to host definition resolvers.
4
+ #
5
+ module DefinitionResolvers
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Tdc
2
+ module DefinitionResolvers
3
+ #
4
+ # Base class for any definition resolver.
5
+ #
6
+ class DefinitionResolver
7
+ attr_reader :current_catalog
8
+
9
+ def configure_current_catalog(current_catalog)
10
+ @current_catalog = current_catalog
11
+ end
12
+
13
+ def resolve(_instance_definition)
14
+ raise Tdc::MissingOverrideError, "Implement the 'resolve' method"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ module Tdc
2
+ module DefinitionResolvers
3
+ #
4
+ # Knows how to resolve the tag value for the specified instance_definition key by replacing it
5
+ # with an object sourced from the current catalog.
6
+ #
7
+ class TagResolver < Tdc::DefinitionResolvers::DefinitionResolver
8
+ attr_reader :key, :source
9
+
10
+ def initialize(key:, source:)
11
+ @key = key
12
+ @source = source
13
+ end
14
+
15
+ def resolve(instance_definition)
16
+ return unless instance_definition.key?(key)
17
+
18
+ # Lookup the source catalog entry in the current_catalog.
19
+ catalog_entry = instance_eval("current_catalog.#{source}", __FILE__, __LINE__)
20
+
21
+ # Before resolution the instance definition value is a tag.
22
+ tag = instance_definition[key]
23
+
24
+ # Use the tag to source an object from the current catalog.
25
+ sourced_object = catalog_entry.send(tag)
26
+
27
+ unless sourced_object
28
+ message = "Could not find a tag reference for '#{key}' in the catalog entries provided."
29
+
30
+ raise Tdc::FatalError, message
31
+ end
32
+
33
+ # Replace the tag value with the sourced object.
34
+ instance_definition[key] = sourced_object
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,6 +1,6 @@
1
1
  module Tdc
2
2
  #
3
- # Namespace to host generators
3
+ # Namespace to host generators.
4
4
  #
5
5
  module Generators
6
6
  end
@@ -14,7 +14,7 @@ module Tdc
14
14
 
15
15
  def run_resolvers_and_generate_instance
16
16
  run_atx_resolvers(instance_definition)
17
- run_tag_resolvers(instance_definition)
17
+ run_definition_resolvers(instance_definition)
18
18
 
19
19
  generate_instance
20
20
  end
@@ -29,7 +29,7 @@ module Tdc
29
29
  #
30
30
  # Hook method: subclasses may include the DefinitionResolvable concern to override.
31
31
  #
32
- def run_tag_resolvers(_instance_definition)
32
+ def run_definition_resolvers(_instance_definition)
33
33
  # Do nothing
34
34
  end
35
35
 
@@ -1,61 +1,59 @@
1
1
  module Tdc
2
2
  module Generators
3
3
  #
4
- # Knows how to resolve tag values in an instance definition. The tag value will be replaced
5
- # with a model instance from the current catalog.
4
+ # Knows how to resolve attribute values in an instance definition. The attribute value will be replaced
5
+ # by the result of invoking the specified resolver.
6
6
  #
7
- # The resolve_tag class macro is provided for generators to define tag resolution.
7
+ # The resolve_tag class macro is provided for generators to define tag resolution. The test data definition
8
+ # YAML file has attribute values than contain a catalog entry tag.
8
9
  #
9
10
  # Example:
10
11
  #
11
- # Suppose a particular instance definition contained { subcomponent: sc_snackers_minis }
12
- # then a generator could resolve the subcomponent by defining:
12
+ # Suppose a particular instance definition contained { subcomponent: sc_snackers_minis } then a generator
13
+ # could resolve the subcomponent tag to the current catalog by defining:
13
14
  #
14
15
  # resolve_tag key: :subcomponent, source: "item_master.items"
15
16
  #
17
+ # The resolve_definition class macro is provided for generators to define definition resolution. The test data
18
+ # definition YAML file has attribute values than contain an arbitrary value that will typically be replaced by
19
+ # an instance of an object returned by the specified resolver.
20
+ #
21
+ # Example:
22
+ #
23
+ # Suppose a particular instance definition contained { pallet_number: P0001 } then a generator could resolve
24
+ # the pallet_number to a pallet instance by defining:
25
+ #
26
+ # resolve_definition :pallet_number, to: :pallet_id, resolver: "TestDataCatalog::Resolvers::PalletNumberResolver"
27
+ #
28
+ # The PalletNumberResolver class inherits from Tdc::DefinitionResolvers::DefinitionResolver.
29
+ #
16
30
  module DefinitionResolvable
17
31
  extend ActiveSupport::Concern
18
32
 
19
33
  included do
20
- class_attribute :_tag_resolvers, instance_writer: false
21
- self._tag_resolvers = []
34
+ class_attribute :_definition_resolvers, instance_writer: false
35
+
36
+ self._definition_resolvers = []
22
37
  end
23
38
 
24
39
  class_methods do
25
- def resolve_tag(key:, source:)
26
- _tag_resolvers << [key, source]
40
+ def resolve_definition(key:, to: nil, resolver:)
41
+ resolver_instance = resolver.constantize.new(key: key, to: to || key)
42
+
43
+ _definition_resolvers << resolver_instance
27
44
  end
28
- end
29
45
 
30
- def run_tag_resolvers(instance_definition)
31
- _tag_resolvers.each { |key, source| _resolve_tag_reference(instance_definition, key, source) }
46
+ def resolve_tag(key:, source:)
47
+ _definition_resolvers << Tdc::DefinitionResolvers::TagResolver.new(key: key, source: source)
48
+ end
32
49
  end
33
50
 
34
- private
35
-
36
- #
37
- # Replace the tag value for the specified instance_definition key with an object sourced from the current catalog.
38
- #
39
- def _resolve_tag_reference(instance_definition, key, source)
40
- return unless instance_definition.key?(key)
41
-
42
- # Lookup the source catalog entry in the current_catalog.
43
- catalog_entry = instance_eval("current_catalog.#{source}", __FILE__, __LINE__)
51
+ def run_definition_resolvers(instance_definition)
52
+ _definition_resolvers.each do |definition_resolver|
53
+ definition_resolver.configure_current_catalog(current_catalog)
44
54
 
45
- # Before resolution the instance definition value is a tag.
46
- tag = instance_definition[key]
47
-
48
- # Use the tag to source an object from the current catalog.
49
- sourced_object = catalog_entry.send(tag)
50
-
51
- unless sourced_object
52
- message = "Could not find a tag reference for '#{key}' in the catalog entries provided."
53
-
54
- raise Tdc::FatalError, message
55
+ definition_resolver.resolve(instance_definition)
55
56
  end
56
-
57
- # Replace the tag value with the sourced object.
58
- instance_definition[key] = sourced_object
59
57
  end
60
58
  end
61
59
  end
@@ -1,3 +1,3 @@
1
1
  module Tdc
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tdc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alistair McKinnell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-19 00:00:00.000000000 Z
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -128,6 +128,9 @@ files:
128
128
  - lib/tdc.rb
129
129
  - lib/tdc/data_definition.rb
130
130
  - lib/tdc/data_definition_file_reader.rb
131
+ - lib/tdc/definition_resolvers.rb
132
+ - lib/tdc/definition_resolvers/definition_resolver.rb
133
+ - lib/tdc/definition_resolvers/tag_resolver.rb
131
134
  - lib/tdc/fatal_error.rb
132
135
  - lib/tdc/generators.rb
133
136
  - lib/tdc/generators/catalog_entries.rb