rdm 0.4.17 → 0.4.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -8
  3. data/bin/rdm +10 -10
  4. data/example/.rdm/templates/configs/<%=config_path%> +2 -0
  5. data/example/.rdm/templates/configs/<%=role_config_path%> +2 -0
  6. data/example/Rdm.packages +17 -1
  7. data/example/configs/app/default.yml +2 -0
  8. data/example/configs/app/production.yml +2 -0
  9. data/example/configs/database/default.yml +3 -0
  10. data/example/env_files/development.env +3 -0
  11. data/example/env_files/production.env +5 -0
  12. data/example/env_files/test.env +3 -0
  13. data/example/infrastructure/repository/Package.rb +1 -0
  14. data/lib/rdm.rb +7 -14
  15. data/lib/rdm/cli/config.rb +31 -0
  16. data/lib/rdm/config.rb +11 -0
  17. data/lib/rdm/config_locals.rb +11 -0
  18. data/lib/rdm/config_manager.rb +68 -0
  19. data/lib/rdm/config_scope.rb +23 -0
  20. data/lib/rdm/errors.rb +0 -3
  21. data/lib/rdm/gen/config.rb +59 -0
  22. data/lib/rdm/gen/init.rb +4 -0
  23. data/lib/rdm/package.rb +9 -18
  24. data/lib/rdm/package_importer.rb +17 -3
  25. data/lib/rdm/package_parser.rb +0 -1
  26. data/lib/rdm/packages/compiler_service.rb +7 -1
  27. data/lib/rdm/settings.rb +21 -10
  28. data/lib/rdm/source.rb +18 -1
  29. data/lib/rdm/source_parser.rb +45 -2
  30. data/lib/rdm/spec_runner/runner.rb +2 -2
  31. data/lib/rdm/templates/configs/<%=config_path%> +2 -0
  32. data/lib/rdm/templates/configs/<%=role_config_path%> +2 -0
  33. data/lib/rdm/templates/init/Rdm.packages +12 -0
  34. data/lib/rdm/templates/init/env_files/development.env +3 -0
  35. data/lib/rdm/templates/init/env_files/production.env +3 -0
  36. data/lib/rdm/templates/init/env_files/test.env +3 -0
  37. data/lib/rdm/version.rb +1 -1
  38. data/rdm.gemspec +0 -3
  39. data/spec/fixtures/SampleSource.rb +4 -2
  40. data/spec/fixtures/config.yml +2 -0
  41. data/spec/rdm/cli/gen_package_spec.rb +2 -0
  42. data/spec/rdm/config_manager_spec.rb +136 -0
  43. data/spec/rdm/gen/config_spec.rb +31 -0
  44. data/spec/rdm/gen/init_spec.rb +12 -0
  45. data/spec/rdm/gen/package_spec.rb +0 -1
  46. data/spec/rdm/package_importer_spec.rb +2 -34
  47. data/spec/rdm/rdm_spec.rb +1 -1
  48. data/spec/rdm/source_parser_spec.rb +59 -0
  49. data/spec/spec_helper.rb +0 -1
  50. metadata +27 -63
  51. data/example/config/app.yml +0 -6
  52. data/example/infrastructure/repository/fixture.txt +0 -1
  53. data/lib/rdm/utils/ostruct_utils.rb +0 -12
  54. data/lib/rdm/yml_config/config_caster.rb +0 -32
  55. data/lib/rdm/yml_config/config_manager.rb +0 -39
  56. data/lib/rdm/yml_config/config_validator.rb +0 -51
  57. data/lib/rdm/yml_config/env_config.rb +0 -46
  58. data/lib/rdm/yml_config/env_config_dsl.rb +0 -92
  59. data/lib/rdm/yml_config/validate_config.rb +0 -13
  60. data/spec/fixtures/app.yml +0 -17
  61. data/spec/rdm/yml_config/config_caster_spec.rb +0 -64
  62. data/spec/rdm/yml_config/config_manager_spec.rb +0 -7
  63. data/spec/rdm/yml_config/config_validator_spec.rb +0 -190
  64. data/spec/rdm/yml_config/env_config_dsl_spec.rb +0 -123
@@ -1,39 +0,0 @@
1
- require 'yaml'
2
-
3
- class Rdm::ConfigManager
4
- class << self
5
- def load_config(envs:, path_to_config:)
6
- new_config = Rdm::ConfigCaster.new(envs).cast(YAML.load(File.read(path_to_config)))
7
- validate_params!(new_config, envs)
8
-
9
- instance.config.merge! new_config
10
- end
11
-
12
- def reset!
13
- instance.config.clear
14
- end
15
-
16
- def method_missing(meth, *args, &block)
17
- instance.send(meth)
18
- end
19
-
20
- def instance
21
- @instance ||= new
22
- end
23
-
24
- private
25
-
26
- def validate_params!(config, envs)
27
- Rdm::ConfigValidator.new(envs).validate!(config)
28
- end
29
- end
30
-
31
- def method_missing(meth)
32
- config.keys.include?(meth) ? Rdm::Utils::Ostruct.to_recursive_ostruct(config).send(meth) :
33
- (raise ArgumentError, ":#{meth} configuration was not defined for current package. Add `import '#{meth}'` to your Package.rb file")
34
- end
35
-
36
- def config
37
- @config ||= {}
38
- end
39
- end
@@ -1,51 +0,0 @@
1
- require 'attr_validator'
2
-
3
- class Rdm::ConfigValidator
4
- def initialize(env_config)
5
- @env_config = env_config
6
- end
7
-
8
- def validate!(hash_config)
9
- dto = OpenStruct.new(hash_config)
10
-
11
- if @env_config.is_hash?
12
- @env_config.children.each do |subconfig|
13
- self.class.new(subconfig).validate!(dto.send(@env_config.name).to_h)
14
- end
15
- elsif @env_config.is_array?
16
- dto.send(@env_config.name).each do |el|
17
- array_dto = OpenStruct.new(@env_config.name => el)
18
- validator(@env_config.name, @env_config.children.first.validates.to_hash).validate!(array_dto)
19
- end
20
- else
21
- validator(@env_config.name, @env_config.validates.to_hash).validate!(dto)
22
- end
23
-
24
- dto
25
- rescue AttrValidator::Errors::ValidationError => e
26
- raise Rdm::Errors::InvalidConfig, e.message
27
- end
28
-
29
- private
30
-
31
- def validator(name, env_config)
32
- return @validator if @validator
33
-
34
- validator_class = Class.new
35
- validator_class.include(AttrValidator::Validator)
36
-
37
- validator_class.class_eval( to_attr_validator_string(name, env_config) )
38
-
39
- @validator = validator_class.new
40
- end
41
-
42
- def to_attr_validator_string(name, validates)
43
- validates_string = []
44
-
45
- validates.each do |key, value|
46
- validates_string.push "validates :#{name}, #{key}: #{value}"
47
- end
48
-
49
- validates_string.join("\n")
50
- end
51
- end
@@ -1,46 +0,0 @@
1
- class Rdm::EnvConfig
2
- module Types
3
- STRING = :string
4
- ARRAY = :array
5
- HASH = :hash
6
- SYMBOL = :symbol
7
- INTEGER = :integer
8
-
9
- ALL = [STRING, ARRAY, HASH, SYMBOL, INTEGER]
10
- end
11
-
12
- attr_reader :name, :type, :optional, :default, :validates
13
- attr_accessor :children
14
-
15
- def initialize(name:, type:, optional: false, default: nil, validates: nil, children: nil)
16
- @name = name
17
- @type = Types::ALL.include?(type) ? type: (raise ArgumentError, "Invalid env type")
18
- @optional = !!optional
19
- @validates = validates || Rdm::ValidateConfig.new
20
- @children = (children || []).select {|e| e.is_a?(Rdm::EnvConfig)}
21
- @default = default
22
- end
23
-
24
- def to_hash
25
- hash = {
26
- name: @name,
27
- type: @type,
28
- optional: @optional,
29
- default: @default,
30
- }.delete_if { |_, v| v.nil? }
31
-
32
- hash[:children] = @children.map(&:to_hash) if @children.any?
33
- hash[:validates] = @validates.to_hash if @validates.to_hash.any?
34
-
35
- hash
36
- end
37
-
38
- def is_array?
39
- @type == Types::ARRAY
40
- end
41
-
42
- def is_hash?
43
- @type == Types::HASH
44
- end
45
- end
46
-
@@ -1,92 +0,0 @@
1
- class Rdm::EnvConfigDSL
2
- attr_reader :data
3
-
4
- module Types
5
- STRING = :string
6
- ARRAY = :array
7
- HASH = :hash
8
- SYMBOL = :symbol
9
- INTEGER = :integer
10
-
11
- ALL = [STRING, ARRAY, HASH, SYMBOL, INTEGER]
12
- end
13
-
14
- def initialize
15
- @data = []
16
- end
17
-
18
- def string(name, opts = {}, &block)
19
- validations = Rdm::ValidateConfig.new
20
- validations.instance_exec(&block) if block_given?
21
-
22
- @data.push(
23
- Rdm::EnvConfig.new(
24
- name: name,
25
- type: Types::STRING,
26
- optional: opts[:optional],
27
- default: opts[:default],
28
- validates: validations
29
- )
30
- )
31
- end
32
-
33
- def symbol(name, opts = {}, &block)
34
- validations = Rdm::ValidateConfig.new
35
- validations.instance_exec(&block) if block_given?
36
-
37
- @data.push(
38
- Rdm::EnvConfig.new(
39
- name: name,
40
- type: Types::SYMBOL,
41
- optional: opts[:optional],
42
- default: opts[:default],
43
- validates: validations
44
- )
45
- )
46
- end
47
-
48
- def integer(name, opts = {}, &block)
49
- validations = Rdm::ValidateConfig.new
50
- validations.instance_exec(&block) if block_given?
51
-
52
- @data.push(
53
- Rdm::EnvConfig.new(
54
- name: name,
55
- type: Types::INTEGER,
56
- optional: opts[:optional],
57
- default: opts[:default],
58
- validates: validations
59
- )
60
- )
61
- end
62
-
63
- def array(name, opts = {}, &block)
64
- array_values = self.class.new
65
- array_values.send(opts.fetch(:each), nil, {}, &block)
66
-
67
- @data.push(
68
- Rdm::EnvConfig.new(
69
- name: name,
70
- type: Types::ARRAY,
71
- optional: opts[:optional],
72
- default: opts[:default],
73
- children: array_values.data
74
- )
75
- )
76
- end
77
-
78
- def hash(name, opts = {}, &block)
79
- hash_values = self.class.new
80
- hash_values.instance_exec(&block)
81
-
82
- @data.push(
83
- Rdm::EnvConfig.new(
84
- name: name,
85
- type: Types::HASH,
86
- optional: opts[:optional],
87
- default: opts[:default],
88
- children: hash_values.data
89
- )
90
- )
91
- end
92
- end
@@ -1,13 +0,0 @@
1
- class Rdm::ValidateConfig
2
- def initialize
3
- @data = {}
4
- end
5
-
6
- def method_missing(method, *args, &block)
7
- @data[method] = args.first
8
- end
9
-
10
- def to_hash
11
- @data
12
- end
13
- end
@@ -1,17 +0,0 @@
1
- core:
2
- some_key:
3
- - value1
4
- - value2
5
- - value3
6
-
7
- web:
8
- some_key:
9
- - value1
10
- - value2
11
- - value3
12
-
13
- factory:
14
- some_key:
15
- - value1
16
- - value2
17
- - value3
@@ -1,64 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Rdm::ConfigCaster do
4
- subject { described_class.new(env) }
5
-
6
- describe "#to_hcast_string" do
7
- context 'for string env' do
8
- let(:env) {
9
- Rdm::EnvConfig.new(
10
- name: :string_env,
11
- type: :string
12
- )
13
- }
14
-
15
- it 'generates proper string' do
16
- expect(
17
- subject.to_hcast_string(env)
18
- ).to eq("string :string_env, optional: false")
19
- end
20
- end
21
-
22
- context 'for array env' do
23
- let(:env) {
24
- Rdm::EnvConfig.new(
25
- name: :array_env,
26
- type: :array,
27
- children: [
28
- Rdm::EnvConfig.new(
29
- name: :string_env,
30
- type: :string
31
- )
32
- ]
33
- )
34
- }
35
-
36
- it 'generates proper string' do
37
- expect(
38
- subject.to_hcast_string(env)
39
- ).to eq("array :array_env, optional: false, each: :string")
40
- end
41
- end
42
-
43
- context 'for hash env' do
44
- let(:env) {
45
- Rdm::EnvConfig.new(
46
- name: :hash_env,
47
- type: :hash,
48
- children: [
49
- Rdm::EnvConfig.new(
50
- name: :string_env,
51
- type: :string
52
- )
53
- ]
54
- )
55
- }
56
-
57
- it 'generates proper string' do
58
- expect(
59
- subject.to_hcast_string(env)
60
- ).to eq("hash :hash_env, optional: false do \n string :string_env, optional: false \nend")
61
- end
62
- end
63
- end
64
- end
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Rdm::ConfigManager do
4
- describe '#load_config' do
5
-
6
- end
7
- end
@@ -1,190 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Rdm::ConfigValidator do
4
-
5
- let(:hash_config) {
6
- {
7
- example_config: {
8
- example_array_config: ['hello', 'world'],
9
- example_symbol_config: :values,
10
- example_integer_config: 25,
11
- example_email_config: 'info@rdm.com'
12
- }
13
- }
14
- }
15
-
16
- let(:array_config) {
17
- Rdm::EnvConfig.new(
18
- name: 'example_array_config',
19
- type: :array,
20
- validates: nil,
21
- children: [
22
- Rdm::EnvConfig.new(
23
- name: nil,
24
- type: :string,
25
- validates: {
26
- length: {
27
- equal_to: 5
28
- }
29
- }
30
- )
31
- ]
32
- )
33
- }
34
- let(:symbol_config) {
35
- Rdm::EnvConfig.new(
36
- name: 'example_symbol_config',
37
- type: :symbol,
38
- validates: {
39
- inclusion: {
40
- in: [:example, :symbol, :config, :values]
41
- }
42
- }
43
- )
44
- }
45
- let(:integer_config) {
46
- Rdm::EnvConfig.new(
47
- name: 'example_integer_config',
48
- type: :integer,
49
- validates: {
50
- numericality: {
51
- greater_than_or_equal_to: 5,
52
- less_than: 50
53
- }
54
- }
55
- )
56
- }
57
- let(:email_config) {
58
- Rdm::EnvConfig.new(
59
- name: 'example_email_config',
60
- type: :string,
61
- optional: true,
62
- validates: {
63
- email: true,
64
- presence: true
65
- }
66
- )
67
- }
68
-
69
- let(:env_config) {
70
- Rdm::EnvConfig.new(
71
- name: 'example_config',
72
- type: :hash,
73
- optional: true,
74
- default: nil,
75
- validates: nil,
76
- children: [
77
- array_config,
78
- symbol_config,
79
- integer_config,
80
- email_config
81
- ]
82
- )
83
- }
84
-
85
- describe '#validate' do
86
- context 'for symbol config' do
87
- it 'not raises error for valid params' do
88
- expect {
89
- described_class.new(symbol_config).validate!({example_symbol_config: :example})
90
- }.not_to raise_error
91
- end
92
-
93
- it 'raises ArgumentError if invalid params' do
94
- expect {
95
- described_class.new(symbol_config).validate!({example_symbol_config: :unpermitted_parameter})
96
- }.to raise_error Rdm::Errors::InvalidConfig
97
- end
98
-
99
- it 'returns OpenStruct' do
100
- expect(
101
- described_class.new(symbol_config).validate!({example_symbol_config: :example})
102
- ).to eq(OpenStruct.new({example_symbol_config: :example}))
103
- end
104
- end
105
-
106
- context 'for email config' do
107
- it 'not raises error for valid params' do
108
- expect {
109
- described_class.new(email_config).validate!({example_email_config: 'hello@world.ru'})
110
- }.not_to raise_error
111
- end
112
-
113
- it 'raises ArgumentError if invalid params' do
114
- expect {
115
- described_class.new(email_config).validate!({example_email_config: 'helloworld'})
116
- }.to raise_error Rdm::Errors::InvalidConfig
117
- end
118
-
119
- it 'returns OpenStruct' do
120
- expect(
121
- described_class.new(email_config).validate!({example_email_config: 'hello@world.ru'})
122
- ).to eq(OpenStruct.new({example_email_config: 'hello@world.ru'}))
123
- end
124
- end
125
-
126
- context 'for integer config' do
127
- it 'not raises error for valid params' do
128
- expect {
129
- described_class.new(integer_config).validate!({example_integer_config: 5})
130
- }.not_to raise_error
131
- end
132
-
133
- it 'not raises error for valid params' do
134
- expect {
135
- described_class.new(integer_config).validate!({example_integer_config: 4})
136
- }.to raise_error Rdm::Errors::InvalidConfig
137
- end
138
-
139
- it 'returns OpenStruct' do
140
- expect(
141
- described_class.new(integer_config).validate!({example_integer_config: 5})
142
- ).to eq(OpenStruct.new({example_integer_config: 5}))
143
- end
144
- end
145
-
146
- context 'for array config' do
147
- it 'not raises error for valid params' do
148
- expect {
149
- described_class.new(symbol_config).validate!({example_array_config: ['hello', 'world']})
150
- }.not_to raise_error
151
- end
152
-
153
- it 'returns OpenStruct' do
154
- expect(
155
- described_class.new(integer_config).validate!({example_array_config: ['hello', 'world']})
156
- ).to eq(OpenStruct.new({example_array_config: ['hello', 'world']}))
157
- end
158
- end
159
-
160
- context 'for hash config' do
161
- it 'not raises error for valid params' do
162
- expect {
163
- described_class.new(symbol_config).validate!({example_array_config: ['hello', 'world']})
164
- }.not_to raise_error
165
- end
166
-
167
- it 'returns OpenStruct' do
168
- expect(
169
- described_class.new(integer_config).validate!(
170
- {
171
- example_array_config: ['hello', 'world'],
172
- example_integer_config: 5,
173
- example_email_config: 'hello@world.pro',
174
- example_symbol_config: :example
175
- }
176
- )
177
- ).to match(
178
- OpenStruct.new(
179
- {
180
- example_array_config: ['hello', 'world'],
181
- example_integer_config: 5,
182
- example_email_config: 'hello@world.pro',
183
- example_symbol_config: :example
184
- }
185
- )
186
- )
187
- end
188
- end
189
- end
190
- end