anyway_config 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 823f018ceb705907a1036edc866bf656039b10d9
4
- data.tar.gz: 99094de1c4829dd8400f08d1614fea2750ba005c
3
+ metadata.gz: e88db3a37e515c2b4fbcb002b3dbe984300fb800
4
+ data.tar.gz: b753b1df76dfc0d48b52a9cfc038b418df5a4fba
5
5
  SHA512:
6
- metadata.gz: 2df8b0c108c4e52cae357201ee79f3504ebd951e35e2ab568aa800636be11fc3fb8f3ad5526dfec5b91055bb861ce9188bca832d08d615eaa1bba8a7dea9dfe1
7
- data.tar.gz: c12bf044e159260ed53cafa28bc4ea75e187572df2b4fa31300444a31e1a9f670661f1798c091dfe4edd9d359ce728f33f42e720d982ea15badadd73a05d26e3
6
+ metadata.gz: 7421be11264c928c62e2edd22452e446f4c8995b40e13de5f748a94dbfa740654a2954128d0c690be085a95a8a92d983beabf00f5004cf0cf80377e1546c9723
7
+ data.tar.gz: 488df50e28e9469aa094c7b3b6b69fba76f835fa0070ed86ad81c25181e89562aa055dfb73cde5e12e45b64f660b7a9f289458c28fd8c253ba51d23c88e8312b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Change log
2
2
 
3
+ ## 1.1.3 (2017-12-20)
4
+
5
+ - Allow to pass raw hash with explicit values to `Config.new`. ([@dsalahutdinov][])
6
+
7
+ Example:
8
+
9
+ ```ruby
10
+ Sniffer::Config.new(
11
+ overrides: {
12
+ enabled: true,
13
+ storage: {capacity: 500}
14
+ }
15
+ )
16
+ ```
17
+
18
+ See more https://github.com/palkan/anyway_config/pull/10
19
+
3
20
  ## 1.1.2 (2017-11-19)
4
21
 
5
22
  - Enable aliases for YAML. ([@onemanstartup][])
@@ -36,3 +53,4 @@ Initial version.
36
53
 
37
54
  [@palkan]: https://github.com/palkan
38
55
  [@onemanstartup]: https://github.com/onemanstartup
56
+ [@dsalahutdinov]: https://github.com/dsalahutdinov
data/README.md CHANGED
@@ -12,6 +12,8 @@ Libraries using Anyway Config:
12
12
 
