kefir 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +7 -1
- data/README.md +1 -1
- data/kefir.gemspec +2 -0
- data/lib/kefir/config.rb +68 -0
- data/lib/kefir/file_store.rb +19 -0
- data/lib/kefir/version.rb +1 -1
- data/lib/kefir.rb +18 -87
- data/spec/kefir/config_spec.rb +124 -0
- data/spec/kefir/file_store_spec.rb +43 -0
- data/spec/kefir_spec.rb +24 -173
- data/spec/spec_helper.rb +3 -0
- metadata +35 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13d8742a055b653495c9e655d9469c0ecbd1894a
|
4
|
+
data.tar.gz: 095c8d016e1d849c91f5556497cc755978777768
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3ce4f0d0abcc83bc1a3b22ed3a70b3ece3b9cb2d79062340dac8a89d0699c4adb8f9b92d04e5eda025dc4cb013179bb4ab88d7abc9f5c0b8d438a12a25c5175
|
7
|
+
data.tar.gz: 8a38019191d5ce6264e3b327b53ae30a159efb5cfca9e3617ed97ee284aefd2ec93e5773c976021ee86406cf2b4ed8c57edc500ec811cc2ca6530b1bc637b6cf
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
kefir (
|
4
|
+
kefir (1.0.0)
|
5
5
|
dig_rb (~> 1.0, >= 1.0.1)
|
6
6
|
env_paths (~> 0.0.2)
|
7
7
|
|
@@ -16,6 +16,7 @@ GEM
|
|
16
16
|
ast (~> 2.2)
|
17
17
|
powerpack (0.1.1)
|
18
18
|
rainbow (2.1.0)
|
19
|
+
rake (10.5.0)
|
19
20
|
rspec (3.5.0)
|
20
21
|
rspec-core (~> 3.5.0)
|
21
22
|
rspec-expectations (~> 3.5.0)
|
@@ -42,6 +43,11 @@ PLATFORMS
|
|
42
43
|
ruby
|
43
44
|
|
44
45
|
DEPENDENCIES
|
46
|
+
bundler (~> 1.7)
|
45
47
|
kefir!
|
48
|
+
rake (~> 10.0)
|
46
49
|
rspec (~> 3)
|
47
50
|
rubocop (~> 0.45)
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
1.12.5
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
kefir
|
1
|
+
kefir [![Build Status](https://travis-ci.org/NickTomlin/ruby-kefir.png?branch=master)](https://travis-ci.org/NickTomlin/ruby-kefir) [![Gem Version](https://badge.fury.io/rb/kefir.svg)](https://badge.fury.io/rb/kefir)
|
2
2
|
===
|
3
3
|
|
4
4
|
Simple configuration for your Gem or application. A ruby port of [conf](https://www.npmjs.com/package/conf).
|
data/kefir.gemspec
CHANGED
@@ -16,6 +16,8 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_dependency 'env_paths', '~> 0.0.2'
|
18
18
|
s.add_dependency 'dig_rb', '~> 1.0', '>= 1.0.1'
|
19
|
+
s.add_development_dependency 'bundler', '~> 1.7'
|
20
|
+
s.add_development_dependency 'rake', '~> 10.0'
|
19
21
|
s.add_development_dependency 'rspec', '~> 3'
|
20
22
|
s.add_development_dependency 'rubocop', '~> 0.45'
|
21
23
|
end
|
data/lib/kefir/config.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module Kefir
|
2
|
+
class Config
|
3
|
+
include Enumerable
|
4
|
+
extend Forwardable
|
5
|
+
|
6
|
+
def_delegators :@config, :key?, :delete
|
7
|
+
def_delegator :@store, :path
|
8
|
+
|
9
|
+
def each(&block)
|
10
|
+
@config.each(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(store, options)
|
14
|
+
@store = store
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
def config
|
19
|
+
@config ||= @store.read.merge!(@options.fetch(:defaults, {}))
|
20
|
+
end
|
21
|
+
|
22
|
+
def persist
|
23
|
+
@store.write(config)
|
24
|
+
end
|
25
|
+
|
26
|
+
def set(*paths)
|
27
|
+
if paths.first.is_a?(Hash)
|
28
|
+
config.merge!(paths.first)
|
29
|
+
elsif paths.size >= 2
|
30
|
+
deep_set(config, paths)
|
31
|
+
else
|
32
|
+
raise ArgumentError.new, 'Kefir::Config.set accepts a hash or key(s) and value'
|
33
|
+
end
|
34
|
+
|
35
|
+
config
|
36
|
+
end
|
37
|
+
|
38
|
+
def empty!
|
39
|
+
@config = {}
|
40
|
+
end
|
41
|
+
|
42
|
+
def get(*paths)
|
43
|
+
config.dig(*paths)
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_s
|
47
|
+
config.to_s
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_h
|
51
|
+
config.dup
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def deep_set(hash, paths)
|
57
|
+
value = paths.pop
|
58
|
+
*keys, last_key = paths
|
59
|
+
|
60
|
+
nested = keys.inject(hash) do |h, key|
|
61
|
+
h[key] = {} unless h[key]
|
62
|
+
h[key]
|
63
|
+
end
|
64
|
+
|
65
|
+
nested[last_key] = value unless nested.nil?
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Kefir
|
2
|
+
class FileStore
|
3
|
+
attr_reader :path
|
4
|
+
|
5
|
+
def initialize(config_file_path)
|
6
|
+
@path = config_file_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def read
|
10
|
+
File.read(@path) do |contents|
|
11
|
+
YAML.load(contents)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def write(data)
|
16
|
+
File.write(@path, YAML.dump(data))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/kefir/version.rb
CHANGED
data/lib/kefir.rb
CHANGED
@@ -1,102 +1,33 @@
|
|
1
|
-
require 'kefir/version'
|
2
1
|
require 'dig_rb'
|
3
2
|
require 'env_paths'
|
4
|
-
require '
|
3
|
+
require 'yaml'
|
4
|
+
require 'kefir/version'
|
5
|
+
require 'kefir/config'
|
6
|
+
require 'kefir/file_store'
|
5
7
|
|
6
8
|
module Kefir
|
7
9
|
class MissingNamespaceError < StandardError; end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def initialize(config_file_path)
|
13
|
-
@path = config_file_path
|
14
|
-
end
|
15
|
-
|
16
|
-
def read
|
17
|
-
File.read(@path) do |contents|
|
18
|
-
YAML.load(contents)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def write(data)
|
23
|
-
File.write(@path, YAML.dump(data))
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class Config
|
28
|
-
include Enumerable
|
29
|
-
extend Forwardable
|
30
|
-
|
31
|
-
def_delegators :@config, :key?, :delete
|
32
|
-
def_delegator :@store, :path
|
33
|
-
|
34
|
-
def each(&block)
|
35
|
-
@config.each(&block)
|
36
|
-
end
|
37
|
-
|
38
|
-
def initialize(store, options)
|
39
|
-
@store = store
|
40
|
-
@options = options
|
41
|
-
end
|
42
|
-
|
43
|
-
def config
|
44
|
-
@config ||= @store.read.merge!(@options.fetch(:defaults, {}))
|
45
|
-
end
|
46
|
-
|
47
|
-
def persist
|
48
|
-
@store.write(config)
|
49
|
-
end
|
50
|
-
|
51
|
-
def set(*paths)
|
52
|
-
if paths.first.is_a?(Hash)
|
53
|
-
config.merge!(paths.first)
|
54
|
-
elsif paths.size >= 2
|
55
|
-
deep_set(config, paths)
|
56
|
-
else
|
57
|
-
raise ArgumentError.new, 'Kefir::Config.set accepts a hash or key(s) and value'
|
58
|
-
end
|
11
|
+
DEFAULT_OPTIONS = {
|
12
|
+
config_name: 'config.yml'
|
13
|
+
}.freeze
|
59
14
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
def empty!
|
64
|
-
@config = {}
|
65
|
-
end
|
66
|
-
|
67
|
-
def get(*paths)
|
68
|
-
config.dig(*paths)
|
69
|
-
end
|
70
|
-
|
71
|
-
def to_s
|
72
|
-
config.to_s
|
73
|
-
end
|
74
|
-
|
75
|
-
def to_h
|
76
|
-
config.dup
|
77
|
-
end
|
78
|
-
|
79
|
-
private
|
80
|
-
|
81
|
-
def deep_set(hash, paths)
|
82
|
-
value = paths.pop
|
83
|
-
*keys, last_key = paths
|
15
|
+
def self.config(namespace, options = {})
|
16
|
+
raise MissingNamespaceError, 'You must supply a namespace for your configuration files' if namespace.nil? || namespace.empty?
|
84
17
|
|
85
|
-
|
86
|
-
|
87
|
-
h[key]
|
88
|
-
end
|
18
|
+
parsed_options = parse_options(namespace, options)
|
19
|
+
store = FileStore.new(parsed_options[:config_file_path])
|
89
20
|
|
90
|
-
|
91
|
-
end
|
21
|
+
Config.new(store, options)
|
92
22
|
end
|
93
23
|
|
94
|
-
|
95
|
-
raise MissingNamespaceError, 'You must supply a namespace for your configuration files' if namespace.nil? || namespace.empty?
|
24
|
+
private_class_method
|
96
25
|
|
97
|
-
|
98
|
-
|
26
|
+
def self.parse_options(namespace, options)
|
27
|
+
parsed = DEFAULT_OPTIONS.merge(options)
|
28
|
+
cwd = parsed.fetch(:cwd, EnvPaths.get(namespace).config)
|
29
|
+
parsed[:config_file_path] = File.expand_path(parsed[:config_name], cwd)
|
99
30
|
|
100
|
-
|
31
|
+
parsed
|
101
32
|
end
|
102
33
|
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
RSpec.describe Kefir::Config do
|
2
|
+
let(:store_double) { double(Kefir::FileStore, read: {}, write: nil) }
|
3
|
+
let(:config) { Kefir::Config.new(store_double, {}) }
|
4
|
+
|
5
|
+
describe 'initialize' do
|
6
|
+
it 'accepts a default option that shallowly merges with config' do
|
7
|
+
store = double(Kefir::FileStore, read: { should_change: 'change_me', should_stay: 'immortal' }, write: nil)
|
8
|
+
options = {
|
9
|
+
defaults: {
|
10
|
+
should_change: 'changed!'
|
11
|
+
}
|
12
|
+
}
|
13
|
+
config = Kefir::Config.new(store, options)
|
14
|
+
|
15
|
+
expect(config.get(:should_change)).to eq('changed!')
|
16
|
+
expect(config.get(:should_stay)).to eq('immortal')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'includes enumerable methods' do
|
21
|
+
config.set(foo: 'hay', biz: 'needle')
|
22
|
+
|
23
|
+
expect(config.any? { |_, v| v == 'needle' }).to eq(true)
|
24
|
+
expect(config.count).to eq(2)
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'set' do
|
28
|
+
it 'sets config' do
|
29
|
+
config.set(:foo, 'bar')
|
30
|
+
|
31
|
+
expect(config.get(:foo)).to eq('bar')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'accepts a hash' do
|
35
|
+
config.set(foo: 'bar', biz: 'baz', hsh: { nested: 'value' })
|
36
|
+
|
37
|
+
expect(config.get(:hsh)).to eq(nested: 'value')
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'raises an error if less than two arguments are supplied' do
|
41
|
+
expect do
|
42
|
+
config.set(:foo)
|
43
|
+
end.to raise_exception(ArgumentError, 'Kefir::Config.set accepts a hash or key(s) and value')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'handles nested config values' do
|
47
|
+
config.set(:my, :nested, :value, 'hello')
|
48
|
+
|
49
|
+
expect(config.get(:my, :nested, :value)).to eq('hello')
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'handles array indexes' do
|
53
|
+
config.set(:nested, :array, [])
|
54
|
+
config.set(:nested, :array, 0, 'hello')
|
55
|
+
|
56
|
+
expect(config.get(:nested, :array, 0)).to eq('hello')
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'handles a mixture of symbols and strings' do
|
60
|
+
config.set(:one, 'two', 'boo')
|
61
|
+
|
62
|
+
expect(config.get(:one, 'two')).to eq('boo')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'empty!' do
|
67
|
+
it 'empties config' do
|
68
|
+
config.set(foo: 'hay', biz: 'needle')
|
69
|
+
expect(config.count).to eq(2)
|
70
|
+
config.empty!
|
71
|
+
expect(config.count).to eq(0)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'path' do
|
76
|
+
it 'returns the path of its store' do
|
77
|
+
store = double(Kefir::FileStore, path: '/config/path')
|
78
|
+
config = Kefir::Config.new(store, {})
|
79
|
+
|
80
|
+
expect(config.path).to eq('/config/path')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'methods delegated to config hash' do
|
85
|
+
it 'allows inspection via key?' do
|
86
|
+
config.set(:one, 'one')
|
87
|
+
|
88
|
+
expect(config.key?(:one)).to eq(true)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'allows for the deletion of items via delete' do
|
92
|
+
config.set(one: 'one', two: 'two')
|
93
|
+
expect(config.key?(:one)).to eq(true)
|
94
|
+
config.delete(:one)
|
95
|
+
|
96
|
+
expect(config.to_h).to eq(two: 'two')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe 'to_s' do
|
101
|
+
it 'stringifies as the value of it\'s config hash' do
|
102
|
+
config.set(:one, :two, 'bar')
|
103
|
+
|
104
|
+
expect(config.to_s).to eq('{:one=>{:two=>"bar"}}')
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe 'to_h' do
|
109
|
+
it 'provides access to the underlying hash' do
|
110
|
+
config.set(:one, 'bar')
|
111
|
+
|
112
|
+
expect(config.to_h).to eq(one: 'bar')
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe 'persist' do
|
117
|
+
it 'persists data to a store' do
|
118
|
+
expect(store_double).to receive(:write).with(one: { two: 'bar' })
|
119
|
+
|
120
|
+
config.set(:one, :two, 'bar')
|
121
|
+
config.persist
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
RSpec.describe Kefir::FileStore do
|
2
|
+
let(:data) { { secret: 'dont tell', users: [{ name: 'bob' }, { name: 'jane' }] } }
|
3
|
+
let(:file_path) { File.join(@temp_dir, 'config') }
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@temp_dir = Dir.mktmpdir
|
7
|
+
end
|
8
|
+
|
9
|
+
after(:each) do
|
10
|
+
FileUtils.remove_dir(@temp_dir)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'reads a configuration file' do
|
14
|
+
File.write(file_path, YAML.dump(data))
|
15
|
+
Kefir::FileStore.new(file_path)
|
16
|
+
parsed = YAML.load_file(file_path)
|
17
|
+
|
18
|
+
expect(parsed).to eq(
|
19
|
+
secret: 'dont tell',
|
20
|
+
users: [
|
21
|
+
{ name: 'bob' },
|
22
|
+
{ name: 'jane' }
|
23
|
+
]
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'writes to a configuration file' do
|
28
|
+
File.write(file_path, YAML.dump(data))
|
29
|
+
store = Kefir::FileStore.new(file_path)
|
30
|
+
|
31
|
+
store.write(data.merge(secret: 'changed'))
|
32
|
+
|
33
|
+
parsed = YAML.load_file(file_path)
|
34
|
+
|
35
|
+
expect(parsed).to eq(
|
36
|
+
secret: 'changed',
|
37
|
+
users: [
|
38
|
+
{ name: 'bob' },
|
39
|
+
{ name: 'jane' }
|
40
|
+
]
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
data/spec/kefir_spec.rb
CHANGED
@@ -1,177 +1,4 @@
|
|
1
|
-
require 'YAML'
|
2
|
-
require 'tmpdir'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
1
|
RSpec.describe Kefir do
|
6
|
-
describe Kefir::Config do
|
7
|
-
let(:store_double) { double(Kefir::FileStore, read: {}, write: nil) }
|
8
|
-
let(:config) { Kefir::Config.new(store_double, {}) }
|
9
|
-
|
10
|
-
describe 'initialize' do
|
11
|
-
it 'accepts a default option that shallowly merges with config' do
|
12
|
-
store = double(Kefir::FileStore, read: { should_change: 'change_me', should_stay: 'immortal' }, write: nil)
|
13
|
-
options = {
|
14
|
-
defaults: {
|
15
|
-
should_change: 'changed!'
|
16
|
-
}
|
17
|
-
}
|
18
|
-
config = Kefir::Config.new(store, options)
|
19
|
-
|
20
|
-
expect(config.get(:should_change)).to eq('changed!')
|
21
|
-
expect(config.get(:should_stay)).to eq('immortal')
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'includes enumerable methods' do
|
26
|
-
config.set(foo: 'hay', biz: 'needle')
|
27
|
-
|
28
|
-
expect(config.any? { |_, v| v == 'needle' }).to eq(true)
|
29
|
-
expect(config.count).to eq(2)
|
30
|
-
end
|
31
|
-
|
32
|
-
describe 'set' do
|
33
|
-
it 'sets config' do
|
34
|
-
config.set(:foo, 'bar')
|
35
|
-
|
36
|
-
expect(config.get(:foo)).to eq('bar')
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'accepts a hash' do
|
40
|
-
config.set(foo: 'bar', biz: 'baz', hsh: { nested: 'value' })
|
41
|
-
|
42
|
-
expect(config.get(:hsh)).to eq(nested: 'value')
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'raises an error if less than two arguments are supplied' do
|
46
|
-
expect do
|
47
|
-
config.set(:foo)
|
48
|
-
end.to raise_exception(ArgumentError, 'Kefir::Config.set accepts a hash or key(s) and value')
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'handles nested config values' do
|
52
|
-
config.set(:my, :nested, :value, 'hello')
|
53
|
-
|
54
|
-
expect(config.get(:my, :nested, :value)).to eq('hello')
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'handles array indexes' do
|
58
|
-
config.set(:nested, :array, [])
|
59
|
-
config.set(:nested, :array, 0, 'hello')
|
60
|
-
|
61
|
-
expect(config.get(:nested, :array, 0)).to eq('hello')
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'handles a mixture of symbols and strings' do
|
65
|
-
config.set(:one, 'two', 'boo')
|
66
|
-
|
67
|
-
expect(config.get(:one, 'two')).to eq('boo')
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe 'empty!' do
|
72
|
-
it 'empties config' do
|
73
|
-
config.set(foo: 'hay', biz: 'needle')
|
74
|
-
expect(config.count).to eq(2)
|
75
|
-
config.empty!
|
76
|
-
expect(config.count).to eq(0)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe 'path' do
|
81
|
-
it 'returns the path of its store' do
|
82
|
-
store = double(Kefir::FileStore, path: '/config/path')
|
83
|
-
config = Kefir::Config.new(store, {})
|
84
|
-
|
85
|
-
expect(config.path).to eq('/config/path')
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
context 'methods delegated to config hash' do
|
90
|
-
it 'allows inspection via key?' do
|
91
|
-
config.set(:one, 'one')
|
92
|
-
|
93
|
-
expect(config.key?(:one)).to eq(true)
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'allows for the deletion of items via delete' do
|
97
|
-
config.set(one: 'one', two: 'two')
|
98
|
-
expect(config.key?(:one)).to eq(true)
|
99
|
-
config.delete(:one)
|
100
|
-
|
101
|
-
expect(config.to_h).to eq(two: 'two')
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe 'to_s' do
|
106
|
-
it 'stringifies as the value of it\'s config hash' do
|
107
|
-
config.set(:one, :two, 'bar')
|
108
|
-
|
109
|
-
expect(config.to_s).to eq('{:one=>{:two=>"bar"}}')
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
describe 'to_h' do
|
114
|
-
it 'provides access to the underlying hash' do
|
115
|
-
config.set(:one, 'bar')
|
116
|
-
|
117
|
-
expect(config.to_h).to eq(one: 'bar')
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
describe 'persist' do
|
122
|
-
it 'persists data to a store' do
|
123
|
-
expect(store_double).to receive(:write).with(one: { two: 'bar' })
|
124
|
-
|
125
|
-
config.set(:one, :two, 'bar')
|
126
|
-
config.persist
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
describe Kefir::FileStore do
|
132
|
-
let(:data) { { secret: 'dont tell', users: [{ name: 'bob' }, { name: 'jane' }] } }
|
133
|
-
let(:file_path) { File.join(@temp_dir, 'config') }
|
134
|
-
|
135
|
-
before(:each) do
|
136
|
-
@temp_dir = Dir.mktmpdir
|
137
|
-
end
|
138
|
-
|
139
|
-
after(:each) do
|
140
|
-
FileUtils.remove_dir(@temp_dir)
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'reads a configuration file' do
|
144
|
-
File.write(file_path, YAML.dump(data))
|
145
|
-
Kefir::FileStore.new(file_path)
|
146
|
-
parsed = YAML.load_file(file_path)
|
147
|
-
|
148
|
-
expect(parsed).to eq(
|
149
|
-
secret: 'dont tell',
|
150
|
-
users: [
|
151
|
-
{ name: 'bob' },
|
152
|
-
{ name: 'jane' }
|
153
|
-
]
|
154
|
-
)
|
155
|
-
end
|
156
|
-
|
157
|
-
it 'writes to a configuration file' do
|
158
|
-
File.write(file_path, YAML.dump(data))
|
159
|
-
store = Kefir::FileStore.new(file_path)
|
160
|
-
|
161
|
-
store.write(data.merge(secret: 'changed'))
|
162
|
-
|
163
|
-
parsed = YAML.load_file(file_path)
|
164
|
-
|
165
|
-
expect(parsed).to eq(
|
166
|
-
secret: 'changed',
|
167
|
-
users: [
|
168
|
-
{ name: 'bob' },
|
169
|
-
{ name: 'jane' }
|
170
|
-
]
|
171
|
-
)
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
2
|
describe '.config' do
|
176
3
|
it 'throws an error if no namespace is provided' do
|
177
4
|
expect do
|
@@ -200,5 +27,29 @@ RSpec.describe Kefir do
|
|
200
27
|
config = Kefir.config('test')
|
201
28
|
config.persist
|
202
29
|
end
|
30
|
+
|
31
|
+
context 'options' do
|
32
|
+
it 'provides defaults' do
|
33
|
+
expect(File).to receive(:read).with(/config\.yml$/).and_yield("user: bob\napi_key: secret-value")
|
34
|
+
config = Kefir.config('test')
|
35
|
+
config.get(:foo)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'allows overriding current directory' do
|
39
|
+
custom_cwd = File.expand_path('custom/path', '/')
|
40
|
+
expect(File).to receive(:read).with(File.join(custom_cwd, 'config.yml')).and_yield("user: bob\napi_key: secret-value")
|
41
|
+
|
42
|
+
config = Kefir.config('test', cwd: custom_cwd)
|
43
|
+
config.get(:foo)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'allows overriding the :config_name' do
|
47
|
+
custom_cwd = File.expand_path('custom/path', '/')
|
48
|
+
expect(File).to receive(:read).with(File.join(custom_cwd, 'custom_config_name.yml')).and_yield("user: bob\napi_key: secret-value")
|
49
|
+
|
50
|
+
config = Kefir.config('test', cwd: custom_cwd, config_name: 'custom_config_name.yml')
|
51
|
+
config.get(:foo)
|
52
|
+
end
|
53
|
+
end
|
203
54
|
end
|
204
55
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kefir
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Tomlin
|
@@ -44,6 +44,34 @@ dependencies:
|
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: 1.0.1
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.7'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.7'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
47
75
|
- !ruby/object:Gem::Dependency
|
48
76
|
name: rspec
|
49
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -90,7 +118,11 @@ files:
|
|
90
118
|
- Rakefile
|
91
119
|
- kefir.gemspec
|
92
120
|
- lib/kefir.rb
|
121
|
+
- lib/kefir/config.rb
|
122
|
+
- lib/kefir/file_store.rb
|
93
123
|
- lib/kefir/version.rb
|
124
|
+
- spec/kefir/config_spec.rb
|
125
|
+
- spec/kefir/file_store_spec.rb
|
94
126
|
- spec/kefir_spec.rb
|
95
127
|
- spec/spec_helper.rb
|
96
128
|
homepage:
|
@@ -118,5 +150,7 @@ signing_key:
|
|
118
150
|
specification_version: 4
|
119
151
|
summary: Simple configuration for your application/gem
|
120
152
|
test_files:
|
153
|
+
- spec/kefir/config_spec.rb
|
154
|
+
- spec/kefir/file_store_spec.rb
|
121
155
|
- spec/kefir_spec.rb
|
122
156
|
- spec/spec_helper.rb
|