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
@@ -1,31 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qonfig
4
- module Loaders
3
+ # @api private
4
+ # @since 0.5.0
5
+ class Qonfig::Loaders::JSON < Qonfig::Loaders::Basic
6
+ class << self
7
+ # @param data [String]
8
+ # @return [Object]
9
+ #
5
10
  # @api private
6
11
  # @since 0.5.0
7
- module JSON
8
- # @since 0.5.0
9
- extend Qonfig::Loaders::Basic
10
-
11
- class << self
12
- # @param data [String]
13
- # @return [Object]
14
- #
15
- # @api private
16
- # @since 0.5.0
17
- def load(data)
18
- ::JSON.parse(data, max_nesting: false, allow_nan: true)
19
- end
12
+ def load(data)
13
+ ::JSON.parse(data, max_nesting: false, allow_nan: true)
14
+ end
20
15
 
21
- # @return [Object]
22
- #
23
- # @api private
24
- # @since 0.5.0
25
- def load_empty_data
26
- load('{}')
27
- end
28
- end
16
+ # @return [Object]
17
+ #
18
+ # @api private
19
+ # @since 0.5.0
20
+ def load_empty_data
21
+ load('{}')
29
22
  end
30
23
  end
31
24
  end
@@ -1,31 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qonfig
4
- module Loaders
3
+ # @api private
4
+ # @since 0.2.0
5
+ class Qonfig::Loaders::YAML < Qonfig::Loaders::Basic
6
+ class << self
7
+ # @param data [String]
8
+ # @return [Object]
9
+ #
5
10
  # @api private
6
11
  # @since 0.2.0
7
- module YAML
8
- # @since 0.5.0
9
- extend Qonfig::Loaders::Basic
10
-
11
- class << self
12
- # @param data [String]
13
- # @return [Object]
14
- #
15
- # @api private
16
- # @since 0.2.0
17
- def load(data)
18
- ::YAML.load(ERB.new(data).result)
19
- end
12
+ def load(data)
13
+ ::YAML.load(ERB.new(data).result)
14
+ end
20
15
 
21
- # @return [Object]
22
- #
23
- # @api private
24
- # @since 0.5.0
25
- def load_empty_data
26
- load('{}')
27
- end
28
- end
16
+ # @return [Object]
17
+ #
18
+ # @api private
19
+ # @since 0.5.0
20
+ def load_empty_data
21
+ load('{}')
29
22
  end
30
23
  end
31
24
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api private
4
+ # @since 0.2.0
5
+ module Qonfig::Loaders
6
+ require_relative 'loaders/basic'
7
+ require_relative 'loaders/json'
8
+ require_relative 'loaders/yaml'
9
+ end
@@ -1,17 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qonfig
4
- module Plugins
3
+ # @api private
4
+ # @since 0.4.0
5
+ class Qonfig::Plugins::Abstract
6
+ class << self
7
+ # @return [void]
8
+ #
5
9
  # @api private
6
10
  # @since 0.4.0
7
- class Abstract
8
- class << self
9
- # @return [void]
10
- #
11
- # @api private
12
- # @since 0.4.0
13
- def load!; end
14
- end
15
- end
11
+ def load!; end
16
12
  end
17
13
  end
@@ -1,30 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qonfig
4
- module Plugins
5
- # @api private
6
- # @since 0.4.0
7
- module AccessMixin
8
- # @param plugin_name [Symbol, String]
9
- # @return [void]
10
- #
11
- # @see Qonfig::Plugins
12
- #
13
- # @api public
14
- # @since 0.4.0
15
- def plugin(plugin_name)
16
- Qonfig::Plugins.load(plugin_name)
17
- end
3
+ # @api private
4
+ # @since 0.4.0
5
+ module Qonfig::Plugins::AccessMixin
6
+ # @param plugin_name [Symbol, String]
7
+ # @return [void]
8
+ #
9
+ # @see Qonfig::Plugins
10
+ #
11
+ # @api public
12
+ # @since 0.4.0
13
+ def plugin(plugin_name)
14
+ Qonfig::Plugins.load(plugin_name)
15
+ end
18
16
 
