tdc 0.4.0 → 0.4.4

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: a574acff7829e69b0f0921e3057ed00ccbf149aa6bc23e77585de0c4372183af
4
- data.tar.gz: b8242f2461ab9eab727e3aca43de8b7ad4f31b45435ce67a4e8cd868893263d4
3
+ metadata.gz: 9afbee316bed8c63956e2a3f1626e376101940ecbb83e45fc6f7dd699d626df4
4
+ data.tar.gz: c4f164c51b6421490d157cbf4761ad4d9e634af72a6a52a6bce0443cfd4b32a9
5
5
  SHA512:
6
- metadata.gz: 5bfe223f7dbbe91f3b92e0d6582d0964a5a1f309190a3f787dde1c6c7132678f2c3f1d0c0faf442231a6c1462ee43941dd1b03460dd0d5f5135612998df0a59b
7
- data.tar.gz: 056c5002c9fbc2cd94ec5a9de9a06d229a271867a915a486abc316c245499ae8c3f069ef8a1bfb14c10d895b9e2af3b5a5035b8af086c5d3187a3bf7592829a5
6
+ metadata.gz: fd3f18ae6079f57dce2faa700c27ec64c86f266eb54820a2952beeeefcf3d4e8bcb0a785e69b02b222bcfa29f56ad5484c888dcea3d8d74e58610435ac4273e0
7
+ data.tar.gz: 219c4136fc7dc4717e9c83849d7318cbe05751b661dd9d95bbbaa2cc5a621188a44b13b7e2624c9c80c4126e34ced7274673dabe0af6e62488977cefa26e29ac
@@ -15,12 +15,20 @@ Layout/LineLength:
15
15
  Layout/MultilineMethodCallBraceLayout:
16
16
  Enabled: false
17
17
 
18
+ # Revisit. Seems a little harsh.
19
+ Lint/MissingSuper:
20
+ Enabled: false
21
+
18
22
  Metrics/BlockLength:
19
23
  Enabled: false
20
24
 
21
25
  Naming/MemoizedInstanceVariableName:
22
26
  EnforcedStyleForLeadingUnderscores: required
23
27
 
28
+ Naming/VariableNumber:
29
+ Enabled: true
30
+ EnforcedStyle: snake_case
31
+
24
32
  RSpec/AnyInstance:
25
33
  Enabled: false
26
34
 
@@ -6,6 +6,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.4.4] - 2020-08-25
10
+
11
+ #### Breaking Changes
12
+
13
+ - Enable ERB expansion of the YAML source with a `.yam.erb` filename suffix.
14
+
15
+ ## [0.4.3] - 2020-08-25
16
+
17
+ #### New Features
18
+
19
+ - Support optional definitions
20
+
21
+ ## [0.4.2] - 2020-08-22
22
+
23
+ #### Breaking Changes
24
+
25
+ - Extended attributes use `_xa` suffix
26
+
27
+ ## [0.4.1] - 2020-08-22
28
+
29
+ - Add the `first` method to `CatalogEntries`
30
+
9
31
  ## [0.4.0] - 2020-08-14
10
32
 
11
33
  #### New Features
data/lib/tdc.rb CHANGED
@@ -41,6 +41,14 @@ require "tdc/generators/definition_sourcable"
41
41
  require "tdc/generators/generator_base"
42
42
  require "tdc/generators/standard_generator"
43
43
 
44
+ # YAML Readers
45
+ require "tdc/yaml_readers"
46
+ require "tdc/yaml_readers/null_yaml_reader"
47
+ require "tdc/yaml_readers/yaml_reader_base"
48
+ require "tdc/yaml_readers/yaml_reader"
49
+ require "tdc/yaml_readers/yaml_reader_with_expansion"
50
+ require "tdc/yaml_readers/yaml_reader_factory"
51
+
44
52
  #
45
53
  # A framework for building a Test Data Catalog
46
54
  #
@@ -6,44 +6,13 @@ module Tdc
6
6
  EMPTY_DEFINITIONS = []
7
7
 
8
8
  def initialize(catalog_root_directory)
9
- super()
10
-
11
9
  @catalog_root_directory = catalog_root_directory
12
10
  end
13
11
 
14
12
  def read(*path_elements)
15
- data_definition_from(definitions_file(path_elements))
16
- end
17
-
18
- private
19
-
20
- def definitions_file(path_elements)
21
- fully_qualified_path_elements = [@catalog_root_directory].concat(path_elements.map(&:to_s))
22
-
23
- fully_qualified_path_elements.last.concat(".yml")
24
-
25
- File.join(*fully_qualified_path_elements)
26
- end
27
-
28
- def data_definition_from(definitions_file)
29
- if File.exist?(definitions_file)
30
- load_yaml(definitions_file) || EMPTY_DEFINITIONS
31
- else
32
- EMPTY_DEFINITIONS
33
- end
34
- end
35
-
36
- def load_yaml(definitions_file)
37
- YAML.load(expand_erb(definitions_file)) # rubocop:disable Security/YAMLLoad
38
- rescue => e
39
- raise Tdc::FatalError, <<~MSG
40
- Unable to load YAML from #{definitions_file}
41
- Cause: #{e.message}"
42
- MSG
43
- end
13
+ reader = Tdc::YamlReaders::YamlReaderFactory.new(@catalog_root_directory, path_elements).create
44
14
 
45
- def expand_erb(definitions_file)
46
- ERB.new(File.read(definitions_file)).result
15
+ reader.data_definitions
47
16
  end
48
17
  end
49
18
  end
@@ -8,8 +8,6 @@ module Tdc
8
8
  attr_reader :key, :source
9
9
 
10
10
  def initialize(key:, source:)
11
- super()
12
-
13
11
  @key = key
14
12
  @source = source
15
13
  end
@@ -1,29 +1,45 @@
1
1
  module Tdc
2
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
+ #
3
8
  class DefaultInterpreter < Tdc::ExtendedAttributes::InterpreterBase
4
9
  def interpret(instance_definition)
5
10
  extended_attribute_definitions = keep_extended_attributes(instance_definition)
6
11
 
7
- extended_attribute_definitions.each do |k, v|
8
- # Remove the original extended attribute.
9
- instance_definition.delete(k)
12
+ extended_attribute_definitions.each do |extended_attribute_key, extended_attribute_value|
13
+ # Remove the extended attribute.
14
+ instance_definition.delete(extended_attribute_key)
10
15
 
11
- # Add a standard attribute.
12
- instance_definition[convert_to_standard_attribute(k)] = extended_attribute_context.instance_eval(v)
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)
13
19
  end
14
20
  end
15
21
 
16
22
  concerning :HookMethods do
17
- def convert_to_standard_attribute(k) # rubocop:disable Naming/MethodParameterName
18
- k.delete_suffix("x")
19
- end
20
-
21
23
  def extended_attribute_context
22
24
  Time.zone
23
25
  end
26
+ end
27
+
28
+ private
29
+
30
+ EXTENDED_ATTRIBUTE_SUFFIX = "_xa"
31
+
32
+ def convert_to_standard_attribute(extended_attribute_key)
33
+ extended_attribute_key.delete_suffix(EXTENDED_ATTRIBUTE_SUFFIX)
34
+ end
35
+
36
+ def extended_attribute?(extended_attribute_key)
37
+ extended_attribute_key.end_with?(EXTENDED_ATTRIBUTE_SUFFIX)
38
+ end
24
39
 
25
- def keep_extended_attributes(instance_definition)
26
- instance_definition.select { |k, _v| /_(at|date|on)x$/ =~ k }
40
+ def keep_extended_attributes(instance_definition)
41
+ instance_definition.select do |extended_attribute_key, _|
42
+ extended_attribute?(extended_attribute_key)
27
43
  end
28
44
  end
29
45
  end
@@ -15,18 +15,22 @@ module Tdc
15
15
  end
16
16
 
17
17
  def interpreters
18
- @interpreters << Tdc::ExtendedAttributes::DefaultInterpreter.new if @interpreters.empty?
19
-
20
- @interpreters
18
+ @interpreters.empty? ? [default_interpreter] : @interpreters
21
19
  end
