config_templates 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/autoload.rb +2 -1
  3. data/lib/config_templates/collections/components.rb +30 -0
  4. data/lib/config_templates/{repositories → collections}/engines.rb +2 -2
  5. data/lib/config_templates/collections/extensions.rb +28 -0
  6. data/lib/config_templates/{repositories → collections}/outputs.rb +1 -1
  7. data/lib/config_templates/collections/settings.rb +15 -0
  8. data/lib/config_templates/collections/templates.rb +16 -0
  9. data/lib/config_templates/collections/validators.rb +23 -0
  10. data/lib/config_templates/config.rb +10 -4
  11. data/lib/config_templates/contexts/compilation.rb +11 -7
  12. data/lib/config_templates/contexts/rendering.rb +20 -19
  13. data/lib/config_templates/criteria/composite.rb +3 -3
  14. data/lib/config_templates/criteria/name.rb +2 -2
  15. data/lib/config_templates/criteria/path.rb +11 -0
  16. data/lib/config_templates/directives/include.rb +9 -0
  17. data/lib/config_templates/directives/invocation.rb +11 -0
  18. data/lib/config_templates/directives/stage.rb +23 -0
  19. data/lib/config_templates/engines/erb.rb +3 -3
  20. data/lib/config_templates/engines/text.rb +2 -2
  21. data/lib/config_templates/errors.rb +1 -0
  22. data/lib/config_templates/models/compilation.rb +6 -5
  23. data/lib/config_templates/models/component.rb +19 -16
  24. data/lib/config_templates/models/template.rb +7 -7
  25. data/lib/config_templates/outputs/filesystem.rb +4 -4
  26. data/lib/config_templates/outputs/stdout.rb +1 -1
  27. data/lib/config_templates/version.rb +1 -1
  28. data/lib/config_templates.rb +1 -0
  29. data/lib/ioc_config.rb +7 -6
  30. data/spec/fixtures/src/kapacitor/config.erb +1 -3
  31. data/spec/fixtures/src/kapacitor/include.erb +3 -0
  32. data/spec/mocks/outputs/test.rb +1 -1
  33. data/spec/mocks/validators/test.rb +15 -0
  34. data/spec/spec_helper.rb +1 -0
  35. data/spec/templates_spec.rb +25 -7
  36. metadata +31 -7
  37. data/lib/config_templates/repositories/settings.rb +0 -15
  38. data/lib/config_templates/repositories/templates.rb +0 -16
  39. data/lib/config_templates/repositories/validators.rb +0 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4377efa926e6aa4db122568f3f249b25a53ee464
4
- data.tar.gz: 7858dfa79ce81a7b0f0fbcb67cb0f042c752807f
3
+ metadata.gz: 2819660249db42a27e0a885f569a1a9cd94f8d44
4
+ data.tar.gz: ea65ab18c2b310e9dabeba9f515037bc501dd5cc
5
5
  SHA512:
6
- metadata.gz: 5e799bf5d17e17dc54dd562042281a9202f70a9e426ac7baf88385593cfd5ab61306e754071d07e7ed23c70e290c900e8e4aea4eac8ddce2af3444f3b08abc87
7
- data.tar.gz: 2f2cc113deea825c4b8f958393bd7c102966326978bd435a143c8c83068a44d4e2b22608cc9ad92e73f4bf413502548d4ac28132f4da6dc54ac8b7db1820ee26
6
+ metadata.gz: c110fccc3f5ddddf266a0de407ca7fd36cd6e2c2a5c92d5da5891b6822b774e39e8cbc9c838b425a5e932d3a67af17d3e16a6ca6e5192f1519bc7eab0dcbe353
7
+ data.tar.gz: e857341a1314ff40eddea158e5f9a7d6c7a0e08999b5d7687bd8bf8647ef0a0e226f9b3bd1b19c363a505cf964b9dd824f06ef81ad128e49d9b663ac5322c9ff
data/lib/autoload.rb CHANGED
@@ -5,8 +5,9 @@ module ConfigTemplates
5
5
  module Filesystem; end
6
6
  module Models; end
7
7
  module Outputs; end
8
- module Repositories; end
8
+ module Collections; end
9
9
  module Validators; end
10
+ module Extensions; end
10
11
  end
11
12
 
