canfig 0.0.6 → 0.0.11

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: 19205ebc7b976f9e5d9f15a0a891699687f48c28
4
- data.tar.gz: a1832b75a3870d72dbc3c303c2a1c154bd51db34
2
+ SHA256:
3
+ metadata.gz: ba430bd04920933ac504636144cb797457e51446c1850c02ce8ba76a2fab1136
4
+ data.tar.gz: 9b38d00d36ab5928c606a5aa869cb26597276743d9433f558b079dd8487ce2ec
5
5
  SHA512:
6
- metadata.gz: ffce29c8ae20dfc4714b4a447f542cb4310f8fa12297b3eb35afc8669b5853d82df314fc7b2244ccf4c3a0877df5f389edd245345954b10f2f6c468e29552dc4
7
- data.tar.gz: 4c60c5327c9207c5811d08bd73700f5ac3cdc8ff0f817123b3935ff557b7d7c1ee42ce59955c4e6b9f9850657930fdb985568e23ecf54b60f91a109372f374d6
6
+ metadata.gz: acf50db63b8d815cb835fc8ae8213e009040aecb68f17d1c09e760ef5fe10087af3eb5c9db91507385fc62617c1bc146b6bc4113addde3e803b5c5fd072c5414
7
+ data.tar.gz: d97d1a93681dfe5c13ef6ea893eb2817cf5a6484a18b77ef40b2ea2da99fd12b2f54823c441f78d2db848e017091f0600e42b61dd20fa80e701549032890edac
@@ -1,9 +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'
4
5
  require 'canfig/yaml'
5
6
  require 'canfig/config'
6
7
  require 'canfig/open_config'
8
+ require 'canfig/env_config'
7
9
  require 'canfig/module'
8
10
  require 'canfig/class'
9
11
  require 'canfig/instance'
@@ -36,13 +36,26 @@ module Canfig
36
36
  end
37
37
  end
38
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.to_s) ? 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
+
39
52
  def []=(key, val)
40
53
  set key, val
41
54
  end
42
55
 
43
- def get(key)
56
+ def get(key, default=nil, &block)
44
57
  raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
45
- @state[key]
58
+ @state[key] || (block_given? ? yield : default)
46
59
  end
47
60
 
48
61
  def [](key)
@@ -99,7 +112,7 @@ module Canfig
99
112
  opt = meth.to_s.gsub(/=/,'').to_sym
100
113
  return set(opt, args.first) if allowed?(opt)
101
114
  else
102
- return @state[meth] if allowed?(meth)
115
+ return get(meth, *args, &block) if allowed?(meth)
103
116
  end
104
117
 
105
118
  super
@@ -107,8 +120,7 @@ module Canfig
107
120
 
108
121
  protected
109
122
 
110
- def initialize(*args, &block)
111
- options = args.extract_options!
123
+ def initialize(*args, **options, &block)
112
124
  @allowed = (args.map(&:to_sym) + options.symbolize_keys.keys).uniq
113
125
  @state = {}
114
126
  enable_state_saves!
@@ -0,0 +1,26 @@
1
+ module Canfig
2
+ class EnvConfig < OpenConfig
3
+ attr_reader :namespace
4
+
5
+ def initialize(namespace=nil, **attributes, &block)
6
+ @namespace = namespace ? "#{namespace.to_s.underscore.upcase.gsub(/_$/, '')}_" : namespace
7
+ super(*attributes.keys, &block)
8
+ attributes.each do |key,val|
9
+ env(key, val)
10
+ end
11
+ end
12
+
13
+ def get(key, default=nil, &block)
14
+ super || env(key, default, &block)
15
+ end
16
+
17
+ def env(key, default=nil, &block)
18
+ @state[key.to_sym] ||= begin
19
+ key = key.to_s.underscore.upcase
20
+ key = key.gsub(/^#{namespace}/, '') if namespace
21
+ val = ENV.fetch("#{namespace}#{key}", default, &block)
22
+ val && ENV.key?(val.to_s) ? env(val, default, &block) : val
23
+ end
24
+ end
25
+ end
26
+ 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
@@ -1,8 +1,8 @@
1
1
  module Canfig
2
2
  class OpenConfig < Config
3
3
  def set(key, val)
4
- super(key, val)
5
4
  @allowed << key unless @allowed.include?(key)
5
+ super(key, val)
6
6
  end
7
7
 
8
8
  def allowed?(opt)
@@ -1,3 +1,3 @@
1
1
  module Canfig
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.11'
3
3
  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.6
4
+ version: 0.0.11
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: 2016-03-31 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,6 +62,7 @@ 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
@@ -76,7 +77,7 @@ files:
76
77
  homepage: http://github.com/markrebec/canfig
77
78
  licenses: []
78
79
  metadata: {}
79
- post_install_message:
80
+ post_install_message:
80
81
  rdoc_options: []
81
82
  require_paths:
82
83
  - lib
@@ -91,15 +92,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
- rubyforge_project:
95
- rubygems_version: 2.4.8
96
- signing_key:
95
+ rubygems_version: 3.1.2
96
+ signing_key:
97
97
  specification_version: 4
98
98
  summary: Dead simple canned configuration for gems or whatever
99
99
  test_files:
100
+ - spec/spec_helper.rb
101
+ - spec/fixtures/config.yml
100
102
  - spec/canfig/config_spec.rb
101
103
  - spec/canfig/open_config_spec.rb
102
104
  - spec/canfig/yaml_spec.rb
103
105
  - spec/canfig_spec.rb
104
- - spec/fixtures/config.yml
105
- - spec/spec_helper.rb