19
- # @return [Array<String>]
20
- #
21
- # @see Qonfig::Plugins
22
- #
23
- # @api public
24
- # @since 0.4.0
25
- def plugins
26
- Qonfig::Plugins.names
27
- end
28
- end
17
+ # @return [Array<String>]
18
+ #
19
+ # @see Qonfig::Plugins
20
+ #
21
+ # @api public
22
+ # @since 0.4.0
23
+ def plugins
24
+ Qonfig::Plugins.names
29
25
  end
30
26
  end
@@ -1,129 +1,125 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qonfig
4
- module Plugins
5
- # @api private
6
- # @since 0.4.0
7
- class Registry
8
- # @return [void]
9
- #
10
- # @api private
11
- # @since 0.4.0
12
- def initialize
13
- @plugin_set = {}
14
- @access_lock = Mutex.new
15
- end
16
-
17
- # @param plugin_name [Symbol, String]
18
- # @return [Qonfig::Plugins::Abstract]
19
- #
20
- # @api private
21
- # @since 0.4.0
22
- def [](plugin_name)
23
- thread_safe { fetch(plugin_name) }
24
- end
25
-
26
- # @param plugin_name [Symbol, String]
27
- # @param plugin_module [Qonfig::Plugins::Abstract]
28
- # @return [void]
29
- #
30
- # @api private
31
- # @since 0.4.0
32
- def register(plugin_name, plugin_module)
33
- thread_safe { apply(plugin_name, plugin_module) }
34
- end
35
- alias_method :[]=, :register
36
-
37
- # @return [Array<String>]
38
- #
39
- # @api private
40
- # @since 0.4.0
41
- def names
42
- thread_safe { plugin_names }
43
- end
44
-
45
- private
46
-
47
- # @return [Hash]
48
- #
49
- # @api private
50
- # @since 0.4.0
51
- attr_reader :plugin_set
52
-
53
- # @return [Mutex]
54
- #
55
- # @api private
56
- # @since 0.4.0
57
- attr_reader :access_lock
58
-
59
- # @return [void]
60
- #
61
- # @api private
62
- # @since 0.4.0
63
- def thread_safe
64
- access_lock.synchronize { yield if block_given? }
65
- end
66
-
67
- # @return [Array<String>]
68
- #
69
- # @api private
70
- # @since 0.4.0
71
- def plugin_names
72
- plugin_set.keys
73
- end
74
-
75
- # @param plugin_name [String]
76
- # @return [Boolean]
77
- #
78
- # @api private
79
- # @since 0.4.0
80
- def registered?(plugin_name)
81
- plugin_set.key?(plugin_name)
82
- end
83
-
84
- # @param plugin_name [Symbol, String]
85
- # @param plugin_module [Qonfig::Plugins::Abstract]
86
- # @return [void]
87
- #
88
- # @raise [Qonfig::AlreadyRegisteredPluginError]
89
- #
90
- # @api private
91
- # @since 0.4.0
92
- def apply(plugin_name, plugin_module)
93
- plugin_name = indifferently_accessable_plugin_name(plugin_name)
94
-
95
- if registered?(plugin_name)
96
- raise Qonfig::AlreadyRegisteredPluginError, "#{plugin_name} plugin already exist"
97
- end
98
-
99
- plugin_set[plugin_name] = plugin_module
100
- end
101
-
102
- # @param plugin_name [Symbol, String]
103
- # @return [Qonfig::Plugins::Abstract]
104
- #
105
- # @raise [Qonfig::UnregisteredPluginError]
106
- #
107
- # @api private
108
- # @since 0.4.0
109
- def fetch(plugin_name)
110
- plugin_name = indifferently_accessable_plugin_name(plugin_name)
111
-
112
- unless registered?(plugin_name)
113
- raise Qonfig::UnregisteredPluginError, "#{plugin_name} plugin is not registered"
114
- end
115
-
116
- plugin_set[plugin_name]
117
- end
118
-
119
- # @param key [Symbol, String]
120
- # @return [String]
121
- #
122
- # @api private
123
- # @since 0.4.0
124
- def indifferently_accessable_plugin_name(plugin_name)
125
- plugin_name.to_s
126
- end
3
+ # @api private
4
+ # @since 0.4.0
5
+ class Qonfig::Plugins::Registry
6
+ # @return [void]
7
+ #
8
+ # @api private
9
+ # @since 0.4.0
10
+ def initialize
11
+ @plugin_set = {}
12
+ @access_lock = Mutex.new
13
+ end
14
+
15
+ # @param plugin_name [Symbol, String]
16
+ # @return [Qonfig::Plugins::Abstract]
17
+ #
18
+ # @api private
19
+ # @since 0.4.0
20
+ def [](plugin_name)
21
+ thread_safe { fetch(plugin_name) }
22
+ end
23
+
24
+ # @param plugin_name [Symbol, String]
25
+ # @param plugin_module [Qonfig::Plugins::Abstract]
26
+ # @return [void]
27
+ #
28
+ # @api private
29
+ # @since 0.4.0
30
+ def register(plugin_name, plugin_module)
31
+ thread_safe { apply(plugin_name, plugin_module) }
32
+ end
33
+ alias_method :[]=, :register
34
+
35
+ # @return [Array<String>]
36
+ #
37
+ # @api private
38
+ # @since 0.4.0
39
+ def names
40
+ thread_safe { plugin_names }
41
+ end
42
+
43
+ private
44
+
45
+ # @return [Hash]
46
+ #
47
+ # @api private
48
+ # @since 0.4.0
49
+ attr_reader :plugin_set
50
+
51
+ # @return [Mutex]
52
+ #
53
+ # @api private
54
+ # @since 0.4.0
55
+ attr_reader :access_lock
56
+
57
+ # @return [void]
58
+ #
59
+ # @api private
60
+ # @since 0.4.0
61
+ def thread_safe
62
+ access_lock.synchronize { yield if block_given? }
63
+ end
64
+
65
+ # @return [Array<String>]
66
+ #
67
+ # @api private
68
+ # @since 0.4.0
69
+ def plugin_names
70
+ plugin_set.keys
71
+ end
72
+
73
+ # @param plugin_name [String]
74
+ # @return [Boolean]
75
+ #
76
+ # @api private
77
+ # @since 0.4.0
78
+ def registered?(plugin_name)
79
+ plugin_set.key?(plugin_name)
80
+ end
81
+
82
+ # @param plugin_name [Symbol, String]
83
+ # @param plugin_module [Qonfig::Plugins::Abstract]
84
+ # @return [void]
85
+ #
86
+ # @raise [Qonfig::AlreadyRegisteredPluginError]
87
+ #
88
+ # @api private
89
+ # @since 0.4.0
90
+ def apply(plugin_name, plugin_module)
91
+ plugin_name = indifferently_accessable_plugin_name(plugin_name)
92
+
93
+ if registered?(plugin_name)
94
+ raise Qonfig::AlreadyRegisteredPluginError, "#{plugin_name} plugin already exist"
95
+ end
96
+
97
+ plugin_set[plugin_name] = plugin_module
98
+ end
99
+
100
+ # @param plugin_name [Symbol, String]
101
+ # @return [Qonfig::Plugins::Abstract]
102
+ #
103
+ # @raise [Qonfig::UnregisteredPluginError]
104
+ #
105
+ # @api private
106
+ # @since 0.4.0
107
+ def fetch(plugin_name)
108
+ plugin_name = indifferently_accessable_plugin_name(plugin_name)
109
+
110
+ unless registered?(plugin_name)
111
+ raise Qonfig::UnregisteredPluginError, "#{plugin_name} plugin is not registered"
127
112
  end
113
+
114
+ plugin_set[plugin_name]
115
+ end
116
+
117
+ # @param key [Symbol, String]
118
+ # @return [String]
119
+ #
120
+ # @api private
121
+ # @since 0.4.0
122
+ def indifferently_accessable_plugin_name(plugin_name)
123
+ plugin_name.to_s
128
124
  end
129
125
  end
@@ -1,62 +1,64 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qonfig
4
- # @api public
3
+ # @api public
4
+ # @since 0.4.0
5
+ module Qonfig::Plugins
6
+ require_relative 'plugins/registry'
7
+ require_relative 'plugins/access_mixin'
8
+ require_relative 'plugins/abstract'
9
+
10
+ # @since 0.4.0
11
+ @plugin_registry = Registry.new
5
12
  # @since 0.4.0
6
- module Plugins
13
+ @access_lock = Mutex.new
14
+
15
+ class << self
16
+ # @param plugin_name [Symbol, String]
17
+ # @return [void]
18
+ #
19
+ # @api public
20
+ # @since 0.4.0
21
+ def load(plugin_name)
22
+ thread_safe { plugin_registry[plugin_name].load! }
23
+ end
24
+
25
+ # @return [Array<String>]
26
+ #
27
+ # @api public
28
+ # @since 0.4.0
29
+ def names
30
+ thread_safe { plugin_registry.names }
31
+ end
32
+
33
+ # @param plugin_name [Symbol, String]
34
+ # @return [void]
35
+ #
36
+ # @api private
37
+ # @since 0.4.0
38
+ def register_plugin(plugin_name, plugin_module)
39
+ thread_safe { plugin_registry[plugin_name] = plugin_module }
40
+ end
41
+
42
+ private
43
+
44
+ # @return [Qonfig::Plugins::Registry]
45
+ #
46
+ # @api private
47
+ # @since 0.4.0
48
+ attr_reader :plugin_registry
49
+
50
+ # @return [Mutex]
51
+ #
52
+ # @api private
7
53
  # @since 0.4.0