22
20
 
23
21
  def register_interpreter(interpreter)
24
- raise Tdc::FatalError, <<~MSG unless interpreter.is_a?(Tdc::ExtendedAttributes::InterpreterBase)
22
+ raise Tdc::FatalError, <<~MSG.chomp unless interpreter.is_a?(Tdc::ExtendedAttributes::InterpreterBase)
25
23
  Cannot register an interpreter unless it inherits from Tdc::ExtendedAttributes::InterpreterBase
26
24
  MSG
27
25
 
28
26
  @interpreters << interpreter
29
27
  end
28
+
29
+ private
30
+
31
+ def default_interpreter
32
+ @_default_interpreter ||= Tdc::ExtendedAttributes::DefaultInterpreter.new
33
+ end
30
34
  end
31
35
  end
32
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
@@ -3,7 +3,10 @@ module Tdc
3
3
  #
4
4
  # Creates ghost methods for use in generators.
5
5
  #
6
- # All ghost methods are named 'key'_definition where 'key' is a key into the instance_definition hash.
6
+ # All ghost methods are named 'key'_definition or 'key'_definition_optional where 'key' is
7
+ # a key into the instance_definition hash.
8
+ #
9
+ # Choose optional if the key may not be present in the instance_definition.
7
10
  #
8
11
  # Example:
9
12
  #
@@ -11,6 +14,7 @@ module Tdc
11
14
  # ghost methods could be used to refer to the value associated with those keys:
12
15
  #
13
16
  # line_definition
17
+ # line_definition_optional
14
18
  # replenishment_parameters_definition
15
19
  #
16
20
  module DefinitionSourcable
@@ -33,19 +37,25 @@ module Tdc
33
37
  private
34
38
 
35
39
  def method_missing(method, *args)
36
- key = transform_method_to_definition_source_key(method)
37
-
38
- definition_source&.key?(key) ? definition_source.fetch(key) : super
40
+ if ghost_definition?(method)
41
+ definition_source.fetch(method.to_s.gsub(/_definition$/, ""))
42
+ elsif ghost_optional_definition?(method)
43
+ definition_source[method.to_s.gsub(/_definition_optional$/, "")]
44
+ else
45
+ super
46
+ end
39
47
  end
40
48
 
41
49
  def respond_to_missing?(method, include_all = false) # rubocop:disable Style/OptionalBooleanParameter
42
- key = transform_method_to_definition_source_key(method)
50
+ ghost_definition?(method) || ghost_optional_definition?(method) ? true : super
51
+ end
43
52
 
44
- definition_source&.key?(key) ? true : super
53
+ def ghost_definition?(method)
54
+ method.to_s.end_with?("_definition")
45
55
  end
46
56
 
47
- def transform_method_to_definition_source_key(method)
48
- method.to_s.gsub(/_definition$/, "")
57
+ def ghost_optional_definition?(method)
58
+ method.to_s.end_with?("_definition_optional")
49
59
  end
50
60
  end
51
61
  end
@@ -4,8 +4,6 @@ module Tdc
4
4
  #
5
5
  class InMemoryDataDefinition < Tdc::DataDefinition
6
6
  def initialize(path_elements_data = {})
7
- super()
8
-
9
7
  @store = path_elements_data
10
8
  end
11
9
 
@@ -1,3 +1,3 @@
1
1
  module Tdc
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.4"
3
3
  end
@@ -0,0 +1,7 @@
1
+ module Tdc
2
+ #
3
+ # Namespace to host YAML readers.
4
+ #
5
+ module YamlReaders
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Tdc
2
+ module YamlReaders
3
+ #
4
+ # When we can't find a YAML source.
5
+ #
6
+ class NullYamlReader
7
+ def data_definitions
8
+ []
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Tdc
2
+ module YamlReaders
3
+ #
4
+ # YAML source is a YAML file.
5
+ #
6
+ class YamlReader < Tdc::YamlReaders::YamlReaderBase
7
+ def file_extension
8
+ ".yml"
9
+ end
10
+
11
+ def source_string
12
+ File.read(definitions_file)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,52 @@
1
+ module Tdc
2
+ module YamlReaders
3
+ #
4
+ # YAML source.
5
+ #
6
+ class YamlReaderBase
7
+ def initialize(catalog_root_directory, path_elements)
8
+ @catalog_root_directory = catalog_root_directory
9
+ @path_elements = path_elements
10
+ end
11
+
12
+ def applies?
13
+ File.exist?(definitions_file)
14
+ end
15
+
16
+ def data_definitions
17
+ definitions_source
18
+ end
19
+
20
+ def definitions_source
21
+ source_string.empty? ? [] : YAML.load(source_string) # rubocop:disable Security/YAMLLoad
22
+ rescue => e
23
+ raise Tdc::FatalError, <<~MSG
24
+ Unable to load YAML from #{definitions_file}
25
+ Cause: #{e.message}"
26
+ MSG
27
+ end
28
+
29
+ concerning :HookMethods do
30
+ def file_extension
31
+ raise MissingOverrideError, "Implement the 'file_extension' method"
32
+ end
33
+
34
+ def source_string
35
+ raise MissingOverrideError, "Implement the 'source_string' method"
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def definitions_file
42
+ @_definitions_file ||= begin
43
+ fully_qualified_path_elements = [@catalog_root_directory].concat(@path_elements.map(&:to_s))
44
+
45
+ fully_qualified_path_elements.last.concat(file_extension)
46
+
47
+ File.join(*fully_qualified_path_elements)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,38 @@
1
+ module Tdc
2
+ module YamlReaders
3
+ #
4
+ # Knows how to create the appropriate YAML reader.
5
+ #
6
+ class YamlReaderFactory
7
+ def initialize(catalog_root_directory, path_elements)
8
+ @catalog_root_directory = catalog_root_directory
9
+ @path_elements = path_elements
10
+ end
11
+
12
+ def create
13
+ if yaml_reader_with_expansion.applies?
14
+ yaml_reader_with_expansion
15
+ elsif yaml_reader.applies?
16
+ yaml_reader
17
+ else
18
+ null_reader
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def yaml_reader_with_expansion
25
+ @_yaml_reader_with_expansion ||=
26
+ Tdc::YamlReaders::YamlReaderWithExpansion.new(@catalog_root_directory, @path_elements)
27
+ end
28
+
29
+ def null_reader
30
+ @_null_reader ||= Tdc::YamlReaders::NullYamlReader.new
31
+ end
32
+
33
+ def yaml_reader
34
+ @_yaml_reader ||= Tdc::YamlReaders::YamlReader.new(@catalog_root_directory, @path_elements)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ module Tdc
2
+ module YamlReaders
3
+ #
4
+ # YAML source is a YAML file that undergoes ERB expansion.
5
+ #
6
+ class YamlReaderWithExpansion < Tdc::YamlReaders::YamlReaderBase
7
+ def file_extension
8
+ ".yml.erb"
9
+ end
10
+
11
+ def source_string
12
+ ERB.new(File.read(definitions_file)).result
13
+ end
14
+ end
15
+ end
16
+ 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.4.0
4
+ version: 0.4.4
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-14 00:00:00.000000000 Z
11
+ date: 2020-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -147,6 +147,12 @@ files:
147
147
  - lib/tdc/missing_override_error.rb
148
148
  - lib/tdc/version.rb
149
149
  - lib/tdc/with_indifferent_access_decorator.rb
150
+ - lib/tdc/yaml_readers.rb
151
+ - lib/tdc/yaml_readers/null_yaml_reader.rb
152
+ - lib/tdc/yaml_readers/yaml_reader.rb
153
+ - lib/tdc/yaml_readers/yaml_reader_base.rb
154
+ - lib/tdc/yaml_readers/yaml_reader_factory.rb
155
+ - lib/tdc/yaml_readers/yaml_reader_with_expansion.rb
150
156
  - tdc.gemspec
151
157
  homepage: https://github.com/nulogy/tdc
152
158
  licenses: