canfig 0.0.4 → 0.0.9
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 +5 -5
- data/lib/canfig.rb +10 -0
- data/lib/canfig/config.rb +25 -8
- data/lib/canfig/env_config.rb +20 -0
- data/lib/canfig/instance.rb +1 -1
- data/lib/canfig/module.rb +1 -1
- data/lib/canfig/open_config.rb +5 -4
- data/lib/canfig/version.rb +1 -1
- data/lib/canfig/yaml.rb +25 -0
- data/spec/canfig/yaml_spec.rb +38 -0
- data/spec/canfig_spec.rb +11 -0
- data/spec/fixtures/config.yml +3 -0
- metadata +13 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cb0af652bc6aced0a5a7be703c93267bbef555c28f821c565c9c5b14f93b6d20
|
4
|
+
data.tar.gz: cb3541d3ca774f5f7a331f75238ecef6231b62be3776b50ae52cac3b12182c6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aab3a3c345efea85b20c75e8aec11ccd62baab19e8dcfa5b5870406a2731e6894311a21273b10cfea3476f037185b300e862e027a19bf8314fbb9300054d2b78
|
7
|
+
data.tar.gz: ebc54005799b649dbecc527bd30f6bbad0149ad4fc022b2998323b0fe6b7888df12ffc1050a9884caee2ee14e30093f315408ed08e657b4e3a8dc657e3449459
|
data/lib/canfig.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
1
|
require 'active_support/core_ext/array/extract_options'
|
2
2
|
require 'active_support/core_ext/hash/keys'
|
3
|
+
require 'active_support/inflector'
|
3
4
|
require 'canfig/version'
|
5
|
+
require 'canfig/yaml'
|
4
6
|
require 'canfig/config'
|
5
7
|
require 'canfig/open_config'
|
8
|
+
require 'canfig/env_config'
|
6
9
|
require 'canfig/module'
|
7
10
|
require 'canfig/class'
|
8
11
|
require 'canfig/instance'
|
9
12
|
|
10
13
|
module Canfig
|
14
|
+
def self.new(*args, &block)
|
15
|
+
Canfig::Config.new *args, &block
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.open(*args, &block)
|
19
|
+
Canfig::OpenConfig.new *args, &block
|
20
|
+
end
|
11
21
|
end
|
data/lib/canfig/config.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
module Canfig
|
2
2
|
class Config
|
3
3
|
|
4
|
-
def configure(argh={}, &block)
|
4
|
+
def configure(argh={}, file=nil, &block)
|
5
5
|
save_state! do
|
6
|
+
configure_with_file file unless file.nil?
|
6
7
|
configure_with_args argh
|
7
8
|
configure_with_block &block
|
8
9
|
end
|
9
10
|
self
|
10
11
|
end
|
11
12
|
|
13
|
+
def configure_with_file(file)
|
14
|
+
save_state! do
|
15
|
+
configure_with_args(Canfig::YAML.new(file).load)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
12
19
|
def configure_with_args(argh)
|
13
20
|
save_state! do
|
14
21
|
argh.symbolize_keys.each { |key,val| set(key, val) }
|
@@ -29,13 +36,24 @@ module Canfig
|
|
29
36
|
end
|
30
37
|
end
|
31
38
|
|
39
|
+
def env(key, default=nil, &block)
|
40
|
+
val = ENV.fetch(key.to_s.underscore.upcase, default, &block)
|
41
|
+
val && ENV.key?(val) ? env(val, default, &block) : val
|
42
|
+
end
|
43
|
+
|
44
|
+
def clear(*keys)
|
45
|
+
save_state! do
|
46
|
+
keys.each { |key| @state[key] = nil }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
32
50
|
def []=(key, val)
|
33
51
|
set key, val
|
34
52
|
end
|
35
53
|
|
36
|
-
def get(key)
|
54
|
+
def get(key, default=nil, &block)
|
37
55
|
raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
|
38
|
-
@state[key]
|
56
|
+
@state[key] ||= env(key, default, &block)
|
39
57
|
end
|
40
58
|
|
41
59
|
def [](key)
|
@@ -47,7 +65,7 @@ module Canfig
|
|
47
65
|
end
|
48
66
|
|
49
67
|
def changed
|
50
|
-
Hash[@
|
68
|
+
Hash[@allowed.map { |key| [key, [@saved_state[key], @state[key]]] if @saved_state[key] != @state[key] }.compact]
|
51
69
|
end
|
52
70
|
|
53
71
|
def changed?(key)
|
@@ -92,7 +110,7 @@ module Canfig
|
|
92
110
|
opt = meth.to_s.gsub(/=/,'').to_sym
|
93
111
|
return set(opt, args.first) if allowed?(opt)
|
94
112
|
else
|
95
|
-
return
|
113
|
+
return get(meth, *args, &block) if allowed?(meth)
|
96
114
|
end
|
97
115
|
|
98
116
|
super
|
@@ -100,9 +118,8 @@ module Canfig
|
|
100
118
|
|
101
119
|
protected
|
102
120
|
|
103
|
-
def initialize(*args, &block)
|
104
|
-
|
105
|
-
@allowed = (args + options.symbolize_keys.keys).uniq.map(&:to_sym)
|
121
|
+
def initialize(*args, **options, &block)
|
122
|
+
@allowed = (args.map(&:to_sym) + options.symbolize_keys.keys).uniq
|
106
123
|
@state = {}
|
107
124
|
enable_state_saves!
|
108
125
|
configure options, &block
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Canfig
|
2
|
+
class EnvConfig < OpenConfig
|
3
|
+
attr_reader :namespace
|
4
|
+
|
5
|
+
def initialize(namespace=nil, **options, &block)
|
6
|
+
@namespace = namespace ? "#{namespace.to_s.underscore.upcase.gsub(/_$/, '')}_" : namespace
|
7
|
+
super(**options, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def set(key, val)
|
11
|
+
raise NotImplementedError, "You cannot set values on a Canfig::EnvConfig"
|
12
|
+
end
|
13
|
+
|
14
|
+
def env(key, default=nil, &block)
|
15
|
+
key = key.to_s.underscore.upcase
|
16
|
+
key = key.gsub(/^#{namespace}/, '') if namespace
|
17
|
+
super("#{namespace}#{key}", default, &block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/canfig/instance.rb
CHANGED
data/lib/canfig/module.rb
CHANGED
@@ -3,7 +3,7 @@ require 'active_support/core_ext/module/attribute_accessors'
|
|
3
3
|
module Canfig
|
4
4
|
module Module
|
5
5
|
def self.included(base)
|
6
|
-
base.send :
|
6
|
+
base.send :mattr_reader, :configuration
|
7
7
|
base.send :class_variable_set, :@@configuration, Canfig::OpenConfig.new
|
8
8
|
base.send :extend, ClassMethods
|
9
9
|
end
|
data/lib/canfig/open_config.rb
CHANGED
data/lib/canfig/version.rb
CHANGED
data/lib/canfig/yaml.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Canfig
|
2
|
+
class YAML
|
3
|
+
attr_reader :file
|
4
|
+
|
5
|
+
def initialize(file)
|
6
|
+
@file = file
|
7
|
+
end
|
8
|
+
|
9
|
+
def load
|
10
|
+
@data ||= ::YAML.load_file(file).symbolize_keys
|
11
|
+
end
|
12
|
+
alias_method :data, :load
|
13
|
+
|
14
|
+
def reload
|
15
|
+
@data = nil
|
16
|
+
self.load
|
17
|
+
end
|
18
|
+
|
19
|
+
def write
|
20
|
+
File.open(file, 'w') do |f|
|
21
|
+
f.write(::YAML.dump(data))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Canfig::YAML do
|
4
|
+
subject { described_class.new('spec/fixtures/config.yml') }
|
5
|
+
|
6
|
+
describe '#load' do
|
7
|
+
it 'loads the file into a hash' do
|
8
|
+
expect(subject.load).to be_a(Hash)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#write' do
|
13
|
+
subject { described_class.new('spec/fixtures/test.yml') }
|
14
|
+
|
15
|
+
before do
|
16
|
+
FileUtils.cp 'spec/fixtures/config.yml', 'spec/fixtures/test.yml'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'writes the hash back to the file in YAML' do
|
20
|
+
subject.data[:foo] = 'bar'
|
21
|
+
subject.write
|
22
|
+
subject.reload
|
23
|
+
expect(subject.data[:foo]).to eq('bar')
|
24
|
+
end
|
25
|
+
|
26
|
+
after do
|
27
|
+
FileUtils.rm 'spec/fixtures/test.yml'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#reload' do
|
32
|
+
it 'reloads the data from the file' do
|
33
|
+
subject.data[:foo] = 'bar'
|
34
|
+
subject.reload
|
35
|
+
expect(subject.data[:foo]).to eq('foo')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/canfig_spec.rb
CHANGED
@@ -1,4 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Canfig do
|
4
|
+
describe '.new' do
|
5
|
+
it 'returns a configuration object' do
|
6
|
+
expect(subject.new).to be_an_instance_of(Canfig::Config)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '.open' do
|
11
|
+
it 'returns an open configuration object' do
|
12
|
+
expect(subject.open).to be_an_instance_of(Canfig::OpenConfig)
|
13
|
+
end
|
14
|
+
end
|
4
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -62,18 +62,22 @@ files:
|
|
62
62
|
- lib/canfig.rb
|
63
63
|
- lib/canfig/class.rb
|
64
64
|
- lib/canfig/config.rb
|
65
|
+
- lib/canfig/env_config.rb
|
65
66
|
- lib/canfig/instance.rb
|
66
67
|
- lib/canfig/module.rb
|
67
68
|
- lib/canfig/open_config.rb
|
68
69
|
- lib/canfig/version.rb
|
70
|
+
- lib/canfig/yaml.rb
|
69
71
|
- spec/canfig/config_spec.rb
|
70
72
|
- spec/canfig/open_config_spec.rb
|
73
|
+
- spec/canfig/yaml_spec.rb
|
71
74
|
- spec/canfig_spec.rb
|
75
|
+
- spec/fixtures/config.yml
|
72
76
|
- spec/spec_helper.rb
|
73
77
|
homepage: http://github.com/markrebec/canfig
|
74
78
|
licenses: []
|
75
79
|
metadata: {}
|
76
|
-
post_install_message:
|
80
|
+
post_install_message:
|
77
81
|
rdoc_options: []
|
78
82
|
require_paths:
|
79
83
|
- lib
|
@@ -88,13 +92,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
92
|
- !ruby/object:Gem::Version
|
89
93
|
version: '0'
|
90
94
|
requirements: []
|
91
|
-
|
92
|
-
|
93
|
-
signing_key:
|
95
|
+
rubygems_version: 3.1.2
|
96
|
+
signing_key:
|
94
97
|
specification_version: 4
|
95
98
|
summary: Dead simple canned configuration for gems or whatever
|
96
99
|
test_files:
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/fixtures/config.yml
|
97
102
|
- spec/canfig/config_spec.rb
|
98
103
|
- spec/canfig/open_config_spec.rb
|
104
|
+
- spec/canfig/yaml_spec.rb
|
99
105
|
- spec/canfig_spec.rb
|
100
|
-
- spec/spec_helper.rb
|