kefir 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 34bd27c107be11b6e9051bf444f5eee377758915
4
+ data.tar.gz: 7354cd1b6751c3453a5e8c3cdf573f22cef8fe77
5
+ SHA512:
6
+ metadata.gz: 0a9cb0e32386852224d238643322e50d42d54371f6bff1315fcf383032f8e46668ec2b3cc1427ed64fa637e5780fc5e0cdda4963715184dd2c660614623c93aa
7
+ data.tar.gz: e6237785bf1bd138c2a0f710b00e9ba01b9de95d62ed09dd6dc0ac1c424ae4287480829e6acdba5b7c5f0fe3b115790a6d63a7e0808e3a2ffa2cfe811b5e9c09
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,4 @@
1
+ Documentation:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install:
6
+ - gem update bundler
7
+ notifications:
8
+ email:
9
+ on_success: never
10
+ on_failure: always
11
+ ignore:
12
+ - README.md
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kefir (0.1.0)
5
+ dig_rb (~> 1.0, >= 1.0.1)
6
+ env_paths (~> 0.0.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.3.0)
12
+ diff-lcs (1.2.5)
13
+ dig_rb (1.0.1)
14
+ env_paths (0.0.2)
15
+ parser (2.3.2.0)
16
+ ast (~> 2.2)
17
+ powerpack (0.1.1)
18
+ rainbow (2.1.0)
19
+ rspec (3.5.0)
20
+ rspec-core (~> 3.5.0)
21
+ rspec-expectations (~> 3.5.0)
22
+ rspec-mocks (~> 3.5.0)
23
+ rspec-core (3.5.4)
24
+ rspec-support (~> 3.5.0)
25
+ rspec-expectations (3.5.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.5.0)
28
+ rspec-mocks (3.5.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.5.0)
31
+ rspec-support (3.5.0)
32
+ rubocop (0.45.0)
33
+ parser (>= 2.3.1.1, < 3.0)
34
+ powerpack (~> 0.1)
35
+ rainbow (>= 1.99.1, < 3.0)
36
+ ruby-progressbar (~> 1.7)
37
+ unicode-display_width (~> 1.0, >= 1.0.1)
38
+ ruby-progressbar (1.8.1)
39
+ unicode-display_width (1.1.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ kefir!
46
+ rspec (~> 3)
47
+ rubocop (~> 0.45)
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nick Tomlin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ kefir kefir [![Build Status](https://travis-ci.org/NickTomlin/kefir.png?branch=master)](https://travis-ci.org/NickTomlin/kefir.fury.io/rb/kefir.svg)](https://badge.fury.io/rb/kefir)
2
+ ===
3
+
4
+ Simple configuration for your Gem or application. A ruby port of [conf](https://www.npmjs.com/package/conf).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'kefir'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install kefir
21
+
22
+ ## Usage
23
+
24
+ ```
25
+ require 'kefir'
26
+
27
+ config = Kefier.config('my_gem')
28
+ api_key = config.get(:api_key)
29
+
30
+ config.set(:api_key, api_key + '!')
31
+
32
+ # write your changes
33
+ config.persist
34
+ ```
35
+
36
+ `get` and `set` can accept multiple keys for nested paths:
37
+
38
+ ```
39
+ config.set(:my, :nested, :value, 'hello!')
40
+ value = config.get(:my, :nested, :value)
41
+
42
+ expect(value).to eq('hello!')
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/nicktomlin/kefir/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Run the tests `rake`
50
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 5. Push to the branch (`git push origin my-new-feature`)
52
+ 6. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rubygems/package_task'
4
+ require 'rspec/core/rake_task'
5
+
6
+ spec = eval(File.read('kefir.gemspec')) # rubocop:disable Lint/Eval
7
+
8
+ Gem::PackageTask.new(spec)
9
+ RuboCop::RakeTask.new
10
+ RSpec::Core::RakeTask.new(:spec)
11
+
12
+ task default: %w(rubocop spec)
data/kefir.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/kefir/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'kefir'
6
+ s.version = Kefir::VERSION
7
+ s.authors = ['Nick Tomlin']
8
+ s.email = ['nick.tomlin@gmail.com']
9
+ s.summary = 'Simple configuration for your application/gem'
10
+ s.licenses = ['MIT']
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
15
+ s.require_paths = ['lib']
16
+
17
+ s.add_dependency 'env_paths', '~> 0.0.2'
18
+ s.add_dependency 'dig_rb', '~> 1.0', '>= 1.0.1'
19
+ s.add_development_dependency 'rspec', '~> 3'
20
+ s.add_development_dependency 'rubocop', '~> 0.45'
21
+ end
@@ -0,0 +1,3 @@
1
+ module Kefir
2
+ VERSION = '0.1.0'
3
+ end
data/lib/kefir.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'kefir/version'
2
+ require 'dig_rb'
3
+ require 'env_paths'
4
+ require 'YAML'
5
+
6
+ module Kefir
7
+ class MissingNamespaceError < StandardError; end
8
+
9
+ class FileStore
10
+ attr_reader :path
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
59
+
60
+ config
61
+ end
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
84
+
85
+ nested = keys.inject(hash) do |h, key|
86
+ h[key] = {} unless h[key]
87
+ h[key]
88
+ end
89
+
90
+ nested[last_key] = value unless nested.nil?
91
+ end
92
+ end
93
+
94
+ def self.config(namespace, options = {})
95
+ raise MissingNamespaceError, 'You must supply a namespace for your configuration files' if namespace.nil? || namespace.empty?
96
+
97
+ config_file_path = EnvPaths.get(namespace).config
98
+ store = FileStore.new(config_file_path)
99
+
100
+ Config.new(store, options)
101
+ end
102
+ end
@@ -0,0 +1,204 @@
1
+ require 'YAML'
2
+ require 'tmpdir'
3
+ require 'fileutils'
4
+
5
+ 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
+ describe '.config' do
176
+ it 'throws an error if no namespace is provided' do
177
+ expect do
178
+ Kefir.config(nil)
179
+ end.to raise_exception(Kefir::MissingNamespaceError)
180
+ end
181
+
182
+ it 'throws an error if an empty namespace is provided' do
183
+ expect do
184
+ Kefir.config('')
185
+ end.to raise_exception(Kefir::MissingNamespaceError)
186
+ end
187
+
188
+ it 'returns a fully functioning config object that can read config' do
189
+ allow(File).to receive(:read).and_yield("user: bob\napi_key: secret-value")
190
+
191
+ config = Kefir.config('test')
192
+
193
+ expect(config.get('user')).to eq('bob')
194
+ end
195
+
196
+ it 'returns a fully functioning config object that can write config' do
197
+ allow(File).to receive(:read).and_yield("user: bob\napi_key: secret-value")
198
+ expect(File).to receive(:write)
199
+
200
+ config = Kefir.config('test')
201
+ config.persist
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'kefir'
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kefir
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Tomlin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: env_paths
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: dig_rb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.0.1
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.0'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.45'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.45'
75
+ description:
76
+ email:
77
+ - nick.tomlin@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".rspec"
84
+ - ".rubocop.yml"
85
+ - ".travis.yml"
86
+ - Gemfile
87
+ - Gemfile.lock
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - kefir.gemspec
92
+ - lib/kefir.rb
93
+ - lib/kefir/version.rb
94
+ - spec/kefir_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage:
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.6.7
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Simple configuration for your application/gem
120
+ test_files:
121
+ - spec/kefir_spec.rb
122
+ - spec/spec_helper.rb