canfig 0.0.5 → 0.0.10
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 +3 -0
- data/lib/canfig/config.rb +27 -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/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: 70fda774961e755691e8a4783071ce7298b694933c4db7b2227b8563c1afeae7
|
4
|
+
data.tar.gz: 87b8c4825bf0e9b3e8d03a97c5e2f3c0643e67cca4703f48eea4e44411304959
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ad137615641ae7ca8c67716720f34c3e4068e92117887eb1f9fe786aa33175da0457d128236477fef4052391106b276ce4b8e278fd8825879e2139ea3d0972f
|
7
|
+
data.tar.gz: 2811ee9abc222199f3fdba1b5398f4e938891faf756f24080c7514e6c853668ea56c47b73847b04922d72f59bac3262f5479ba69d7da040a106d674fe5f7a124
|
data/lib/canfig.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
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'
|
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,26 @@ module Canfig
|
|
29
36
|
end
|
30
37
|
end
|
31
38
|
|
39
|
+
def env(key, default=nil, &block)
|
40
|
+
@state[key] ||= begin
|
41
|
+
val = ENV.fetch(key.to_s.underscore.upcase, default, &block)
|
42
|
+
val && ENV.key?(val) ? env(val, default, &block) : val
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def clear(*keys)
|
47
|
+
save_state! do
|
48
|
+
keys.each { |key| @state[key] = nil }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
32
52
|
def []=(key, val)
|
33
53
|
set key, val
|
34
54
|
end
|
35
55
|
|
36
|
-
def get(key)
|
56
|
+
def get(key, default=nil, &block)
|
37
57
|
raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
|
38
|
-
@state[key]
|
58
|
+
@state[key] || (block_given? ? yield : default)
|
39
59
|
end
|
40
60
|
|
41
61
|
def [](key)
|
@@ -47,7 +67,7 @@ module Canfig
|
|
47
67
|
end
|
48
68
|
|
49
69
|
def changed
|
50
|
-
Hash[@
|
70
|
+
Hash[@allowed.map { |key| [key, [@saved_state[key], @state[key]]] if @saved_state[key] != @state[key] }.compact]
|
51
71
|
end
|
52
72
|
|
53
73
|
def changed?(key)
|
@@ -92,7 +112,7 @@ module Canfig
|
|
92
112
|
opt = meth.to_s.gsub(/=/,'').to_sym
|
93
113
|
return set(opt, args.first) if allowed?(opt)
|
94
114
|
else
|
95
|
-
return
|
115
|
+
return get(meth, *args, &block) if allowed?(meth)
|
96
116
|
end
|
97
117
|
|
98
118
|
super
|
@@ -100,9 +120,8 @@ module Canfig
|
|
100
120
|
|
101
121
|
protected
|
102
122
|
|
103
|
-
def initialize(*args, &block)
|
104
|
-
|
105
|
-
@allowed = (args + options.symbolize_keys.keys).uniq.map(&:to_sym)
|
123
|
+
def initialize(*args, **options, &block)
|
124
|
+
@allowed = (args.map(&:to_sym) + options.symbolize_keys.keys).uniq
|
106
125
|
@state = {}
|
107
126
|
enable_state_saves!
|
108
127
|
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 get(key, default=nil, &block)
|
11
|
+
super || env(key, default, &block)
|
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
|
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.10
|
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
|