8
- @plugin_registry = Registry.new
54
+ attr_reader :access_lock
55
+
56
+ # @return [void]
57
+ #
58
+ # @api private
9
59
  # @since 0.4.0
10
- @access_lock = Mutex.new
11
-
12
- class << self
13
- # @param plugin_name [Symbol, String]
14
- # @return [void]
15
- #
16
- # @api public
17
- # @since 0.4.0
18
- def load(plugin_name)
19
- thread_safe { plugin_registry[plugin_name].load! }
20
- end
21
-
22
- # @return [Array<String>]
23
- #
24
- # @api public
25
- # @since 0.4.0
26
- def names
27
- thread_safe { plugin_registry.names }
28
- end
29
-
30
- # @param plugin_name [Symbol, String]
31
- # @return [void]
32
- #
33
- # @api private
34
- # @since 0.4.0
35
- def register_plugin(plugin_name, plugin_module)
36
- thread_safe { plugin_registry[plugin_name] = plugin_module }
37
- end
38
-
39
- private
40
-
41
- # @return [Qonfig::Plugins::Registry]
42
- #
43
- # @api private
44
- # @since 0.4.0
45
- attr_reader :plugin_registry
46
-
47
- # @return [Mutex]
48
- #
49
- # @api private
50
- # @since 0.4.0
51
- attr_reader :access_lock
52
-
53
- # @return [void]
54
- #
55
- # @api private
56
- # @since 0.4.0
57
- def thread_safe
58
- access_lock.synchronize { yield if block_given? }
59
- end
60
+ def thread_safe
61
+ access_lock.synchronize { yield if block_given? }
60
62
  end
61
63
  end
62
64
  end
@@ -1,21 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Qonfig
4
- class Settings
3
+ # @api private
4
+ # @since 0.2.0
5
+ module Qonfig::Settings::Builder
6
+ class << self
7
+ # @param commands [Qonfig::CommandSet]
8
+ # @return [Qonfig::Settings]
9
+ #
5
10
  # @api private
6
11
  # @since 0.2.0
7
- module Builder
8
- class << self
9
- # @param [Qonfig::CommandSet]
10
- # @return [Qonfig::Settings]
11
- #
12
- # @api private
13
- # @since 0.2.0
14
- def build(commands)
15
- Qonfig::Settings.new.tap do |settings|
16
- commands.each { |command| command.call(settings) }
17
- end
18
- end
12
+ def build(commands)
13
+ Qonfig::Settings.new.tap do |settings|
14
+ commands.each { |command| command.call(settings) }
19
15
  end
20
16
  end
21
17
  end