qonfig 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -1
  4. data/.rubocop.yml +1 -1
  5. data/.travis.yml +3 -3
  6. data/CHANGELOG.md +10 -0
  7. data/README.md +146 -1
  8. data/Rakefile +2 -0
  9. data/lib/qonfig/command_set.rb +53 -55
  10. data/lib/qonfig/commands/add_nested_option.rb +36 -40
  11. data/lib/qonfig/commands/add_option.rb +33 -37
  12. data/lib/qonfig/commands/base.rb +9 -13
  13. data/lib/qonfig/commands/compose.rb +29 -33
  14. data/lib/qonfig/commands/expose_yaml.rb +154 -158
  15. data/lib/qonfig/commands/load_from_env.rb +77 -79
  16. data/lib/qonfig/commands/load_from_json.rb +52 -56
  17. data/lib/qonfig/commands/load_from_self.rb +57 -61
  18. data/lib/qonfig/commands/load_from_yaml.rb +54 -58
  19. data/lib/qonfig/commands.rb +15 -0
  20. data/lib/qonfig/configurable.rb +88 -90
  21. data/lib/qonfig/data_set/class_builder.rb +17 -21
  22. data/lib/qonfig/data_set.rb +186 -138
  23. data/lib/qonfig/dsl.rb +106 -108
  24. data/lib/qonfig/{error.rb → exceptions.rb} +13 -1
  25. data/lib/qonfig/loaders/basic.rb +30 -32
  26. data/lib/qonfig/loaders/json.rb +16 -23
  27. data/lib/qonfig/loaders/yaml.rb +16 -23
  28. data/lib/qonfig/loaders.rb +9 -0
  29. data/lib/qonfig/plugins/abstract.rb +7 -11
  30. data/lib/qonfig/plugins/access_mixin.rb +21 -25
  31. data/lib/qonfig/plugins/registry.rb +120 -124
  32. data/lib/qonfig/plugins.rb +56 -54
  33. data/lib/qonfig/settings/builder.rb +10 -14
  34. data/lib/qonfig/settings/key_guard.rb +60 -64
  35. data/lib/qonfig/settings/lock.rb +53 -57
  36. data/lib/qonfig/settings.rb +392 -354
  37. data/lib/qonfig/uploaders/base.rb +18 -0
  38. data/lib/qonfig/uploaders/file.rb +55 -0
  39. data/lib/qonfig/uploaders/json.rb +35 -0
  40. data/lib/qonfig/uploaders/yaml.rb +93 -0
  41. data/lib/qonfig/uploaders.rb +10 -0
  42. data/lib/qonfig/version.rb +1 -1
  43. data/lib/qonfig.rb +4 -21
  44. data/qonfig.gemspec +1 -1
  45. metadata +13 -6
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.11.0
5
+ class Qonfig::Uploaders::Base
6
+ class << self
7
+ # @param settings [Qonfig::Settings]
8
+ # @param options [Hash<Symbol,Any>]
9
+ # @param value_procssor [Block]
10
+ # @return [void]
11
+ #
12
+ # @api private
13
+ # @since 0.11.0
14
+ def upload(settings, options: {}, &value_processor)
15
+ nil # NOTE: consciously return nil (for clarity)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.11.0
5
+ class Qonfig::Uploaders::File < Qonfig::Uploaders::Base
6
+ # @return [String]
7
+ #
8
+ # @api private
9
+ # @since 0.11.0
10
+ FILE_OPENING_MODE = 'w'
11
+
12
+ # @return [String]
13
+ #
14
+ # @api private
15
+ # @since 0.11.0
16
+ EMPTY_SETTINGS_REPRESENTATION = ''
17
+
18
+ # @return [Hash]
19
+ #
20
+ # @api private
21
+ # @since 0.11.0
22
+ DEFAULT_OPTIONS = {}.freeze
23
+
24
+ class << self
25
+ # @param settings [Qonfig::Settings]
26
+ # @param options [Hash<Symbol|String,Any>]
27
+ # @param value_processor [Block]
28
+ # @option path [String]
29
+ # @return [void]
30
+ #
31
+ # @api private
32
+ # @since 0.11.0
33
+ def upload(settings, path:, options: self::DEFAULT_OPTIONS, &value_processor)
34
+ ::File.open(path, FILE_OPENING_MODE) do |file_descriptor|
35
+ settings_representation = represent_settings(settings, options, &value_processor)
36
+ file_descriptor.write(settings_representation)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ # @param settings [Qonfig::Settings]
43
+ # @param options [Hash<Symbol|String,Any>]
44
+ # @param value_processor [Block]
45
+ # @return [String]
46
+ #
47
+ # @api private
48
+ # @since 0.11.0
49
+ def represent_settings(settings, options, &value_processor)
50
+ # :nocov:
51
+ EMPTY_SETTINGS_REPRESENTATION
52
+ # :nocov:
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.11.0
5
+ class Qonfig::Uploaders::JSON < Qonfig::Uploaders::File
6
+ # @return [Hash<Symbol,Any>]
7
+ #
8
+ # @api private
9
+ # @since 0.11.0
10
+ DEFAULT_OPTIONS = {
11
+ indent: ' ',
12
+ space: ' ',
13
+ object_nl: "\n"
14
+ }.freeze
15
+
16
+ class << self
17
+ # @param settings [Qonfig::Settings]
18
+ # @param options [Hash<Symbol,Any>]
19
+ # @param value_processor [Block]
20
+ # @return [String]
21
+ #
22
+ # @api private
23
+ # @since 0.11.0
24
+ def represent_settings(settings, options, &value_processor)
25
+ settings_hash =
26
+ if block_given?
27
+ settings.__to_hash__(transform_value: value_processor)
28
+ else
29
+ settings.__to_hash__
30
+ end
31
+
32
+ ::JSON.generate(settings_hash, options)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.11.0
5
+ class Qonfig::Uploaders::YAML < Qonfig::Uploaders::File
6
+ # @api private
7
+ # @since 0.11.0
8
+ class YAMLRepresenter < Psych::Visitors::YAMLTree
9
+ # Needed for using the '~' symbol used for null value representation in YAML files
10
+ # (instead of space symbol (' ')) (see psych/lib/psych/visitors/yaml_tree.rb)
11
+ #
12
+ # @param object [Any]
13
+ # @return [Any]
14
+ #
15
+ # @api private
16
+ # @since 0.11.0
17
+ def visit_NilClass(object) # rubocop:disable Naming/MethodName
18
+ @emitter.scalar('~', nil, 'tag:yaml.org,2002:null', true, false, Psych::Nodes::Scalar::ANY)
19
+ end
20
+ end
21
+
22
+ # @return [Hash<Symbol,Any>]
23
+ #
24
+ # @api private
25
+ # @since 0.11.0
26
+ DEFAULT_OPTIONS = {
27
+ indentation: 2,
28
+ line_width: -1,
29
+ canonical: false,
30
+ header: false,
31
+ symbolize_keys: false
32
+ }.freeze
33
+
34
+ # @return [Proc]
35
+ #
36
+ # @api private
37
+ # @since 0.11.0
38
+ KEY_SYMBOLIZER = -> (value) { value.to_sym }.freeze
39
+
40
+ class << self
41
+ # @param settings [Qonfig::Settings]
42
+ # @param options [Hash<Symbol,Any>]
43
+ # @param value_processor [Block]
44
+ # @return [String]
45
+ #
46
+ # @api private
47
+ # @since 0.11.0
48
+ def represent_settings(settings, options, &value_processor)
49
+ settings_hash_opts = hash_representation_options(options, &value_processor)
50
+ settings_hash = settings.__to_hash__(settings_hash_opts)
51
+ to_yaml_string(settings_hash, options)
52
+ end
53
+
54
+ # @param options [Hash<Symbol|String,Any>]
55
+ # @param value_processor [Block]
56
+ # @return [Hash]
57
+ #
58
+ # @api private
59
+ # @since 0.11.0
60
+ def hash_representation_options(options, &value_processor)
61
+ {}.tap do |representation_opts|
62
+ # NOTE: this case/when with the same logic is only used for better code readbility
63
+ case
64
+ # NOTE: options has :symbolize_keys key
65
+ when options.key?(:symbolize_keys) && !!options[:symbolize_keys]
66
+ representation_opts[:transform_key] = KEY_SYMBOLIZER
67
+ # NOTE: options does not have :symbolize_keys key
68
+ when !options.key?(:symbolize_keys) && DEFAULT_OPTIONS[:symbolize_keys]
69
+ # :nocov:
70
+ representation_opts[:transform_key] = KEY_SYMBOLIZER
71
+ # :nocov:
72
+ end
73
+
74
+ # NOTE: provide value transformer
75
+ if block_given?
76
+ representation_opts[:transform_value] = value_processor
77
+ end
78
+ end
79
+ end
80
+
81
+ # @param settings_hash [Hash<String|Symbol,Any>]
82
+ # @param yaml_options [Hash<Symbol,Any>]
83
+ # @return [String]
84
+ #
85
+ # @api private
86
+ # @since 0.11.0
87
+ def to_yaml_string(settings_hash, yaml_options)
88
+ representer = YAMLRepresenter.create(yaml_options)
89
+ representer << settings_hash
90
+ representer.tree.yaml(nil, yaml_options)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.11.0
5
+ module Qonfig::Uploaders
6
+ require_relative 'uploaders/base'
7
+ require_relative 'uploaders/file'
8
+ require_relative 'uploaders/json'
9
+ require_relative 'uploaders/yaml'
10
+ end
@@ -5,5 +5,5 @@ module Qonfig
5
5
  #
6
6
  # @api public
7
7
  # @since 0.1.0
8
- VERSION = '0.10.0'
8
+ VERSION = '0.11.0'
9
9
  end
data/lib/qonfig.rb CHANGED
@@ -5,33 +5,16 @@ require 'json'
5
5
  require 'erb'
6
6
 
7
7
  module Qonfig
8
- require_relative 'qonfig/error'
9
- require_relative 'qonfig/loaders/basic'
10
- require_relative 'qonfig/loaders/json'
11
- require_relative 'qonfig/loaders/yaml'
12
- require_relative 'qonfig/commands/base'
13
- require_relative 'qonfig/commands/add_option'
14
- require_relative 'qonfig/commands/add_nested_option'
15
- require_relative 'qonfig/commands/compose'
16
- require_relative 'qonfig/commands/load_from_yaml'
17
- require_relative 'qonfig/commands/load_from_json'
18
- require_relative 'qonfig/commands/load_from_self'
19
- require_relative 'qonfig/commands/load_from_env'
20
- require_relative 'qonfig/commands/load_from_env/value_converter'
21
- require_relative 'qonfig/commands/expose_yaml'
8
+ require_relative 'qonfig/exceptions'
9
+ require_relative 'qonfig/loaders'
10
+ require_relative 'qonfig/uploaders'
11
+ require_relative 'qonfig/commands'
22
12
  require_relative 'qonfig/command_set'
