config_templates 1.0.0 → 1.1.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
  SHA1:
3
- metadata.gz: 2fea41b43ab1e4aa04064072c84739edaae30424
4
- data.tar.gz: 1f983e8528f0bdec2a5d472fd9fe8dce2af49bb4
3
+ metadata.gz: e66c47dca61de3ce9695b3650d08c201bed7f1c1
4
+ data.tar.gz: 6526fa9950e98b668a134af33f2e49098b2bfd75
5
5
  SHA512:
6
- metadata.gz: f53d36f5f37d87ed7cc3d4fea3b8ca1244db8e2d6947e720051d5d332c1d742418768b5b1410cfbff9f71613791cb2acec65ac36cf2d2b618b521427533dbc63
7
- data.tar.gz: ef51cafcc3f71dc9c7849bd8d939e831774bba20bbba3f44ec374a6af60ec41c76adbed99853cf191a102d22d2e9b71f58e345a0a3282785c8d373f0b1483e68
6
+ metadata.gz: ac03e9885abf6a5bcc39b18630a7add0913fa4b04924fde186b86a651d970550fa6b65ca936fbabaee7361c0b2a0b27da98ae06c53b8da54b8ef612f9245a35e
7
+ data.tar.gz: b2c4f27c584a088e5aa110752e5584fc983c9cf9911ebfedcf5cf74c7678704f9831b04e511766cf2b49d3c603ac8b326ab0c800f3299df41e093edb42c16779
data/lib/autoload.rb CHANGED
@@ -1,12 +1,9 @@
1
- require 'erb'
2
- require 'yaml'
3
- require 'fileutils'
4
-
5
1
  module ConfigTemplates
6
2
  module Contexts; end
7
3
  module Criteria; end
8
4
  module Engines; end
9
- module Models; end
5
+ module Filesystem; end
6
+ module Templates; end
10
7
  module Outputs; end
11
8
  module Repositories; end
12
9
  module Validators; end
@@ -1,13 +1,16 @@
1
- require_relative 'autoload'
1
+ require 'erb'
2
+ require 'yaml'
3
+ require 'fileutils'
2
4
 
3
5
  module ConfigTemplates
4
- class << self
5
- def configure
6
- yield config
7
- end
6
+ def self.configure
7
+ yield container.resolve(:config)
8
+ end
8
9
 
9
- def config
10
- @config ||= Config.new
11
- end
10
+ def self.container
11
+ @container ||= ::Dry::Container.new
12
12
  end
13
13
  end
14
+
15
+ require_relative 'ioc_config'
16
+ require_relative 'autoload'
@@ -1,15 +1,21 @@
1
1
  module ConfigTemplates
2
2
  class Config
3
+ include ConfigTemplates::Inject[
4
+ 'repositories.validators',
5
+ 'repositories.engines',
6
+ 'repositories.outputs'
7
+ ]
8
+
3
9
  attr_accessor :templates_path, :destination_path
4
10
  attr_accessor :settings_path, :settings_file_basename
5
11
  attr_accessor :stages
6
12
  attr_reader :stage
7
13
 
8
- def initialize
14
+ def initialize(validators, engines, outputs)
9
15
  @stages = []
10
- @outputs = ::ConfigTemplates::Repositories::Outputs.new
11
- @engines = ::ConfigTemplates::Repositories::Engines.new
12
- @validators = ::ConfigTemplates::Repositories::Validators.new
16
+ @engines = engines
17
+ @outputs = outputs
18
+ @validators = validators
13
19
  end
14
20
 
15
21
  def stage=(stage)
@@ -20,26 +26,14 @@ module ConfigTemplates
20
26
  end
21
27
  end
22
28
 
23
- def output(name)
24
- @outputs.find_by_name name
25
- end
26
-
27
29
  def outputs(outputs)
28
30
  @outputs.add outputs
29
31
  end
30
32
 
31
- def engine(extension)
32
- @engines.find_by_extension extension
33
- end
34
-
35
33
  def engines(engines)
36
34
  @engines.add engines
37
35
  end
38
36
 
39
- def validator(file_name)
40
- @validators.find_by_file_name file_name
41
- end
42
-
43
37
  def validators(validators)
44
38
  @validators.add validators
45
39
  end
@@ -1,8 +1,15 @@
1
1
  module ConfigTemplates::Contexts
2
2
  class Compilation
3
- def initialize
4
- @templates = ::ConfigTemplates::Repositories::Templates.new
5
- @settings = ::ConfigTemplates::Repositories::Settings.new
3
+ include ConfigTemplates::Inject[
4
+ 'repositories.templates',
5
+ 'repositories.validators',
6
+ 'repositories.engines'
7
+ ]
8
+
9
+ def initialize(templates, validators, engines)
10
+ @templates = templates
11
+ @validators = validators
12
+ @engines = engines
6
13
  @criteria = ::ConfigTemplates::Criteria::Composite.new
7
14
  end
8
15
 
@@ -10,16 +17,16 @@ module ConfigTemplates::Contexts
10
17
  @criteria = ::ConfigTemplates::Criteria::Composite.new @criteria, criteria
11
18
  end
12
19
 
13
- def renderers
20
+ def components
14
21
  @templates.find_all_by(@criteria).map do |template|
15
- ::ConfigTemplates::Models::Renderer.new template, renderer_context
22
+ ::ConfigTemplates::Models::Component.new template, context, @validators, @engines
16
23
  end
17
24
  end
18
25
 
19
26
  private
20
27
 
21
- def renderer_context
22
- ::ConfigTemplates::Contexts::Renderer.new @settings
28
+ def context
29
+ ::ConfigTemplates::Contexts::Rendering.new
23
30
  end
24
31
  end
25
32
  end
@@ -1,8 +1,10 @@
1
1
  module ConfigTemplates::Contexts
2
- class Renderer
3
- def initialize(settings)
2
+ class Rendering
3
+ include ::ConfigTemplates::Inject['repositories.settings', 'config']
4
+
5
+ def initialize(settings, config)
4
6
  @settings = settings
5
- @config = ::ConfigTemplates.config
7
+ @config = config
6
8
  end
7
9
 
8
10
  def param(path, default = nil)
@@ -0,0 +1,16 @@
1
+ module ConfigTemplates::Filesystem
2
+ class Locator
3
+ include ::ConfigTemplates::Inject['config']
4
+
5
+ def templates
6
+ ::Dir[::File.join(config.templates_path, '**', '*')].lazy.select { |path| ::File.file? path }
7
+ end
8
+
9
+ def settings
10
+ ::Dir[
11
+ ::File.join(config.settings_path, "#{config.settings_file_basename}.yml"),
12
+ ::File.join(config.settings_path, "#{config.settings_file_basename}.#{config.stage}.yml")
13
+ ]
14
+ end
15
+ end
16
+ end
@@ -1,7 +1,9 @@
1
1
  module ConfigTemplates::Models
2
2
  class Compilation
3
- def initialize
4
- @config = ::ConfigTemplates.config
3
+ include ::ConfigTemplates::Inject['repositories.outputs']
4
+
5
+ def initialize(outputs)
6
+ @outputs = outputs
5
7
  @context = ::ConfigTemplates::Contexts::Compilation.new
6
8
  end
7
9
 
@@ -16,10 +18,10 @@ module ConfigTemplates::Models
16
18
  end
17
19
 
18
20
  def send_to(output_name)
19
- @context.renderers.tap do |renderers|
20
- output = @config.output output_name
21
- renderers.each &:validate!
22
- renderers.each { |renderer| output.print renderer }
21
+ @context.components.tap do |components|
22
+ output = outputs.find_by_name output_name
23
+ components.each &:validate!
24
+ components.each { |component| output.print component }
23
25
  end
24
26
  end
25
27
  end
@@ -0,0 +1,34 @@
1
+ module ConfigTemplates::Models
2
+ class Component
3
+ def initialize(template, context, validators, engines)
4
+ @template = template
5
+ @context = context
6
+ @validators = validators
7
+ @engines = engines
8
+ end
9
+
10
+ def destination
11
+ engine.output_file_name @template.destination
12
+ end
13
+
14
+ def validate!
15
+ unless validator.valid? render
16
+ raise ::ConfigTemplates::Errors::InvalidTemplate, @template.path
17
+ end
18
+ end
19
+
20
+ def render
21
+ @render ||= engine.evaluate @template.content, @context
22
+ end
23
+
24
+ private
25
+
26
+ def validator
27
+ @validator ||= @validators.find_by_file_name @template.path
28
+ end
29
+
30
+ def engine
31
+ @engine ||= @engines.find_by_extension @template.extension
32
+ end
33
+ end
34
+ end
@@ -2,9 +2,9 @@ module ConfigTemplates::Models
2
2
  class Template
3
3
  attr_reader :path
4
4
 
5
- def initialize(path)
5
+ def initialize(path, config)
6
6
  @path = path
7
- @config = ::ConfigTemplates.config
7
+ @config = config
8
8
  end
9
9
 
10
10
  def content
@@ -16,19 +16,7 @@ module ConfigTemplates::Models
16
16
  end
17
17
 
18
18
  def destination
19
- engine.output_file_name ::File.join(
20
- @config.destination_path,
21
- @config.stage.to_s,
22
- @path.sub(@config.templates_path, '')
23
- )
24
- end
25
-
26
- def validator
27
- @config.validator @path
28
- end
29
-
30
- def engine
31
- @config.engine extension
19
+ ::File.join @config.destination_path, @config.stage.to_s, @path.sub(@config.templates_path, '')
32
20
  end
33
21
  end
34
22
  end
@@ -1,9 +1,9 @@
1
1
  module ConfigTemplates::Outputs
2
2
  class Filesystem
3
- def print(renderer)
4
- destination = renderer.template.destination
3
+ def print(component)
4
+ destination = component.destination
5
5
  ::FileUtils.mkdir_p ::File.dirname destination
6
- ::File.write destination, renderer.render
6
+ ::File.write destination, component.render
7
7
  end
8
8
  end
9
9
  end
@@ -1,7 +1,7 @@
1
1
  module ConfigTemplates::Outputs
2
2
  class Stdout
3
- def print(renderer)
4
- ::Kernel.puts renderer.render
3
+ def print(component)
4
+ ::Kernel.puts component.render
5
5
  end
6
6
  end
7
7
  end
@@ -1,7 +1,9 @@
1
1
  module ConfigTemplates::Repositories
2
2
  class Settings
3
- def initialize
4
- @locator = ::ConfigTemplates::FileLocator.new
3
+ include ::ConfigTemplates::Inject['files.locator']
4
+
5
+ def initialize(locator)
6
+ @locator = locator
5
7
  end
6
8
 
7
9
  def find_all
@@ -1,13 +1,16 @@
1
1
  module ConfigTemplates::Repositories
2
2
  class Templates
3
- def initialize
4
- @locator = ::ConfigTemplates::FileLocator.new
3
+ include ::ConfigTemplates::Inject['config', 'files.locator']
4
+
5
+ def initialize(config, locator)
6
+ @config = config
7
+ @locator = locator
5
8
  end
6
9
 
7
10
  def find_all_by(criteria)
8
11
  @locator.templates
9
12
  .select { |filename| criteria.matches? filename }
10
- .map { |filename| ::ConfigTemplates::Models::Template.new filename }
13
+ .map { |filename| ::ConfigTemplates::Models::Template.new filename, @config }
11
14
  end
12
15
  end