12
13
  Dir[
@@ -0,0 +1,30 @@
1
+ module ConfigTemplates::Collections
2
+ class Components
3
+ def initialize(criteria)
4
+ @criteria = criteria
5
+ @components = {}
6
+ end
7
+
8
+ def <<(component)
9
+ @components[component.source_path] = component
10
+ end
11
+
12
+ def find_all
13
+ @criteria.filter(@components).lazy
14
+ .reject { |_, component| component.child? }
15
+ .map { |_, component| component }
16
+ end
17
+
18
+ def find_by_path!(path)
19
+ find_by! ConfigTemplates::Criteria::Path.new path
20
+ rescue
21
+ raise ConfigTemplates::Errors::ComponentNotFound, path
22
+ end
23
+
24
+ def find_by!(criteria)
25
+ criteria.filter(@components).first.last
26
+ rescue
27
+ raise ConfigTemplates::Errors::ComponentNotFound
28
+ end
29
+ end
30
+ end
@@ -1,4 +1,4 @@
1
- module ConfigTemplates::Repositories
1
+ module ConfigTemplates::Collections
2
2
  class Engines
3
3
  def initialize
4
4
  @default = ::ConfigTemplates::Engines::Text.new
@@ -14,7 +14,7 @@ module ConfigTemplates::Repositories
14
14
  end
15
15
 
16
16
  def find_by(criteria)
17
- @engines.select { |(k, _)| criteria.matches?(k) }.first.last
17
+ criteria.filter(@engines).first.last
18
18
  rescue
19
19
  @default
20
20
  end
@@ -0,0 +1,28 @@
1
+ module ConfigTemplates::Collections
2
+ class Extensions
3
+ def initialize
4
+ @extensions = {
5
+ /^.+\?$/ => ::ConfigTemplates::Extensions::Stage,
6
+ 'include' => ::ConfigTemplates::Extensions::Include
7
+ }
8
+ end
9
+
10
+ def add(extensions)
11
+ @extensions.merge! extensions
12
+ end
13
+
14
+ def exists_with_name?(name)
15
+ find_by_name(name) != nil
16
+ end
17
+
18
+ def find_by_name(name)
19
+ find_by ::ConfigTemplates::Criteria::Name.new name
20
+ end
21
+
22
+ def find_by(criteria)
23
+ criteria.filter(@extensions).first.last
24
+ rescue
25
+ nil
26
+ end
27
+ end
28
+ end
@@ -1,4 +1,4 @@
1
- module ConfigTemplates::Repositories
1
+ module ConfigTemplates::Collections
2
2
  class Outputs
3
3
  def initialize
4
4
  @default = :stdout
@@ -0,0 +1,15 @@
1
+ module ConfigTemplates::Collections
2
+ class Settings
3
+ include ::ConfigTemplates::Inject['files.locator']
4
+
5
+ def initialize(locator)
6
+ @locator = locator
7
+ end
8
+
9
+ def find_all
10
+ @find_all ||= @locator.settings.reduce({}) do |result, current|
11
+ ::DeepMerge.deep_merge! ::YAML.load_file(current, Hash.new), result
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module ConfigTemplates::Collections
2
+ class Templates
3
+ include ::ConfigTemplates::Inject['config', 'files.locator']
4
+
5
+ def initialize(config, locator)
6
+ @config = config
7
+ @locator = locator
8
+ end
9
+
10
+ def find_all
11
+ @locator.templates.map do |path|
12
+ ::ConfigTemplates::Models::Template.new path, @config
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module ConfigTemplates::Collections
2
+ class Validators
3
+ def initialize
4
+ @validators = {}
5
+ end
6
+
7
+ def add(validators)
8
+ @validators.merge! validators
9
+ end
10
+
11
+ def find_by_file_name(file_name)
12
+ ::ConfigTemplates::Validators::Composite.new find_all_by_file_name file_name
13
+ end
14
+
15
+ def find_all_by_file_name(file_name)
16
+ find_all_by ::ConfigTemplates::Criteria::Name.new file_name
17
+ end
18
+
19
+ def find_all_by(criteria)
20
+ criteria.filter(@validators).values
21
+ end
22
+ end
23
+ end
@@ -1,9 +1,10 @@
1
1
  module ConfigTemplates
2
2
  class Config
3
3
  include ConfigTemplates::Inject[
4
- 'repositories.validators',
5
- 'repositories.engines',
6
- 'repositories.outputs'
4
+ 'collections.extensions',
5
+ 'collections.validators',
6
+ 'collections.engines',
7
+ 'collections.outputs'
7
8
  ]
8
9
 
9
10
  attr_accessor :templates_path, :destination_path
@@ -11,11 +12,12 @@ module ConfigTemplates
11
12
  attr_accessor :stages
12
13
  attr_reader :stage
13
14
 
14
- def initialize(validators, engines, outputs)
15
+ def initialize(extensions, validators, engines, outputs)
15
16
  @stages = []
16
17
  @engines = engines
17
18
  @outputs = outputs
18
19
  @validators = validators
20
+ @extensions = extensions
19
21
  end
20
22
 
21
23
  def stage=(stage)
@@ -37,5 +39,9 @@ module ConfigTemplates
37
39
  def validators(validators)
38
40
  @validators.add validators
39
41
  end
42
+
43
+ def extensions(extensions)
44
+ @extensions.add extensions
45
+ end
40
46
  end
41
47
  end
@@ -1,9 +1,9 @@
1
1
  module ConfigTemplates::Contexts
2
2
  class Compilation
3
3
  include ConfigTemplates::Inject[
4
- 'repositories.templates',
5
- 'repositories.validators',
6
- 'repositories.engines'
4
+ 'collections.templates',
5
+ 'collections.validators',
6
+ 'collections.engines'
7
7
  ]
8
8
 
9
9
  def initialize(templates, validators, engines)
@@ -18,15 +18,19 @@ module ConfigTemplates::Contexts
18
18
  end
19
19
 
20
20
  def components
21
- @templates.find_all_by(@criteria).map do |template|
22
- ::ConfigTemplates::Models::Component.new template, context, @validators, @engines
21
+ ::ConfigTemplates::Collections::Components.new(@criteria).tap do |collection|
22
+ context = ::ConfigTemplates::Contexts::Rendering.new
23
+ context.components = collection
24
+ @templates.find_all.each { |template| collection << component(template, context) }
23
25
  end
24
26
  end
25
27
 
26
28
  private
27
29
 
28
- def context
29
- ::ConfigTemplates::Contexts::Rendering.new
30
+ def component(template, context)
31
+ validator = @validators.find_by_file_name template.source_path
32
+ engine = @engines.find_by_extension template.extension
33
+ ::ConfigTemplates::Models::Component.new template, context, validator, engine
30
34
  end
31
35
  end
32
36
  end
@@ -1,14 +1,25 @@
1
1
  module ConfigTemplates::Contexts
2
2
  class Rendering
3
- include ::ConfigTemplates::Inject['repositories.settings', 'config']
3
+ include ::ConfigTemplates::Inject[
4
+ 'collections.extensions',
5
+ 'collections.settings',
6
+ 'config'
7
+ ]
4
8
 
5
- def initialize(settings, config)
9
+ attr_accessor :components
10
+
11
+ def initialize(extensions, settings, config)
12
+ @extensions = extensions
6
13
  @settings = settings
7
14
  @config = config
8
15
  end
9
16
 
10
- def param(path, default = nil)
11
- path.split('.').inject(@settings.find_all) { |value, current| value[current] }
17
+ def binding
18
+ ::Kernel.binding
19
+ end
20
+
21
+ def param(xpath, default = nil)
22
+ xpath.split('.').reduce(@settings.find_all) { |value, key| value.fetch key }
12
23
  rescue
13
24
  default
14
25
  end
@@ -17,24 +28,14 @@ module ConfigTemplates::Contexts
17
28
  @config.stage
18
29
  end
19
30
 
20
- def method_missing(method_name)
21
- stage_request = method_name[-1] == '?'
22
- stage = method_name[0..-2] if stage_request
23
- stage.nil? ? super : stage?(stage)
31
+ def method_missing(method_name, *args, &block)
32
+ extension_class = @extensions.find_by_name method_name
33
+ invocation = ::ConfigTemplates::Extensions::Invocation.new method_name, args, block
34
+ extension_class.new.call(self, invocation) rescue super
24
35
  end
25
36
 
26
37
  def respond_to_missing?(method_name)
27
- method_name[-1] == '?' || super
28
- end
29
-
30
- private
31
-
32
- def stage?(stage)
33
- if @config.stages.include?(stage.to_sym)
34
- @config.stage == stage.to_sym
35
- else
36
- raise ::ConfigTemplates::Errors::StageNotFound, stage
37
- end
38
+ @extensions.exists_with_name?(method_name) || super
38
39
  end
39
40
  end
40
41
  end
@@ -4,9 +4,9 @@ module ConfigTemplates::Criteria
4
4
  @criteria = criteria
5
5
  end
6
6
 
7
- def matches?(target)
8
- @criteria.inject(false) do |result, criteria|
9
- result || criteria.matches?(target)
7
+ def filter(hash)
8
+ @criteria.reduce({}) do |result, criteria|
9
+ result.merge criteria.filter hash
10
10
  end
11
11
  end
12
12
  end
@@ -5,8 +5,8 @@ module ConfigTemplates::Criteria
5
5
  @straight = straight
6
6
  end
7
7
 
8
- def matches?(expression)
9
- @pattern.match?(cast expression) && @straight
8
+ def filter(hash)
9
+ hash.select { |pattern| @pattern.match?(cast pattern) && @straight }
10
10
  end
11
11
 
12
12
  def cast(expression)
@@ -0,0 +1,11 @@
1
+ module ConfigTemplates::Criteria
2
+ class Path
3
+ def initialize(path)
4
+ @path = path
5
+ end
6
+
7
+ def filter(hash)
8
+ hash.key?(@path) ? { @path => hash[@path] } : {}
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module ConfigTemplates::Extensions
2
+ class Include
3
+ def call(context, invocation)
4
+ component = context.components.find_by_path! invocation.args.first
5
+ component.child!
6
+ component.render
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module ConfigTemplates::Extensions
2
+ class Invocation
3
+ attr_reader :method, :args, :block
4
+
5
+ def initialize(method, args, block)
6
+ @method = method
7
+ @args = args
8
+ @block = block
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module ConfigTemplates::Extensions
2
+ class Stage
3
+ include ::ConfigTemplates::Inject['config']
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ end
8
+
9
+ def call(_, invocation)
10
+ check invocation.method[0..-2]
11
+ end
12
+
13
+ private
14
+
15
+ def check(stage)
16
+ if @config.stages.include?(stage.to_sym)
17
+ @config.stage == stage.to_sym
18
+ else
19
+ raise ::ConfigTemplates::Errors::StageNotFound, stage
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,11 +1,11 @@
1
1
  module ConfigTemplates::Engines
2
2
  class ERB
3
3
  def evaluate(template, context)
4
- ::ERB.new(template).result(context.instance_eval { binding })
4
+ ::ERB.new(template).result context.binding
5
5
  end
6
6
 
7
- def output_file_name(file_name)
8
- file_name.sub '.erb', ''
7
+ def destination_path(path)
8
+ path.sub '.erb', ''
9
9
  end
10
10
  end
11
11
  end
@@ -4,8 +4,8 @@ module ConfigTemplates::Engines
4
4
  template
5
5
  end
6
6
 
7
- def output_file_name(file_name)
8
- file_name
7
+ def destination_path(path)
8
+ path
9
9
  end
10
10
  end
11
11
  end
@@ -1,4 +1,5 @@
1
1
  module ConfigTemplates::Errors
2
2
  class StageNotFound < RuntimeError; end
3
+ class ComponentNotFound < RuntimeError; end
3
4
  class InvalidTemplate < RuntimeError; end
4
5
  end
@@ -1,6 +1,6 @@
1
1
  module ConfigTemplates::Models
2
2
  class Compilation
3
- include ::ConfigTemplates::Inject['repositories.outputs']
3
+ include ::ConfigTemplates::Inject['collections.outputs']
4
4
 
5
5
  def initialize(outputs)
6
6
  @outputs = outputs
@@ -18,10 +18,11 @@ module ConfigTemplates::Models
18
18
  end
19
19
 
20
20
  def send_to(output_name)
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 }
21
+ @context.components.tap do |collection|
22
+ collection.find_all.each &:render
23
+ collection.find_all.each &:validate!
24
+ output = @outputs.find_by_name output_name
25
+ collection.find_all.each { |component| output.write component }
25
26
  end
26
27
  end
27
28
  end
@@ -1,34 +1,37 @@
1
1
  module ConfigTemplates::Models
2
2
  class Component
3
- def initialize(template, context, validators, engines)
3
+ def initialize(template, context, validator, engine)
4
4
  @template = template
5
5
  @context = context
6
- @validators = validators
7
- @engines = engines
6
+ @validator = validator
7
+ @engine = engine
8
+ @child = false
8
9
  end
9
10
 
10
- def destination
11
- engine.output_file_name @template.destination
11
+ def child?
12
+ @child
12
13
  end
13
14
 
14
- def validate!
15
- unless validator.valid? render
16
- raise ::ConfigTemplates::Errors::InvalidTemplate, @template.path
17
- end
15
+ def child!
16
+ @child = true
18
17
  end
19
18
 
20
- def render
21
- @render ||= engine.evaluate @template.content, @context
19
+ def source_path
20
+ @template.source_path
22
21
  end
23
22
 
24
- private
23
+ def destination_path
24
+ @engine.destination_path @template.destination_path
25
+ end
25
26
 
26
- def validator
27
- @validator ||= @validators.find_by_file_name @template.path
27
+ def validate!
28
+ unless @validator.valid? render
29
+ raise ::ConfigTemplates::Errors::InvalidTemplate, source_path
30
+ end
28
31
  end
29
32
 
30
- def engine
31
- @engine ||= @engines.find_by_extension @template.extension
33
+ def render
34
+ @render ||= @engine.evaluate @template.content, @context
32
35
  end
33
36
  end
34
37
  end
@@ -1,22 +1,22 @@
1
1
  module ConfigTemplates::Models
2
2
  class Template
3
- attr_reader :path
3
+ attr_reader :source_path
4
4
 
5
- def initialize(path, config)
6
- @path = path
5
+ def initialize(source_path, config)
6
+ @source_path = source_path
7
7
  @config = config
8
8
  end
9
9
 
10
10
  def content
11
- @content ||= ::File.read @path
11
+ @content ||= ::File.read @source_path
12
12
  end
13
13
 
14
14
  def extension
15
- ::File.extname @path
15
+ ::File.extname @source_path
16
16
  end
17
17
 
18
- def destination
19
- ::File.join @config.destination_path, @config.stage.to_s, @path.sub(@config.templates_path, '')
18
+ def destination_path
19
+ ::File.join @config.destination_path, @config.stage.to_s, @source_path.sub(@config.templates_path, '')
20
20
  end
21
21
  end
22
22
  end
@@ -1,9 +1,9 @@
1
1
  module ConfigTemplates::Outputs
2
2
  class Filesystem
3
- def print(component)
4
- destination = component.destination
5
- ::FileUtils.mkdir_p ::File.dirname destination
6
- ::File.write destination, component.render
3
+ def write(component)
4
+ destination_path = component.destination_path
5
+ ::FileUtils.mkdir_p ::File.dirname destination_path
6
+ ::File.write destination_path, component.render
7
7
  end
8
8
  end
9
9
  end
@@ -1,6 +1,6 @@
1
1
  module ConfigTemplates::Outputs
2
2
  class Stdout
3
- def print(component)
3
+ def write(component)
4
4
  ::Kernel.puts component.render
5
5
  end
6
6
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigTemplates
2
- VERSION = '1.1.1'.freeze
2
+ VERSION = '1.1.2'.freeze
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'erb'
2
2
  require 'yaml'
3
3
  require 'fileutils'
4
+ require 'deep_merge/core'
4
5
 
5
6
  module ConfigTemplates
6
7
  def self.configure
data/lib/ioc_config.rb CHANGED
@@ -10,11 +10,12 @@ require 'dry-container'
10
10
  register(:locator, memoize: true) { ::ConfigTemplates::Filesystem::Locator.new }
11
11
  end
12
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 }
13
+ container.namespace :collections do
14
+ register(:engines, memoize: true) { ::ConfigTemplates::Collections::Engines.new }
15
+ register(:outputs, memoize: true) { ::ConfigTemplates::Collections::Outputs.new }
16
+ register(:settings, memoize: true) { ::ConfigTemplates::Collections::Settings.new }
17
+ register(:templates, memoize: true) { ::ConfigTemplates::Collections::Templates.new }
18
+ register(:validators, memoize: true) { ::ConfigTemplates::Collections::Validators.new }
19
+ register(:extensions, memoize: true) { ::ConfigTemplates::Collections::Extensions.new }
19
20
  end
20
21
  end
@@ -1,3 +1 @@
1
- <% if production? %>
2
- <%= param('test.value') %>
3
- <% end %>
1
+ <%= include('spec/fixtures/src/kapacitor/include.erb') %>
@@ -0,0 +1,3 @@
1
+ <% if production? %>
2
+ <%= param('test.value', stage) %>
3
+ <% end %>
@@ -3,7 +3,7 @@ module Mocks
3
3
  class Test
4
4
  attr_accessor :result
5
5
 
6
- def print(component)
6
+ def write(component)
7
7
  @result = component.render
8
8
  end
9
9
  end
@@ -0,0 +1,15 @@
1
+ module Mocks
2
+ module Validators
3
+ class Test
4
+ attr_accessor :valid
5
+
6
+ def initialize
7
+ @valid = true
8
+ end
9
+
10
+ def valid?(_)
11
+ @valid
12
+ end
13
+ end
14
+ end
15
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative './mocks/outputs/test'
2
+ require_relative './mocks/validators/test'
2
3
 
3
4
  RSpec.configure do |spec|
4
5
  spec.before(:all) do
@@ -2,28 +2,46 @@ require 'config_templates'
2
2
  require 'spec_helper'
3
3
 
4
4
  RSpec.describe ::ConfigTemplates do
5
- before(:all) { @output_mock = ::Mocks::Outputs::Test.new }
6
5
  before(:all) do
7
- ::ConfigTemplates.configure { |config| config.outputs test: @output_mock }
6
+ @output = ::Mocks::Outputs::Test.new
7
+ @validator = ::Mocks::Validators::Test.new
8
8
  end
9
9
 
10
- before(:each) { @output_mock.result = nil }
10
+ before(:all) do
11
+ ::ConfigTemplates.configure do |config|
12
+ config.outputs test: @output
13
+ config.validators config: @validator
14
+ end
15
+ end
16
+
17
+ before(:each) do
18
+ @output.result = nil
19
+ @validator.valid = true
20
+ end
11
21
 
12
22
  it 'renders selected template using configs and environment metadata' do
13
23
  compilation = ::ConfigTemplates::Models::Compilation.new
14
- compilation.reject(/.*/).select(Regexp.new 'config.erb').send_to(:test)
15
- expect(@output_mock.result.strip).to eq('true')
24
+ compilation.reject(/.*/).select(Regexp.new 'config.erb')
25
+ compilation.send_to(:test)
26
+ expect(@output.result.strip).to eq('true')
16
27
  end
17
28
 
18
29
  it 'doesnt render not selected template' do
19
30
  compilation = ::ConfigTemplates::Models::Compilation.new
20
31
  compilation.reject(/.*/).send_to(:test)
21
- expect(@output_mock.result).to be_nil
32
+ expect(@output.result).to be_nil
22
33
  end
23
34
 
24
35
  it 'doesnt render anything without explicitly selected templates' do
25
36
  compilation = ::ConfigTemplates::Models::Compilation.new
26
37
  compilation.send_to(:test)
27
- expect(@output_mock.result).to be_nil
38
+ expect(@output.result).to be_nil
39
+ end
40
+
41
+ it 'fails if validation doesnt pass' do
42
+ @validator.valid = false
43
+ compilation = ::ConfigTemplates::Models::Compilation.new
44
+ compilation.select(Regexp.new 'config.erb')
45
+ expect { compilation.send_to(:test) }.to raise_error(::ConfigTemplates::Errors::InvalidTemplate, /config/)
28
46
  end
29
47
  end
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.1.1
4
+ version: 1.1.2
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-04-02 00:00:00.000000000 Z
11
+ date: 2018-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: deep_merge
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'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: dry-auto_inject
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -103,11 +117,22 @@ extra_rdoc_files: []
103
117
  files:
104
118
  - lib/autoload.rb
105
119
  - lib/config_templates.rb
120
+ - lib/config_templates/collections/components.rb
121
+ - lib/config_templates/collections/engines.rb
122
+ - lib/config_templates/collections/extensions.rb
123
+ - lib/config_templates/collections/outputs.rb
124
+ - lib/config_templates/collections/settings.rb
125
+ - lib/config_templates/collections/templates.rb
126
+ - lib/config_templates/collections/validators.rb
106
127
  - lib/config_templates/config.rb
107
128
  - lib/config_templates/contexts/compilation.rb
108
129
  - lib/config_templates/contexts/rendering.rb
109
130
  - lib/config_templates/criteria/composite.rb
110
131
  - lib/config_templates/criteria/name.rb
132
+ - lib/config_templates/criteria/path.rb
133
+ - lib/config_templates/directives/include.rb
134
+ - lib/config_templates/directives/invocation.rb
135
+ - lib/config_templates/directives/stage.rb
111
136
  - lib/config_templates/engines/erb.rb
112
137
  - lib/config_templates/engines/text.rb
113
138
  - lib/config_templates/errors.rb
@@ -117,18 +142,15 @@ files:
117
142
  - lib/config_templates/models/template.rb
118
143
  - lib/config_templates/outputs/filesystem.rb
119
144
  - lib/config_templates/outputs/stdout.rb
120
- - lib/config_templates/repositories/engines.rb
121
- - lib/config_templates/repositories/outputs.rb
122
- - lib/config_templates/repositories/settings.rb
123
- - lib/config_templates/repositories/templates.rb
124
- - lib/config_templates/repositories/validators.rb
125
145
  - lib/config_templates/validators/composite.rb
126
146
  - lib/config_templates/version.rb
127
147
  - lib/ioc_config.rb
128
148
  - spec/fixtures/settings/settings.production.yml
129
149
  - spec/fixtures/settings/settings.yml
130
150
  - spec/fixtures/src/kapacitor/config.erb
151
+ - spec/fixtures/src/kapacitor/include.erb
131
152
  - spec/mocks/outputs/test.rb
153
+ - spec/mocks/validators/test.rb
132
154
  - spec/spec_helper.rb
133
155
  - spec/templates_spec.rb
134
156
  homepage:
@@ -156,8 +178,10 @@ specification_version: 4
156
178
  summary: config_templates
157
179
  test_files:
158
180
  - spec/fixtures/src/kapacitor/config.erb
181
+ - spec/fixtures/src/kapacitor/include.erb
159
182
  - spec/fixtures/settings/settings.yml
160
183
  - spec/fixtures/settings/settings.production.yml
161
184
  - spec/mocks/outputs/test.rb
185
+ - spec/mocks/validators/test.rb
162
186
  - spec/templates_spec.rb
163
187
  - spec/spec_helper.rb
@@ -1,15 +0,0 @@
1
- module ConfigTemplates::Repositories
2
- class Settings
3
- include ::ConfigTemplates::Inject['files.locator']
4
-
5
- def initialize(locator)
6
- @locator = locator
7
- end
8
-
9
- def find_all
10
- @find_all ||= @locator.settings.reduce({}) {
11
- |result, current| result.merge ::YAML.load_file current, Hash.new
12
- }
13
- end
14
- end
15
- end
@@ -1,16 +0,0 @@
1
- module ConfigTemplates::Repositories
2
- class Templates
3
- include ::ConfigTemplates::Inject['config', 'files.locator']
4
-
5
- def initialize(config, locator)
6
- @config = config
7
- @locator = locator
8
- end
9
-
10
- def find_all_by(criteria)
11
- @locator.templates
12
- .select { |filename| criteria.matches? filename }
13
- .map { |filename| ::ConfigTemplates::Models::Template.new filename, @config }
14
- end
15
- end
16
- end
@@ -1,21 +0,0 @@
1
- module ConfigTemplates::Repositories
2
- class Validators
3
- def initialize
4
- @validators = {}
5
- end
6
-
7
- def add(validators)
8
- @validators.merge! validators
9
- end
10
-
11
- def find_by_file_name(file_name)
12
- ::ConfigTemplates::Validators::Composite.new(
13
- find_all_by ::ConfigTemplates::Criteria::Name.new file_name
14
- )
15
- end
16
-
17
- def find_all_by(criteria)
18
- @validators.select { |(k, _)| criteria.matches?(k) }.values
19
- end
20
- end
21
- end