tty-config 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +103 -18
- data/lib/tty/config.rb +66 -129
- data/lib/tty/config/dependency_loader.rb +55 -0
- data/lib/tty/config/generator.rb +57 -0
- data/lib/tty/config/marshaller.rb +56 -0
- data/lib/tty/config/marshaller_registry.rb +43 -0
- data/lib/tty/config/marshallers.rb +35 -0
- data/lib/tty/config/marshallers/hcl_marshaller.rb +25 -0
- data/lib/tty/config/marshallers/ini_marshaller.rb +28 -0
- data/lib/tty/config/marshallers/java_props_marshaller.rb +25 -0
- data/lib/tty/config/marshallers/json_marshaller.rb +25 -0
- data/lib/tty/config/marshallers/toml_marshaller.rb +25 -0
- data/lib/tty/config/marshallers/yaml_marshaller.rb +29 -0
- data/lib/tty/config/version.rb +1 -1
- metadata +54 -32
- data/Rakefile +0 -8
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/spec/spec_helper.rb +0 -54
- data/spec/unit/alias_setting_spec.rb +0 -72
- data/spec/unit/append_spec.rb +0 -26
- data/spec/unit/autoload_env_spec.rb +0 -62
- data/spec/unit/delete_spec.rb +0 -22
- data/spec/unit/exist_spec.rb +0 -24
- data/spec/unit/fetch_spec.rb +0 -45
- data/spec/unit/generate_spec.rb +0 -70
- data/spec/unit/merge_spec.rb +0 -22
- data/spec/unit/new_spec.rb +0 -6
- data/spec/unit/normalize_hash_spec.rb +0 -21
- data/spec/unit/read_spec.rb +0 -118
- data/spec/unit/remove_spec.rb +0 -16
- data/spec/unit/set_from_env_spec.rb +0 -78
- data/spec/unit/set_if_empty_spec.rb +0 -26
- data/spec/unit/set_spec.rb +0 -62
- data/spec/unit/validate_spec.rb +0 -76
- data/spec/unit/write_spec.rb +0 -197
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-config.gemspec +0 -28
data/Rakefile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "tty/config"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
if ENV['COVERAGE'] || ENV['TRAVIS']
|
2
|
-
require 'simplecov'
|
3
|
-
require 'coveralls'
|
4
|
-
|
5
|
-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
6
|
-
SimpleCov::Formatter::HTMLFormatter,
|
7
|
-
Coveralls::SimpleCov::Formatter
|
8
|
-
]
|
9
|
-
|
10
|
-
SimpleCov.start do
|
11
|
-
command_name 'spec'
|
12
|
-
add_filter 'spec'
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
require "bundler/setup"
|
17
|
-
require "tty/config"
|
18
|
-
|
19
|
-
module TestHelpers
|
20
|
-
module Paths
|
21
|
-
def gem_root
|
22
|
-
File.expand_path(File.join(File.dirname(__FILE__), ".."))
|
23
|
-
end
|
24
|
-
|
25
|
-
def dir_path(*args)
|
26
|
-
path = File.join(gem_root, *args)
|
27
|
-
FileUtils.mkdir_p(path) unless ::File.exist?(path)
|
28
|
-
File.realpath(path)
|
29
|
-
end
|
30
|
-
|
31
|
-
def tmp_path(*args)
|
32
|
-
File.join(dir_path('tmp'), *args)
|
33
|
-
end
|
34
|
-
|
35
|
-
def fixtures_path(*args)
|
36
|
-
File.join(dir_path('spec/fixtures'), *args)
|
37
|
-
end
|
38
|
-
|
39
|
-
def within_dir(target, &block)
|
40
|
-
::Dir.chdir(target, &block)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
RSpec.configure do |config|
|
46
|
-
config.include(TestHelpers::Paths)
|
47
|
-
config.disable_monkey_patching!
|
48
|
-
config.after(:example, type: :cli) do
|
49
|
-
FileUtils.rm_rf(tmp_path)
|
50
|
-
end
|
51
|
-
config.expect_with :rspec do |c|
|
52
|
-
c.syntax = :expect
|
53
|
-
end
|
54
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Config, '#alias_setting' do
|
4
|
-
it "aliases setting key" do
|
5
|
-
config = TTY::Config.new
|
6
|
-
config.set :foo, value: :baz
|
7
|
-
|
8
|
-
config.alias_setting :foo, to: :bar
|
9
|
-
|
10
|
-
expect(config.fetch(:foo)).to eq(:baz)
|
11
|
-
expect(config.fetch(:bar)).to eq(:baz)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "aliases nested key to flat key" do
|
15
|
-
config = TTY::Config.new
|
16
|
-
config.set(:foo, :bar, :baz) { 12 }
|
17
|
-
|
18
|
-
config.alias_setting(:foo, :bar, :baz, to: :flat_foo)
|
19
|
-
|
20
|
-
expect(config.fetch(:foo, :bar, :baz)).to eq(12)
|
21
|
-
expect(config.fetch(:flat_foo)).to eq(12)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "aliases nested key as a string to flat key" do
|
25
|
-
config = TTY::Config.new
|
26
|
-
config.set('foo.bar.baz') { 12 }
|
27
|
-
|
28
|
-
config.alias_setting(:foo, :bar, :baz, to: :flat_foo)
|
29
|
-
|
30
|
-
expect(config.fetch(:foo, :bar, :baz)).to eq(12)
|
31
|
-
expect(config.fetch(:flat_foo)).to eq(12)
|
32
|
-
end
|
33
|
-
|
34
|
-
it "aliases nested key to nested key" do
|
35
|
-
config = TTY::Config.new
|
36
|
-
config.set(:foo, :bar, :baz) { 12 }
|
37
|
-
|
38
|
-
config.alias_setting(:foo, :bar, :baz, to: [:bee, :bop])
|
39
|
-
|
40
|
-
expect(config.fetch(:foo, :bar, :baz)).to eq(12)
|
41
|
-
expect(config.fetch(:bee, :bop)).to eq(12)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "fails to alias to already existing key" do
|
45
|
-
config = TTY::Config.new
|
46
|
-
config.set(:foo, value: 1)
|
47
|
-
config.set(:bar, value: 2)
|
48
|
-
|
49
|
-
expect {
|
50
|
-
config.alias_setting(:foo, to: :bar)
|
51
|
-
}.to raise_error(ArgumentError, "Setting already exists with an alias ':bar'")
|
52
|
-
end
|
53
|
-
|
54
|
-
it "fails to alias to already existing key" do
|
55
|
-
config = TTY::Config.new
|
56
|
-
config.set(:foo, :bar, value: 1)
|
57
|
-
config.set(:baz, :woo, value: 2)
|
58
|
-
|
59
|
-
expect {
|
60
|
-
config.alias_setting(:foo, :bar, to: [:baz, :woo])
|
61
|
-
}.to raise_error(ArgumentError, "Setting already exists with an alias ':baz, :woo'")
|
62
|
-
end
|
63
|
-
|
64
|
-
it "fails to alias to matching key" do
|
65
|
-
config = TTY::Config.new
|
66
|
-
config.set(:foo, :bar, value: 1)
|
67
|
-
|
68
|
-
expect {
|
69
|
-
config.alias_setting(:foo, :bar, to: [:foo, :bar])
|
70
|
-
}.to raise_error(ArgumentError, "Alias matches setting key")
|
71
|
-
end
|
72
|
-
end
|
data/spec/unit/append_spec.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
RSpec.describe TTY::Config, '#append' do
|
2
|
-
it "returns appended values" do
|
3
|
-
config = TTY::Config.new
|
4
|
-
values = config.append(:foo, :bar, to: :values)
|
5
|
-
|
6
|
-
expect(values).to eq([:foo, :bar])
|
7
|
-
expect(config.fetch(:values)).to eq([:foo, :bar])
|
8
|
-
end
|
9
|
-
|
10
|
-
it "appends values to already existing key" do
|
11
|
-
config = TTY::Config.new
|
12
|
-
config.set(:values) { :foo }
|
13
|
-
values = config.append(:bar, :baz, to: :values)
|
14
|
-
|
15
|
-
expect(config.fetch(:values)).to eq([:foo, :bar, :baz])
|
16
|
-
expect(values).to eq([:foo, :bar, :baz])
|
17
|
-
end
|
18
|
-
|
19
|
-
it "appends values to nested key" do
|
20
|
-
config = TTY::Config.new
|
21
|
-
config.set(:foo, :bar) { 1 }
|
22
|
-
values = config.append(2,3, to: [:foo, :bar])
|
23
|
-
expect(values).to eq([1,2,3])
|
24
|
-
expect(config.fetch(:foo, :bar)).to eq([1,2,3])
|
25
|
-
end
|
26
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Config, '#autoload_env' do
|
4
|
-
it "autoloads env variables" do
|
5
|
-
allow(ENV).to receive(:[]).with('HOST').and_return('localhost')
|
6
|
-
config = TTY::Config.new
|
7
|
-
expect(config.fetch(:host)).to eq(nil)
|
8
|
-
|
9
|
-
config.autoload_env
|
10
|
-
|
11
|
-
expect(config.fetch(:host)).to eq('localhost')
|
12
|
-
end
|
13
|
-
|
14
|
-
it "autoloads env variables with prefix" do
|
15
|
-
allow(ENV).to receive(:[]).with('MYTOOL_HOST').and_return('localhost')
|
16
|
-
config = TTY::Config.new
|
17
|
-
config.env_prefix = 'mytool'
|
18
|
-
|
19
|
-
expect(config.fetch(:host)).to eq(nil)
|
20
|
-
|
21
|
-
config.autoload_env
|
22
|
-
|
23
|
-
expect(config.fetch(:host)).to eq('localhost')
|
24
|
-
end
|
25
|
-
|
26
|
-
it "prioritises set over env vars" do
|
27
|
-
allow(ENV).to receive(:[]).with('HOST').and_return('localhost')
|
28
|
-
config = TTY::Config.new
|
29
|
-
config.autoload_env
|
30
|
-
|
31
|
-
config.set(:host, value: 'myhost')
|
32
|
-
|
33
|
-
expect(config.fetch(:host)).to eq('myhost')
|
34
|
-
end
|
35
|
-
|
36
|
-
it "prioritises env vars over defaults when a keyword" do
|
37
|
-
allow(ENV).to receive(:[]).with('PORT').and_return('7727')
|
38
|
-
config = TTY::Config.new
|
39
|
-
|
40
|
-
config.autoload_env
|
41
|
-
|
42
|
-
expect(config.fetch(:port, default: '3000')).to eq('7727')
|
43
|
-
end
|
44
|
-
|
45
|
-
it "prioritises env vars over defaults when block" do
|
46
|
-
allow(ENV).to receive(:[]).with('PORT').and_return('7727')
|
47
|
-
config = TTY::Config.new
|
48
|
-
|
49
|
-
config.autoload_env
|
50
|
-
|
51
|
-
expect(config.fetch(:port) {'3000' }).to eq('7727')
|
52
|
-
end
|
53
|
-
|
54
|
-
it "prioritises present configuration over env vars" do
|
55
|
-
allow(ENV).to receive(:[]).with('PORT').and_return('7727')
|
56
|
-
config = TTY::Config.new(port: '3000')
|
57
|
-
|
58
|
-
config.autoload_env
|
59
|
-
|
60
|
-
expect(config.fetch(:port)).to eq('3000')
|
61
|
-
end
|
62
|
-
end
|
data/spec/unit/delete_spec.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
RSpec.describe TTY::Config, '#delete' do
|
2
|
-
it "deletes the value" do
|
3
|
-
config = TTY::Config.new
|
4
|
-
config.set(:foo, value: 2)
|
5
|
-
expect(config.delete(:foo)).to eq(2)
|
6
|
-
expect(config.fetch(:foo)).to eq(nil)
|
7
|
-
end
|
8
|
-
|
9
|
-
it "deletes the value under deeply nested key" do
|
10
|
-
config = TTY::Config.new
|
11
|
-
config.set(:foo, :bar, :baz) { 2 }
|
12
|
-
expect(config.delete(:foo, :bar, :baz).call).to eq(2)
|
13
|
-
expect(config.fetch(:foo, :bar, :baz)).to eq(nil)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "deletes innermost key with array value" do
|
17
|
-
config = TTY::Config.new
|
18
|
-
config.set(:foo, :bar, value: [1,2,3])
|
19
|
-
expect(config.delete(:foo, :bar)).to eq([1,2,3])
|
20
|
-
expect(config.fetch(:foo, :bar)).to eq(nil)
|
21
|
-
end
|
22
|
-
end
|
data/spec/unit/exist_spec.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
RSpec.describe TTY::Config, '#exist?', type: :cli do
|
2
|
-
it "checks if configuration file exists" do
|
3
|
-
config = TTY::Config.new
|
4
|
-
config.append_path(tmp_path)
|
5
|
-
|
6
|
-
expect(config.exist?).to eq(false)
|
7
|
-
|
8
|
-
config.write(tmp_path('investments.yml'))
|
9
|
-
|
10
|
-
expect(config.exist?).to eq(true)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "checks if a file without extension is present" do
|
14
|
-
config = TTY::Config.new
|
15
|
-
config.append_path tmp_path
|
16
|
-
|
17
|
-
expect(config.exist?).to eq(false)
|
18
|
-
|
19
|
-
config.write(tmp_path('investments'), format: :yml)
|
20
|
-
|
21
|
-
expect(config.exist?).to eq(true)
|
22
|
-
expect(config.persisted?).to eq(true)
|
23
|
-
end
|
24
|
-
end
|
data/spec/unit/fetch_spec.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
RSpec.describe TTY::Config do
|
2
|
-
it "fetches default if no value" do
|
3
|
-
config = TTY::Config.new
|
4
|
-
expect(config.fetch(:foo, default: :bar)).to eq(:bar)
|
5
|
-
end
|
6
|
-
|
7
|
-
it "fetches default proc value" do
|
8
|
-
config = TTY::Config.new
|
9
|
-
expect(config.fetch(:foo, default: -> { :bar })).to eq(:bar)
|
10
|
-
end
|
11
|
-
|
12
|
-
it "fetches deeply nested proc value" do
|
13
|
-
config = TTY::Config.new
|
14
|
-
expect(config.fetch(:foo, default: -> { -> { :bar }})).to eq(:bar)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "fetches default as block" do
|
18
|
-
config = TTY::Config.new
|
19
|
-
expect(config.fetch(:foo) { :bar }).to eq(:bar)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "fetches default as block for deeply nested missing key" do
|
23
|
-
config = TTY::Config.new
|
24
|
-
expect(config.fetch(:foo, :bar, :baz) { 2 }).to eq(2)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "fetches value for deeply nested key" do
|
28
|
-
config = TTY::Config.new
|
29
|
-
config.set(:foo, :bar, :baz, value: 2)
|
30
|
-
expect(config.fetch(:foo, :bar, :baz)).to eq(2)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "fetches value as string delimited by . for deeply nested key" do
|
34
|
-
config = TTY::Config.new
|
35
|
-
config.set('foo', 'bar', 'baz') { 2 }
|
36
|
-
expect(config.fetch("foo.bar.baz")).to eq(2)
|
37
|
-
end
|
38
|
-
|
39
|
-
it "fetches key with indifferent access" do
|
40
|
-
config = TTY::Config.new
|
41
|
-
config.set(:foo, :bar, :baz, value: 2)
|
42
|
-
|
43
|
-
expect(config.fetch('foo', :bar, 'baz')).to eq(2)
|
44
|
-
end
|
45
|
-
end
|
data/spec/unit/generate_spec.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe TTY::Config, '#generate' do
|
4
|
-
it "generate config content" do
|
5
|
-
conf = {
|
6
|
-
'int' => 1,
|
7
|
-
'false' => false,
|
8
|
-
'str' => 'hello',
|
9
|
-
'array' => [1,2,3],
|
10
|
-
'deep_array' => [
|
11
|
-
{foo: 1},
|
12
|
-
{bar: 2}
|
13
|
-
],
|
14
|
-
'section' => {
|
15
|
-
'value' => 1,
|
16
|
-
'empty' => nil,
|
17
|
-
'array' => [1,2,3]
|
18
|
-
},
|
19
|
-
"empty" => { },
|
20
|
-
'nil' => nil,
|
21
|
-
}
|
22
|
-
|
23
|
-
content = TTY::Config.generate(conf)
|
24
|
-
|
25
|
-
expect(content).to eq <<-EOS
|
26
|
-
array = 1,2,3
|
27
|
-
false = false
|
28
|
-
int = 1
|
29
|
-
str = hello
|
30
|
-
|
31
|
-
[deep_array]
|
32
|
-
foo = 1
|
33
|
-
bar = 2
|
34
|
-
|
35
|
-
[section]
|
36
|
-
value = 1
|
37
|
-
array = 1,2,3
|
38
|
-
EOS
|
39
|
-
end
|
40
|
-
|
41
|
-
it "generate config content with custom separator" do
|
42
|
-
conf = {
|
43
|
-
'str' => 'hello',
|
44
|
-
'array' => [1,2,3],
|
45
|
-
'deep_array' => [
|
46
|
-
{foo: 1},
|
47
|
-
{bar: 2}
|
48
|
-
],
|
49
|
-
'section' => {
|
50
|
-
'value' => 1,
|
51
|
-
'array' => [1,2,3]
|
52
|
-
}
|
53
|
-
}
|
54
|
-
|
55
|
-
content = TTY::Config.generate(conf, separator: ':')
|
56
|
-
|
57
|
-
expect(content).to eq <<-EOS
|
58
|
-
array : 1,2,3
|
59
|
-
str : hello
|
60
|
-
|
61
|
-
[deep_array]
|
62
|
-
foo : 1
|
63
|
-
bar : 2
|
64
|
-
|
65
|
-
[section]
|
66
|
-
value : 1
|
67
|
-
array : 1,2,3
|
68
|
-
EOS
|
69
|
-
end
|
70
|
-
end
|
data/spec/unit/merge_spec.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
RSpec.describe TTY::Config, '#merge' do
|
2
|
-
it "merges nested hash" do
|
3
|
-
config = TTY::Config.new
|
4
|
-
config.set(:a, :b, value: 1)
|
5
|
-
config.set(:a, :c, value: 2)
|
6
|
-
|
7
|
-
config.merge({'a' => {'c' => 3, 'd' => 4}})
|
8
|
-
|
9
|
-
expect(config.fetch(:a, :b)).to eq(1)
|
10
|
-
expect(config.fetch(:a, :c)).to eq(3)
|
11
|
-
expect(config.fetch(:a, :d)).to eq(4)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "only merges hash like objects" do
|
15
|
-
config = TTY::Config.new
|
16
|
-
config.set(:a, :b, value: 1)
|
17
|
-
|
18
|
-
config.merge(Object.new)
|
19
|
-
|
20
|
-
expect(config.to_hash).to match({"a" => {"b" => 1}})
|
21
|
-
end
|
22
|
-
end
|