tdc 0.3.6.1 → 0.4.1

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: 4a9bab09cb1cb215daa94f3e53b950fd09f1baa059a3c6c8cddf47abd3fd95a7
4
- data.tar.gz: 2bfccac38422fc897d0c2208e0d5853f0a4adf13074c9da93262476d09007cf1
3
+ metadata.gz: f00288e8e5b9fd0db193bd5cb1f861dc54decc8966acc22a7b3c0ca37107970c
4
+ data.tar.gz: 8e57f9daa09264951dc05b6f0addab100a885dd9ca4de1a49466bf34e97b74a4
5
5
  SHA512:
6
- metadata.gz: 41e0866b7801d672791f6ea28d6878072f009aa31281d06889a2782c38464d79e2e2f3fd175d45cc9dbe9867491e07811fc58d1dffa0c1a1cd3c37d15f77c004
7
- data.tar.gz: 63588b60c890b674dd4541e10b848f97108d7244f63cce86fcbb26f7daf5b25689791f436fa10aa3545604cfdbca1422d1c61cf90d4cff7804ec726b1c50aec0
6
+ metadata.gz: 998df3b2cec606f557721fef093730ec59c038b492bc66087dea92793c0d5767dae2d2fa6b06edce00aa105e586597f92e71f5dd7624a7fd03ad6819c07a0435
7
+ data.tar.gz: f183c0656d49e69ad0c7834da267d2491ca2a58339a1cd820dd639459f7bfba7cf07a15df9cbe98d9749b1add54a5b03daa4a6815a70650286f52ed954595604
@@ -21,6 +21,10 @@ Metrics/BlockLength:
21
21
  Naming/MemoizedInstanceVariableName:
22
22
  EnforcedStyleForLeadingUnderscores: required
23
23
 
24
+ Naming/VariableNumber:
25
+ Enabled: true
26
+ EnforcedStyle: snake_case
27
+
24
28
  RSpec/AnyInstance:
25
29
  Enabled: false
26
30
 
@@ -6,9 +6,42 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.4.1] - 2020-08-22
10
+
11
+ - Add the `first` method to `CatalogEntries`
12
+
13
+ ## [0.4.0] - 2020-08-14
14
+
15
+ #### New Features
16
+
17
+ - Drop requirement that an empty test data definition file must be provided
18
+
19
+ ## [0.3.9] - 2020-08-12
20
+
21
+ #### Breaking Changes
22
+
23
+ - Collapse `ConfigurableGenerator` into `StandardGenerator`
24
+
25
+ ## [0.3.8] - 2020-08-11
26
+
27
+ #### New Features
28
+
29
+ - Multiple subclass instances of `ExtendedAttributes::InterpreterBase` may be registered
30
+ - A default extended attribute interpreter recognizes `_atx`, `_datex`, or `_onx` date/time attributes
31
+
32
+ #### Breaking Changes
33
+
34
+ - Rename `AtxContextFactory` to `ExtendedAttributes::InterpreterRegistry`
35
+
36
+ ## [0.3.7] - 2020-08-10
37
+
38
+ #### New Features
39
+
40
+ - Register a context with the `AtxContextFactory`
41
+
9
42
  ## [0.3.6.1] - 2020-08-10
10
43
 
11
- - Add the AtxContextFactory abstraction
44
+ - Add the `AtxContextFactory` abstraction
12
45
 
13
46
  ## [0.3.6] - 2020-08-06
14
47
 
data/README.md CHANGED
@@ -13,3 +13,5 @@ During generation the test data catalog will be represented by ```CatalogEntries
13
13
  **Data Definition DSL**
14
14
 
15
15
  ```DefinitionResolvable``` and ```DefinitionSourcable``` provide a DSL that you may use in your generators to work more easily with a ```DataDefinition```.
16
+
17
+ By registering a class instance that inherits from ```ExtendedAttributes::InterpreterBase``` with the ```ExtendedAttributes::InterpreterRegistry``` you may extend the DSL that is used to interpret attribute values.
Binary file
data/lib/tdc.rb CHANGED
@@ -7,10 +7,21 @@ require "active_support/core_ext"
7
7
 
8
8
  require "tdc/version"
9
9
 
10
- # Errors
10
+ # Definition Resolvers
11
+ require "tdc/definition_resolvers"
12
+ require "tdc/definition_resolvers/definition_resolver"
13
+ require "tdc/definition_resolvers/tag_resolver"
14
+
15
+ # Tdc Errors
11
16
  require "tdc/fatal_error"
12
17
  require "tdc/missing_override_error"
13
18
 
19
+ # Extended Attributes
20
+ require "tdc/extended_attributes"
21
+ require "tdc/extended_attributes/interpreter_base"
22
+ require "tdc/extended_attributes/default_interpreter"
23
+ require "tdc/extended_attributes/interpreter_registry"
24
+
14
25
  # Data Definition Hierarchy
15
26
  require "tdc/data_definition"
16
27
  require "tdc/data_definition_file_reader"
@@ -19,26 +30,17 @@ require "tdc/with_indifferent_access_decorator"
19
30
 
20
31
  # Generators
21
32
  require "tdc/generators"
22
- require "tdc/generators/atx_context_factory"
23
- require "tdc/generators/generation_context"
24
-
25
- # Current Catalog
26
33
  require "tdc/generators/catalog_entries"
34
+ require "tdc/generators/generation_context"
27
35
 
28
- # Concerns
36
+ # Generator Concerns
29
37
  require "tdc/generators/definition_resolvable"
30
38
  require "tdc/generators/definition_sourcable"
31
39
 
32
40
  # Generator Hierarchy
33
41
  require "tdc/generators/generator_base"
34
- require "tdc/generators/configurable_generator"
35
42
  require "tdc/generators/standard_generator"
36
43
 
37
- # Definition Resolvers
38
- require "tdc/definition_resolvers"
39
- require "tdc/definition_resolvers/definition_resolver"
40
- require "tdc/definition_resolvers/tag_resolver"
41
-
42
44
  #
43
45
  # A framework for building a Test Data Catalog
44
46
  #
@@ -29,7 +29,7 @@ module Tdc
29
29
  if File.exist?(definitions_file)
30
30
  load_yaml(definitions_file) || EMPTY_DEFINITIONS
31
31
  else
32
- missing_data_definition(definitions_file)
32
+ EMPTY_DEFINITIONS
33
33
  end
34
34
  end
35
35
 
@@ -45,11 +45,5 @@ module Tdc
45
45
  def expand_erb(definitions_file)
46
46
  ERB.new(File.read(definitions_file)).result
47
47
  end
48
-
49
- def missing_data_definition(definitions_file)
50
- return EMPTY_DEFINITIONS if ENV.key?("TDC_IGNORE_MISSING_TEST_DATA_DEFINITION_ERROR")
51
-
52
- raise Tdc::FatalError, "Missing test definitions file: '#{definitions_file}'"
53
- end
54
48
  end
55
49
  end
@@ -0,0 +1,7 @@
1
+ module Tdc
2
+ #
3
+ # Namespace to host extended attributes.
4
+ #
5
+ module ExtendedAttributes
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ module Tdc
2
+ module ExtendedAttributes
3
+ #
4
+ # Know how on interpret extended attributes.
5
+ #
6
+ # Note: extended attribute keys are expected to be strings rather than symbols.
7
+ #
8
+ class DefaultInterpreter < Tdc::ExtendedAttributes::InterpreterBase
9
+ def interpret(instance_definition)
10
+ extended_attribute_definitions = keep_extended_attributes_from(instance_definition)
11
+
12
+ extended_attribute_definitions.each do |extended_attribute_key, extended_attribute_value|
13
+ # Remove the extended attribute.
14
+ instance_definition.delete(extended_attribute_key)
15
+
16
+ # Add the extended attribute back as a standard attribute.
17
+ instance_definition[convert_to_standard_attribute(extended_attribute_key)] =
18
+ extended_attribute_context.instance_eval(extended_attribute_value)
19
+ end
20
+ end
21
+
22
+ concerning :HookMethods do
23
+ def convert_to_standard_attribute(extended_attribute_key)
24
+ extended_attribute_key.gsub(/_(at|date|on)x$/, "_\\1")
25
+ end
26
+
27
+ def extended_attribute_context
28
+ Time.zone
29
+ end
30
+
31
+ def extended_attribute?(extended_attribute_key)
32
+ /_(at|date|on)x$/ =~ extended_attribute_key
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def keep_extended_attributes_from(instance_definition)
39
+ instance_definition.select { |extended_attribute_key, _| extended_attribute?(extended_attribute_key) }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ module Tdc
2
+ module ExtendedAttributes
3
+ class InterpreterBase
4
+ def interpret(_instance_definition)
5
+ raise Tdc::MissingOverrideError, "Implement the 'keep_extended_attributes' method"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ module Tdc
2
+ module ExtendedAttributes
3
+ #
4
+ # Knows the class instances that interpret extended attribute values.
5
+ #
6
+ class InterpreterRegistry
7
+ include Singleton
8
+
9
+ def self.register(interpreter:)
10
+ instance.register_interpreter(interpreter)
11
+ end
12
+
13
+ def initialize
14
+ @interpreters = []
15
+ end
16
+
17
+ def interpreters
18
+ @interpreters.empty? ? [default_interpreter] : @interpreters
19
+ end
20
+
21
+ def register_interpreter(interpreter)
22
+ raise Tdc::FatalError, <<~MSG.chomp unless interpreter.is_a?(Tdc::ExtendedAttributes::InterpreterBase)
23
+ Cannot register an interpreter unless it inherits from Tdc::ExtendedAttributes::InterpreterBase
24
+ MSG
25
+
26
+ @interpreters << interpreter
27
+ end
28
+
29
+ private
30
+
31
+ def default_interpreter
32
+ @_default_interpreter ||= Tdc::ExtendedAttributes::DefaultInterpreter.new
33
+ end
34
+ end
35
+ end
36
+ end
@@ -13,10 +13,14 @@ module Tdc
13
13
  to_h.empty?
14
14
  end
15
15
 
16
+ def first
17
+ to_h.first&.second
18
+ end
19
+
16
20
  def single_entry
17
21
  raise Tdc::FatalError, "There is more than one entry" if to_h.many?
18
22
 
19
- to_h.first.second
23
+ first
20
24
  end
21
25
  end
22
26
  end
@@ -1,11 +1,15 @@
1
1
  module Tdc
2
2
  module Generators
3
3
  #
4
- # Abstract class for defining generators that define a collection of model instances.
4
+ # Knows how to provide a configurable instance definition.
5
5
  #
6
- # See also SingularGenerator.
7
- #
8
- class StandardGenerator < Tdc::Generators::ConfigurableGenerator
6
+ class StandardGenerator < Tdc::Generators::GeneratorBase
7
+ include Tdc::Generators::DefinitionSourcable
8
+
9
+ attr_reader :instance_definition
10
+
11
+ source_definition_from :instance_definition
12
+
9
13
  def generate
10
14
  CatalogEntries.new.tap do |catalog_entries|
11
15
  instance_definitions.each do |instance_definition|
@@ -21,6 +25,43 @@ module Tdc
21
25
  end
22
26
  end
23
27
  end
28
+
29
+ def run_resolvers_and_generate_instance
30
+ run_extended_attribute_interpreters(instance_definition)
31
+ run_definition_resolvers(instance_definition)
32
+
33
+ generate_instance
34
+ end
35
+
36
+ #
37
+ # Hook method: subclasses are expected to define how to generate a model instance.
38
+ #
39
+ def generate_instance
40
+ raise Tdc::MissingOverrideError, "Implement the 'generate_instance' method"
41
+ end
42
+
43
+ #
44
+ # Hook method: subclasses may include the DefinitionResolvable concern to override.
45
+ #
46
+ def run_definition_resolvers(_instance_definition)
47
+ # Do nothing
48
+ end
49
+
50
+ private
51
+
52
+ def configure_instance_definition(instance_definition)
53
+ @instance_definition = instance_definition
54
+
55
+ configure_definition_source(instance_definition)
56
+ end
57
+
58
+ def run_extended_attribute_interpreters(instance_definition)
59
+ interpreters.each { |interpreter| interpreter.interpret(instance_definition) }
60
+ end
61
+
62
+ def interpreters
63
+ Tdc::ExtendedAttributes::InterpreterRegistry.instance.interpreters
64
+ end
24
65
  end
25
66
  end
26
67
  end
@@ -1,3 +1,3 @@
1
1
  module Tdc
2
- VERSION = "0.3.6.1"
2
+ VERSION = "0.4.1"
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.3.6.1
4
+ version: 0.4.1
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-08-10 00:00:00.000000000 Z
11
+ date: 2020-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -131,11 +131,13 @@ files:
131
131
  - lib/tdc/definition_resolvers.rb
132
132
  - lib/tdc/definition_resolvers/definition_resolver.rb
133
133
  - lib/tdc/definition_resolvers/tag_resolver.rb
134
+ - lib/tdc/extended_attributes.rb
135
+ - lib/tdc/extended_attributes/default_interpreter.rb
136
+ - lib/tdc/extended_attributes/interpreter_base.rb
137
+ - lib/tdc/extended_attributes/interpreter_registry.rb
134
138
  - lib/tdc/fatal_error.rb
135
139
  - lib/tdc/generators.rb
136
- - lib/tdc/generators/atx_context_factory.rb
137
140
  - lib/tdc/generators/catalog_entries.rb
138
- - lib/tdc/generators/configurable_generator.rb
139
141
  - lib/tdc/generators/definition_resolvable.rb
140
142
  - lib/tdc/generators/definition_sourcable.rb
141
143
  - lib/tdc/generators/generation_context.rb
@@ -1,16 +0,0 @@
1
- module Tdc
2
- module Generators
3
- #
4
- # Knows a class instance that supplies the instance_eval context for interpreting _atx attribute values.
5
- #
6
- class AtxContextFactory
7
- include Singleton
8
-
9
- attr_reader :context
10
-
11
- def initialize
12
- @context = Time.zone
13
- end
14
- end
15
- end
16
- end
@@ -1,59 +0,0 @@
1
- module Tdc
2
- module Generators
3
- #
4
- # Knows how to provide a configurable instance definition.
5
- #
6
- class ConfigurableGenerator < Tdc::Generators::GeneratorBase
7
- include Tdc::Generators::DefinitionSourcable
8
-
9
- attr_reader :instance_definition
10
-
11
- source_definition_from :instance_definition
12
-
13
- def run_resolvers_and_generate_instance
14
- run_atx_resolvers(instance_definition)
15
- run_definition_resolvers(instance_definition)
16
-
17
- generate_instance
18
- end
19
-
20
- #
21
- # Hook method: subclasses are expected to define how to generate a model instance.
22
- #
23
- def generate_instance
24
- raise Tdc::MissingOverrideError, "Implement the 'generate_instance' method"
25
- end
26
-
27
- #
28
- # Hook method: subclasses may include the DefinitionResolvable concern to override.
29
- #
30
- def run_definition_resolvers(_instance_definition)
31
- # Do nothing
32
- end
33
-
34
- private
35
-
36
- def configure_instance_definition(instance_definition)
37
- @instance_definition = instance_definition
38
-
39
- configure_definition_source(instance_definition)
40
- end
41
-
42
- def run_atx_resolvers(instance_definition)
43
- atx_definitions = instance_definition.select { |k, _v| /_atx$/ =~ k }
44
-
45
- atx_definitions.each do |k, v|
46
- # Remove the original _atx attribute.
47
- instance_definition.delete(k)
48
-
49
- # Add a standard _at attribute.
50
- instance_definition[k.delete_suffix("x")] = atx_context.instance_eval(v)
51
- end
52
- end
53
-
54
- def atx_context
55
- Tdc::Generators::AtxContextFactory.instance.context
56
- end
57
- end
58
- end
59
- end