canfig 0.0.5 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 87d7a71f3425215cb0226cd0f6eeabc7912dd552
4
- data.tar.gz: 378a6def629c7ed263384211bf8361fbe28338a6
2
+ SHA256:
3
+ metadata.gz: 70fda774961e755691e8a4783071ce7298b694933c4db7b2227b8563c1afeae7
4
+ data.tar.gz: 87b8c4825bf0e9b3e8d03a97c5e2f3c0643e67cca4703f48eea4e44411304959
5
5
  SHA512:
6
- metadata.gz: cbf3b023b2f8853dcbd0d5731baa98f9de2995ece01982da2718fe7919724efb7b4de7d350054330cf0749aaa7a5cd3c362f74db340e42c4521ca993800bbe09
7
- data.tar.gz: bb8e16f4eaee8db347246e7e45e5abcf16a673c1bb1eacab89b76f930a8eeac3d4abee81c638d208ef061e1b2115286bab8608078f5d09919ef949a3da65c14d
6
+ metadata.gz: 4ad137615641ae7ca8c67716720f34c3e4068e92117887eb1f9fe786aa33175da0457d128236477fef4052391106b276ce4b8e278fd8825879e2139ea3d0972f
7
+ data.tar.gz: 2811ee9abc222199f3fdba1b5398f4e938891faf756f24080c7514e6c853668ea56c47b73847b04922d72f59bac3262f5479ba69d7da040a106d674fe5f7a124
@@ -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'
@@ -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[@state.map { |key,val| [key, [@saved_state[key], val]] if @saved_state[key] != val }.compact]
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 @state[meth] if allowed?(meth)
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
- options = args.extract_options!
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
@@ -5,7 +5,7 @@ module Canfig
5
5
  end
6
6
 
7
7
  def configure(&block)
8
- @configuration.configure &block
8
+ configuration.configure &block
9
9
  end
10
10
  end
11
11
  end
@@ -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 :cattr_reader, :configuration
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
@@ -1,11 +1,12 @@
1
1
  module Canfig
2
2
  class OpenConfig < Config
3
- def allowed?(opt)
4
- true
3
+ def set(key, val)
4
+ @allowed << key unless @allowed.include?(key)
5
+ super(key, val)
5
6
  end
6
7
 
7
- def to_h
8
- @state.to_h
8
+ def allowed?(opt)
9
+ true
9
10
  end
10
11
  end
11
12
  end
@@ -1,3 +1,3 @@
1
1
  module Canfig
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.10'
3
3
  end
@@ -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
@@ -0,0 +1,3 @@
1
+ foo: 'foo'
2
+ bar: 'bar'
3
+ baz: 'baz'
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.5
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: 2015-05-21 00:00:00.000000000 Z
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
- rubyforge_project:
92
- rubygems_version: 2.2.2
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