13
16
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigTemplates
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
data/lib/ioc_config.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'dry-auto_inject'
2
+ require 'dry-container'
3
+
4
+ ::ConfigTemplates::Inject = ::Dry::AutoInject(::ConfigTemplates.container).args
5
+
6
+ ::ConfigTemplates.container.tap do |container|
7
+ container.register(:config, memoize: true) { ::ConfigTemplates::Config.new }
8
+
9
+ container.namespace :files do
10
+ register(:locator, memoize: true) { ::ConfigTemplates::Filesystem::Locator.new }
11
+ end
12
+
13
+ container.namespace :repositories do
14
+ register(:engines, memoize: true) { ::ConfigTemplates::Repositories::Engines.new }
15
+ register(:outputs, memoize: true) { ::ConfigTemplates::Repositories::Outputs.new }
16
+ register(:settings, memoize: true) { ::ConfigTemplates::Repositories::Settings.new }
17
+ register(:templates, memoize: true) { ::ConfigTemplates::Repositories::Templates.new }
18
+ register(:validators, memoize: true) { ::ConfigTemplates::Repositories::Validators.new }
19
+ end
20
+ end
@@ -11,7 +11,7 @@ RSpec.describe ::ConfigTemplates do
11
11
 
12
12
  it 'renders selected template using configs and environment metadata' do
13
13
  compilation = ::ConfigTemplates::Models::Compilation.new
14
- compilation.reject(/.*/).select('kapacitor/config.erb').send_to(:test)
14
+ compilation.reject(/.*/).select(Regexp.new 'kapacitor/config.erb').send_to(:test)
15
15
  expect(@output_mock.result.strip).to eq('true')
16
16
  end
17
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - g.ivanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-31 00:00:00.000000000 Z
11
+ date: 2018-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-auto_inject
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-container
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -77,15 +105,15 @@ files:
77
105
  - lib/config_templates.rb
78
106
  - lib/config_templates/config.rb
79
107
  - lib/config_templates/contexts/compilation.rb
80
- - lib/config_templates/contexts/renderer.rb
108
+ - lib/config_templates/contexts/rendering.rb
81
109
  - lib/config_templates/criteria/composite.rb
82
110
  - lib/config_templates/criteria/name.rb
83
111
  - lib/config_templates/engines/erb.rb
84
112
  - lib/config_templates/engines/text.rb
85
113
  - lib/config_templates/errors.rb
86
- - lib/config_templates/file_locator.rb
114
+ - lib/config_templates/filesystem/locator.rb
87
115
  - lib/config_templates/models/compilation.rb
88
- - lib/config_templates/models/renderer.rb
116
+ - lib/config_templates/models/component.rb
89
117
  - lib/config_templates/models/template.rb
90
118
  - lib/config_templates/outputs/filesystem.rb
91
119
  - lib/config_templates/outputs/stdout.rb
@@ -96,6 +124,7 @@ files:
96
124
  - lib/config_templates/repositories/validators.rb
97
125
  - lib/config_templates/validators/composite.rb
98
126
  - lib/config_templates/version.rb
127
+ - lib/ioc_config.rb
99
128
  - spec/fixtures/dest/production/kapacitor/config
100
129
  - spec/fixtures/settings/settings.production.yml
101
130
  - spec/fixtures/settings/settings.yml
@@ -1,18 +0,0 @@
1
- module ConfigTemplates
2
- class FileLocator
3
- def initialize
4
- @config = ::ConfigTemplates.config
5
- end
6
-
7
- def templates
8
- ::Dir[::File.join(@config.templates_path, '**', '*')].lazy.select { |path| ::File.file? path }
9
- end
10
-
11
- def settings
12
- ::Dir[
13
- ::File.join(@config.settings_path, "#{@config.settings_file_basename}.yml"),
14
- ::File.join(@config.settings_path, "#{@config.settings_file_basename}.#{@config.stage}.yml")
15
- ]
16
- end
17
- end
18
- end
@@ -1,20 +0,0 @@
1
- module ConfigTemplates::Models
2
- class Renderer
3
- attr_reader :template
4
-
5
- def initialize(template, context)
6
- @template = template
7
- @context = context
8
- end
9
-
10
- def validate!
11
- unless @template.validator.valid? render
12
- raise ::ConfigTemplates::Errors::InvalidTemplate, @template.path
13
- end
14
- end
15
-
16
- def render
17
- @render ||= @template.engine.evaluate @template.content, @context
18
- end
19
- end
20
- end