23
13
  require_relative 'qonfig/settings'
24
- require_relative 'qonfig/settings/lock'
25
- require_relative 'qonfig/settings/builder'
26
- require_relative 'qonfig/settings/key_guard'
27
14
  require_relative 'qonfig/dsl'
28
15
  require_relative 'qonfig/data_set'
29
- require_relative 'qonfig/data_set/class_builder'
30
16
  require_relative 'qonfig/configurable'
31
- require_relative 'qonfig/plugins/registry'
32
17
  require_relative 'qonfig/plugins'
33
- require_relative 'qonfig/plugins/access_mixin'
34
- require_relative 'qonfig/plugins/abstract'
35
18
 
36
19
  # @api public
37
20
  # @since 0.4.0
data/qonfig.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency 'coveralls', '~> 0.8'
32
32
  spec.add_development_dependency 'simplecov', '~> 0.16'
33
33
  spec.add_development_dependency 'rspec', '~> 3.8'
34
- spec.add_development_dependency 'armitage-rubocop', '~> 0.21'
34
+ spec.add_development_dependency 'armitage-rubocop', '~> 0.30'
35
35
 
36
36
  spec.add_development_dependency 'bundler'
37
37
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qonfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rustam Ibragimov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-26 00:00:00.000000000 Z
11
+ date: 2019-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coveralls
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.21'
61
+ version: '0.30'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.21'
68
+ version: '0.30'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -132,6 +132,7 @@ files:
132
132
  - bin/setup
133
133
  - lib/qonfig.rb
134
134
  - lib/qonfig/command_set.rb
135
+ - lib/qonfig/commands.rb
135
136
  - lib/qonfig/commands/add_nested_option.rb
136
137
  - lib/qonfig/commands/add_option.rb
137
138
  - lib/qonfig/commands/base.rb
@@ -147,7 +148,8 @@ files:
147
148
  - lib/qonfig/data_set/class_builder.rb
148
149
  - lib/qonfig/data_set/validator.rb
149
150
  - lib/qonfig/dsl.rb
150
- - lib/qonfig/error.rb
151
+ - lib/qonfig/exceptions.rb
152
+ - lib/qonfig/loaders.rb
151
153
  - lib/qonfig/loaders/basic.rb
152
154
  - lib/qonfig/loaders/json.rb
153
155
  - lib/qonfig/loaders/yaml.rb
@@ -159,6 +161,11 @@ files:
159
161
  - lib/qonfig/settings/builder.rb
160
162
  - lib/qonfig/settings/key_guard.rb
161
163
  - lib/qonfig/settings/lock.rb
164
+ - lib/qonfig/uploaders.rb
165
+ - lib/qonfig/uploaders/base.rb
166
+ - lib/qonfig/uploaders/file.rb
167
+ - lib/qonfig/uploaders/json.rb
168
+ - lib/qonfig/uploaders/yaml.rb
162
169
  - lib/qonfig/version.rb
163
170
  - qonfig.gemspec
164
171
  homepage: https://github.com/0exp/qonfig
@@ -180,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
187
  - !ruby/object:Gem::Version
181
188
  version: '0'
182
189
  requirements: []
183
- rubygems_version: 3.0.2
190
+ rubygems_version: 3.0.3
184
191
  signing_key:
185
192
  specification_version: 4
186
193
  summary: Config object