13
13
  - [AnyCable](https://github.com/anycable/anycable)
14
14
 
15
+ - [Sniffer](https://github.com/aderyabin/sniffer)
16
+
15
17
  - [and others](https://github.com/palkan/anyway_config/network/dependents).
16
18
 
17
19
  ## Installation
@@ -87,6 +89,23 @@ module MyCoolGem
87
89
  end
88
90
  ```
89
91
 
92
+ #### Provide explicit values
93
+
94
+ Sometimes it's useful to set some parameters explicitly during config initialization.
95
+ You can do that using `overrides` option:
96
+
97
+ ```ruby
98
+ config = MyCoolGem::Config.new(
99
+ overrides: {
100
+ user: 'john',
101
+ password: 'rubyisnotdead'
102
+ }
103
+ )
104
+
105
+ # The value would not be overriden from other sources (such as YML file, env)
106
+ config.user == 'john'
107
+ ```
108
+
90
109
  ### Dynamic configuration
91
110
 
92
111
  You can also create configuration objects without pre-defined schema (just like `Rails.application.config_for` but more [powerful](#railsapplicationconfig_for-vs-anywayconfigfor)):
@@ -118,6 +137,7 @@ Environmental variables work the same way as with Rails.
118
137
 
119
138
  There are `#clear` and `#reload` functions on your config (which do exactly what they state).
120
139
 
140
+ Note: `#reload` also accepts `overrides` key to provide explicit values (see above).
121
141
 
122
142
  ## `Rails.application.config_for` vs `Anyway::Config.for`
123
143
 
@@ -13,7 +13,6 @@ Gem::Specification.new do |s|
13
13
  s.summary = "Configuration DSL for Ruby libraries and applications"
14
14
  s.description = %{
15
15
  Configuration DSL for Ruby libraries and applications.
16
-
17
16
  Allows you to easily follow the twelve-factor application principles (https://12factor.net/config).
18
17
  }
19
18
 
@@ -24,6 +23,6 @@ Gem::Specification.new do |s|
24
23
  s.required_ruby_version = '>= 2.2'
25
24
 
26
25
  s.add_development_dependency "rspec", "~> 3.5"
27
- s.add_development_dependency "rubocop", "~> 0.49"
26
+ s.add_development_dependency "rubocop", "~> 0.52"
28
27
  s.add_development_dependency "simplecov", ">= 0.3.8"
29
28
  end
data/lib/anyway/config.rb CHANGED
@@ -44,21 +44,35 @@ module Anyway # :nodoc:
44
44
  # my_config = Anyway::Config.for(:my_app)
45
45
  # # will load data from config/my_app.yml, secrets.my_app, ENV["MY_APP_*"]
46
46
  def for(name)
47
- new(name, false).load_from_sources
47
+ new(name: name, load: false).load_from_sources
48
48
  end
49
49
  end
50
50
 
51
51
  attr_reader :config_name
52
52
 
53
- def initialize(config_name = nil, do_load = true)
54
- @config_name = config_name || self.class.config_name
53
+ # Instantiate config with specified name, loads the data and applies overrides
54
+ #
55
+ # Example:
56
+ #
57
+ # my_config = Anyway::Config.new(name: :my_app, load: true, overrides: { some: :value })
58
+ #
59
+ # rubocop:disable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
60
+ def initialize(config_name = nil, do_load = nil, name: nil, load: true, overrides: {})
61
+ unless config_name.nil? && do_load.nil?
62
+ warn "[Deprecated] Positional arguments for Anyway::Config#initialize will be removed in 1.2.0. Use keyword arguments instead: initialize(name:, load:, overrides:)"
63
+ end
64
+ name = config_name unless config_name.nil?
65
+ load = do_load unless do_load.nil?
66
+
67
+ @config_name = name || self.class.config_name
55
68
  raise ArgumentError, "Config name is missing" unless @config_name
56
- load if do_load
69
+ self.load(overrides) if load
57
70
  end
71
+ # rubocop:enable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
58
72
 
59
- def reload
73
+ def reload(overrides = {})
60
74
  clear
61
- load
75
+ load(overrides)
62
76
  self
63
77
  end
64
78
 
@@ -69,8 +83,10 @@ module Anyway # :nodoc:
69
83
  self
70
84
  end
71
85
 
72
- def load
86
+ def load(overrides = {})
73
87
  config = load_from_sources((self.class.defaults || {}).deep_dup)
88
+
89
+ config.merge!(overrides) unless overrides.nil?
74
90
  config.each do |key, val|
75
91
  set_value(key, val)
76
92
  end
@@ -86,9 +102,7 @@ module Anyway # :nodoc:
86
102
  def load_from_file(config)
87
103
  config_path = Anyway.env.fetch(config_name).delete('conf') ||
88
104
  "./config/#{config_name}.yml"
89
- if config_path && File.file?(config_path)
90
- config.deep_merge!(parse_yml(config_path) || {})
91
- end
105
+ config.deep_merge!(parse_yml(config_path) || {}) if config_path && File.file?(config_path)
92
106
  config
93
107
  end
94
108
 
@@ -18,9 +18,7 @@ module Anyway
18
18
 
19
19
  def load_from_file(config)
20
20
  config_path = Rails.root.join("config", "#{@config_name}.yml")
21
- if File.file? config_path
22
- config.deep_merge!(parse_yml(config_path)[Rails.env] || {})
23
- end
21
+ config.deep_merge!(parse_yml(config_path)[Rails.env] || {}) if File.file? config_path
24
22
  config
25
23
  end
26
24
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anyway # :nodoc:
4
- VERSION = "1.1.2"
4
+ VERSION = "1.1.3"
5
5
  end
data/spec/config_spec.rb CHANGED
@@ -54,6 +54,11 @@ describe Anyway::Config do
54
54
  expect(conf.host).to eq "test.host"
55
55
  end
56
56
 
57
+ it "sets overrides after loading YAML" do
58
+ config = CoolConfig.new(overrides: { host: 'overrided.host' })
59
+ expect(config.host).to eq "overrided.host"
60
+ end
61
+
57
62
  if Rails.application.respond_to?(:secrets)
58
63
  it "load config from secrets" do
59
64
  expect(conf.user[:name]).to eq "test"
@@ -79,6 +84,17 @@ describe Anyway::Config do
79
84
  ENV['ANYWAY_SECRET_PASSWORD'] = 'my_pass'
80
85
  expect(conf.user[:password]).to eq 'my_pass'
81
86
  end
87
+
88
+ it "overrides loaded value by explicit" do
89
+ ENV['ANYWAY_SECRET_PASSWORD'] = 'my_pass'
90
+
91
+ config = CoolConfig.new(
92
+ overrides: {
93
+ user: { password: 'explicit_password' }
94
+ }
95
+ )
96
+ expect(config.user[:password]).to eq "explicit_password"
97
+ end
82
98
  end
83
99
 
84
100
  describe "clear" do
@@ -118,9 +134,7 @@ describe Anyway::Config do
118
134
  data = Anyway::Config.for(:my_app)
119
135
  expect(data[:test]).to eq 1
120
136
  expect(data[:name]).to eq 'my_app'
121
- if Rails.application.respond_to?(:secrets)
122
- expect(data[:secret]).to eq 'my_secret'
123
- end
137
+ expect(data[:secret]).to eq 'my_secret' if Rails.application.respond_to?(:secrets)
124
138
  end
125
139
  end
126
140
 
@@ -133,6 +147,14 @@ describe Anyway::Config do
133
147
  end
134
148
  end
135
149
 
150
+ context "config with initial hash values" do
151
+ let(:conf) { SmallConfig.new(overrides: { 'meta': 'dummy' }) }
152
+
153
+ it "works" do
154
+ expect(conf.meta).to eq 'dummy'
155
+ end
156
+ end
157
+
136
158
  context "when name is missing" do
137
159
  let(:config) do
138
160
  Class.new(described_class)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anyway_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-19 00:00:00.000000000 Z
11
+ date: 2017-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.49'
33
+ version: '0.52'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.49'
40
+ version: '0.52'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: simplecov
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.3.8
55
- description: "\n Configuration DSL for Ruby libraries and applications.\n\n Allows
55
+ description: "\n Configuration DSL for Ruby libraries and applications.\n Allows
56
56
  you to easily follow the twelve-factor application principles (https://12factor.net/config).\n
57
57
  \ "
58
58
  email: