complex_config 0.3.1 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c6959598393b83ee40918943044f4c4137f67ee6
4
- data.tar.gz: 88b984849cc26105ecb236cd46b7c426a5f72437
3
+ metadata.gz: ce05b6145a1404ee49c681b56299be1ea31fb1e4
4
+ data.tar.gz: 8056f3a0946139f199c27d2f72bfef27f9b7c4eb
5
5
  SHA512:
6
- metadata.gz: e4d354e51cf38457e101eea56a2015d8e16ffbd4d1423196f694e707733fe0fafdfd5b7a52a2a36004e062022b51b3f242ee66e328184184710d4cbfa753ff85
7
- data.tar.gz: c146d3ebd2914af05e521f9925aef0a99dea4d61e28b91692bf4871d3fe90219fc8f1786566a1a692fd0c86192ef618884c6a7181546e2f7c2854d2d79359a83
6
+ metadata.gz: 0e333940bdbf7bc28857ecddde5986b754cc90e388ed077faa53608fee1e9440b64890893581a906da8e0b53d4eb50459b25afeef382dc8eaef87cef5d696842
7
+ data.tar.gz: 2cc771c28cdbdb6ada774b87f63b20b8f46b94074571b808c5179dc52ce55a2fb7d8c9b057b25f7e98f047aff893404879fac80b14a835fe2dccc454eb8e82e2
data/README.md CHANGED
@@ -17,7 +17,8 @@ You can also put this line into your Gemfile
17
17
  gem 'complex_config', require: 'complex_config/rude'
18
18
 
19
19
  and bundle. This command will enable all the default plugins and make the `cc`
20
- and `complex_config` shortcuts available.
20
+ and `complex_config` shortcuts available. The configurations are expected to be
21
+ in the `config` subdirectory according to the rails convention.
21
22
 
22
23
  ## Usage
23
24
 
@@ -52,34 +53,37 @@ can now access the configuration.
52
53
 
53
54
  Fetching the name of a product:
54
55
 
55
- > cc(:products).flux_capacitor.enterprise_version.name => "Flux Capacitor Enterpise"
56
+ > cc.products.flux_capacitor.enterprise_version.name => "Flux Capacitor Enterpise"
57
+
58
+ If the name of configuration file isn't valid ruby method name syntax you can also
59
+ use `cc(:products).flux_capacitor…` to avoid this problem.
56
60
 
57
61
  Fetching the price of a product in cents:
58
62
 
59
- > cc(:products).flux_capacitor.enterprise_version.price_in_cents => 160000000
63
+ > c.products.flux_capacitor.enterprise_version.price_in_cents => 160000000
60
64
 
61
65
  Fetching the price of a product and using the ComplexConfig::Plugins::MONEY
62
66
  plugin to format it:
63
67
 
64
- > cc(:products).flux_capacitor.enterprise_version.price.format => "€1,600,000.00"
68
+ > c.products.flux_capacitor.enterprise_version.price.format => "€1,600,000.00"
65
69
 
66
70
  Fetching the URL of a product manual as a string:
67
71
 
68
- > cc(:products).flux_capacitor.enterprise_version.manual_pdf_url => "http://brown-inc.com/manuals/fc_enterprise.pdf"
72
+ > c.products.flux_capacitor.enterprise_version.manual_pdf_url => "http://brown-inc.com/manuals/fc_enterprise.pdf"
69
73
 
70
74
  Fetching the URL of a product manual and using the ComplexConfig::Plugins::URI
71
75
  plugin return an URI instance:
72
76
 
73
- > cc(:products).flux_capacitor.enterprise_version.manual_pdf_uri => #<URI::HTTP:0x007ff626d2a2e8 URL:http://brown-inc.com/manuals/fc_enterprise.pdf>
77
+ > c.products.flux_capacitor.enterprise_version.manual_pdf_uri => #<URI::HTTP:0x007ff626d2a2e8 URL:http://brown-inc.com/manuals/fc_enterprise.pdf>
74
78
 
75
79
  You can also fetch config settings from a different environment:
76
80
 
77
- >> pp cc(:products, :test); nil
78
- flux_capacitor.test_version.name = "Yadayada"
79
- flux_capacitor.test_version.price_in_cents = 666
80
- flux_capacitor.test_version.manual_pdf_url = "http://staging.brown-inc.com/manuals/fc_10.pdf"
81
+ >> pp cc.products(:test); nil
82
+ products.flux_capacitor.test_version.name = "Yadayada"
83
+ products.flux_capacitor.test_version.price_in_cents = 666
84
+ products.flux_capacitor.test_version.manual_pdf_url = "http://staging.brown-inc.com/manuals/fc_10.pdf"
81
85
 
82
- Calling `complex_config(:products)` instead of `cc(…)` would skip the implicite
86
+ Calling `complex_config.products.` instead of `cc(…)` would skip the implicite
83
87
  namespacing via the `RAILS_ENV` environment, so
84
88
  `complex_config(:products).test.flux_capacitor` returns the same settings
85
89
  object.
@@ -112,6 +116,8 @@ Here is the `ComplexConfig::Plugins::MONEY` plugin for example:
112
116
  end
113
117
 
114
118
  ## Changes
119
+ * 2015-11-17 Release 0.4.0
120
+ * Add root object for configuration, e. g. cc.name instead of cc(name).
115
121
  * 2015-11-03 Release 0.3.1
116
122
  * Add missing dependency to tins.
117
123
  * 2015-11-03 Release 0.3.0
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,22 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: complex_config 0.3.1 ruby lib
2
+ # stub: complex_config 0.4.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "complex_config"
6
- s.version = "0.3.1"
6
+ s.version = "0.4.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Florian Frank"]
11
- s.date = "2015-11-03"
11
+ s.date = "2015-11-17"
12
12
  s.description = "This library allows you to access configuration files via a simple interface"
13
13
  s.email = "flori@ping.de"
14
- s.extra_rdoc_files = ["README.md", "lib/complex_config.rb", "lib/complex_config/errors.rb", "lib/complex_config/plugins.rb", "lib/complex_config/plugins/enable.rb", "lib/complex_config/plugins/money.rb", "lib/complex_config/plugins/uri.rb", "lib/complex_config/provider.rb", "lib/complex_config/rude.rb", "lib/complex_config/settings.rb", "lib/complex_config/shortcuts.rb", "lib/complex_config/version.rb"]
15
- s.files = [".gitignore", ".rspec", ".travis.yml", ".utilsrc", "COPYING", "Gemfile", "README.md", "Rakefile", "TODO.md", "VERSION", "complex_config.gemspec", "lib/complex_config.rb", "lib/complex_config/errors.rb", "lib/complex_config/plugins.rb", "lib/complex_config/plugins/enable.rb", "lib/complex_config/plugins/money.rb", "lib/complex_config/plugins/uri.rb", "lib/complex_config/provider.rb", "lib/complex_config/rude.rb", "lib/complex_config/settings.rb", "lib/complex_config/shortcuts.rb", "lib/complex_config/version.rb", "spec/complex_config/plugins_spec.rb", "spec/complex_config/provider_spec.rb", "spec/complex_config/settings_spec.rb", "spec/complex_config/shortcuts_spec.rb", "spec/config/broken_config.yml", "spec/config/config.yml", "spec/spec_helper.rb"]
14
+ s.extra_rdoc_files = ["README.md", "lib/complex_config.rb", "lib/complex_config/errors.rb", "lib/complex_config/plugins.rb", "lib/complex_config/plugins/enable.rb", "lib/complex_config/plugins/money.rb", "lib/complex_config/plugins/uri.rb", "lib/complex_config/provider.rb", "lib/complex_config/proxy.rb", "lib/complex_config/rude.rb", "lib/complex_config/settings.rb", "lib/complex_config/shortcuts.rb", "lib/complex_config/version.rb"]
15
+ s.files = [".gitignore", ".rspec", ".travis.yml", ".utilsrc", "COPYING", "Gemfile", "README.md", "Rakefile", "TODO.md", "VERSION", "complex_config.gemspec", "config/products.yml", "lib/complex_config.rb", "lib/complex_config/errors.rb", "lib/complex_config/plugins.rb", "lib/complex_config/plugins/enable.rb", "lib/complex_config/plugins/money.rb", "lib/complex_config/plugins/uri.rb", "lib/complex_config/provider.rb", "lib/complex_config/proxy.rb", "lib/complex_config/rude.rb", "lib/complex_config/settings.rb", "lib/complex_config/shortcuts.rb", "lib/complex_config/version.rb", "spec/complex_config/plugins_spec.rb", "spec/complex_config/provider_spec.rb", "spec/complex_config/settings_spec.rb", "spec/complex_config/shortcuts_spec.rb", "spec/config/broken_config.yml", "spec/config/config.yml", "spec/spec_helper.rb"]
16
16
  s.homepage = "https://github.com/flori/complex_config"
17
17
  s.licenses = ["Apache-2.0"]
18
18
  s.rdoc_options = ["--title", "ComplexConfig -- configuration library", "--main", "README.md"]
19
- s.rubygems_version = "2.4.8"
19
+ s.rubygems_version = "2.5.0"
20
20
  s.summary = "configuration library"
21
21
  s.test_files = ["spec/complex_config/plugins_spec.rb", "spec/complex_config/provider_spec.rb", "spec/complex_config/settings_spec.rb", "spec/complex_config/shortcuts_spec.rb", "spec/spec_helper.rb"]
22
22
 
@@ -0,0 +1,21 @@
1
+ development:
2
+ flux_capacitor:
3
+ version_20:
4
+ name: Flux Capacitor Version 2.0
5
+ price_in_cents: 12_000_00
6
+ manual_pdf_url: "http://brown-inc.com/manuals/fc_20.pdf"
7
+ pro_version:
8
+ name: Flux Capacitor Professional
9
+ price_in_cents: 23_000_00
10
+ manual_pdf_url: "http://brown-inc.com/manuals/fc_pro.pdf"
11
+ enterprise_version:
12
+ name: Flux Capacitor Enterpise
13
+ price_in_cents: 1_600_000_00
14
+ manual_pdf_url: "http://brown-inc.com/manuals/fc_enterprise.pdf"
15
+
16
+ test:
17
+ flux_capacitor:
18
+ test_version:
19
+ name: Yadayada
20
+ price_in_cents: 6_66
21
+ manual_pdf_url: "http://staging.brown-inc.com/manuals/fc_10.pdf"
@@ -5,5 +5,6 @@ end
5
5
 
6
6
  require 'complex_config/version'
7
7
  require 'complex_config/errors'
8
+ require 'complex_config/proxy'
8
9
  require 'complex_config/settings'
9
10
  require 'complex_config/provider'
@@ -37,9 +37,10 @@ class ComplexConfig::Provider
37
37
  root + "config/#{name}.yml"
38
38
  end
39
39
 
40
- def config(pathname)
40
+ def config(pathname, name = nil)
41
41
  result = evaluate(pathname)
42
- ComplexConfig::Settings[::YAML.load(result, pathname)].tap do |settings|
42
+ hash = ::YAML.load(result, pathname)
43
+ ComplexConfig::Settings.build(name, hash).tap do |settings|
43
44
  deep_freeze? and settings.deep_freeze
44
45
  end
45
46
  rescue ::Errno::ENOENT => e
@@ -49,10 +50,14 @@ class ComplexConfig::Provider
49
50
  end
50
51
 
51
52
  def [](name)
52
- config pathname(name)
53
+ config pathname(name), name
53
54
  end
54
55
  memoize_method :[]
55
56
 
57
+ def proxy(env = nil)
58
+ ComplexConfig::Proxy.new(env)
59
+ end
60
+
56
61
  def flush_cache
57
62
  memoize_cache_clear
58
63
  self
@@ -0,0 +1,32 @@
1
+ module ComplexConfig
2
+ class Proxy < BasicObject
3
+ def initialize(env = nil)
4
+ @env = env
5
+ end
6
+
7
+ def to_s
8
+ 'ComplexConfig::Proxy'
9
+ end
10
+
11
+ def inspect
12
+ "#<#{to_s}>"
13
+ end
14
+
15
+ def reload
16
+ ::ComplexConfig::Provider.flush_cache
17
+ self
18
+ end
19
+
20
+ def method_missing(name, *args)
21
+ config = ::ComplexConfig::Provider[name]
22
+ env, = args
23
+ if env
24
+ config[env]
25
+ elsif @env
26
+ config[@env]
27
+ else
28
+ config
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,11 +1,32 @@
1
1
  require 'json'
2
2
  require 'tins/xt/ask_and_send'
3
+ require 'tins/thread_local'
3
4
 
4
5
  class ComplexConfig::Settings < JSON::GenericObject
5
6
  def self.[](*a)
6
7
  from_hash *a
7
8
  end
8
9
 
10
+ class << self
11
+ extend Tins::ThreadLocal
12
+
13
+ thread_local :name_prefix
14
+ end
15
+
16
+ attr_accessor :name_prefix
17
+
18
+ def self.build(name, hash)
19
+ name.nil? or self.name_prefix = name.to_sym
20
+ self[hash]
21
+ ensure
22
+ self.name_prefix = nil
23
+ end
24
+
25
+ def initialize(*)
26
+ self.name_prefix = self.class.name_prefix
27
+ super
28
+ end
29
+
9
30
  def attribute_set?(name)
10
31
  table.key?(name.to_sym)
11
32
  end
@@ -37,7 +58,7 @@ class ComplexConfig::Settings < JSON::GenericObject
37
58
  end
38
59
  end
39
60
 
40
- def pathes(hash = table, path_sep: ?., prefix: '', result: {})
61
+ def pathes(hash = table, path_sep: ?., prefix: name_prefix.to_s, result: {})
41
62
  hash.each do |key, value|
42
63
  path = prefix.empty? ? key.to_s : "#{prefix}#{path_sep}#{key}"
43
64
  case value
@@ -1,10 +1,18 @@
1
1
  require 'complex_config'
2
2
 
3
- def complex_config_with_env(name, env = ComplexConfig::Provider.env)
4
- ComplexConfig::Provider[name][env.to_s]
3
+ def complex_config_with_env(name = nil, env = ComplexConfig::Provider.env)
4
+ if name
5
+ ComplexConfig::Provider[name][env.to_s]
6
+ else
7
+ ComplexConfig::Provider.proxy(env.to_s)
8
+ end
5
9
  end
6
10
  alias cc complex_config_with_env
7
11
 
8
- def complex_config(name)
9
- ComplexConfig::Provider[name]
12
+ def complex_config(name = nil)
13
+ if name
14
+ ComplexConfig::Provider[name]
15
+ else
16
+ ComplexConfig::Provider.proxy
17
+ end
10
18
  end
@@ -1,6 +1,6 @@
1
1
  module ComplexConfig
2
2
  # ComplexConfig version
3
- VERSION = '0.3.1'
3
+ VERSION = '0.4.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -107,8 +107,8 @@ RSpec.describe ComplexConfig::Provider do
107
107
  }.to change {
108
108
  provider.instance.__send__(:__memoize_cache__).size
109
109
  }.by(1)
110
- expect(provider['config']).to be_a ComplexConfig::Settings
111
110
  expect(provider).not_to receive(:config)
111
+ expect(provider['config']).to be_a ComplexConfig::Settings
112
112
  end
113
113
 
114
114
  it 'can flush loaded configurations' do
@@ -10,35 +10,56 @@ RSpec.describe 'shortcuts' do
10
10
  provider.root = Pathname.new(__FILE__).dirname.dirname
11
11
  end
12
12
 
13
+ it 'returns a proxy object for the shortcuts' do
14
+ expect(complex_config_with_env.to_s).to eq 'ComplexConfig::Proxy'
15
+ expect(cc.inspect).to eq '#<ComplexConfig::Proxy>'
16
+ expect(complex_config.to_s).to eq 'ComplexConfig::Proxy'
17
+ expect(cc.config.name_prefix).to eq :config
18
+ end
19
+
20
+ it 'can be reloaded via shortcut' do
21
+ expect(ComplexConfig::Provider).to receive(:flush_cache)
22
+ complex_config.reload
23
+ end
24
+
13
25
  it 'has the complex_config_with_env "shortcut"' do
14
- settings = complex_config_with_env('config', 'development')
26
+ settings = complex_config_with_env.config('development')
15
27
  expect(settings).to be_a ComplexConfig::Settings
16
28
  expect(settings.config.baz).to eq 'something'
17
- settings = complex_config_with_env('config', 'test')
29
+ settings = complex_config_with_env.config('test')
18
30
  expect(settings).to be_a ComplexConfig::Settings
19
31
  expect(settings.config.baz).to eq 'something else'
20
32
  end
21
33
 
22
34
  it 'has the alias cc for complex_config_with_env' do
23
- settings = cc(:config, :development)
35
+ settings = cc.config(:development)
24
36
  expect(settings).to be_a ComplexConfig::Settings
25
37
  expect(settings.config.baz).to eq 'something'
26
38
  end
27
39
 
28
40
  it 'considers the environment for complex_config_with_env' do
29
- settings = complex_config_with_env('config')
41
+ settings = complex_config_with_env.config
30
42
  expect(settings).to be_a ComplexConfig::Settings
31
43
  expect(settings.config.baz).to eq 'something'
32
44
  allow(provider).to receive(:env).and_return('test')
33
- settings = complex_config_with_env('config')
45
+ settings = complex_config_with_env.config
34
46
  expect(settings).to be_a ComplexConfig::Settings
35
47
  expect(settings.config.baz).to eq 'something else'
36
48
  end
37
49
 
38
50
  it 'has the complex_config shortcut' do
39
- settings = complex_config(:config)
51
+ settings = complex_config.config
40
52
  expect(settings).to be_a ComplexConfig::Settings
41
53
  expect(settings.development.config.baz).to eq 'something'
42
54
  expect(settings.test.config.baz).to eq 'something else'
43
55
  end
56
+
57
+ it 'has alternatives instead of method syntax' do
58
+ settings = complex_config_with_env(:config, :development)
59
+ expect(settings).to be_a ComplexConfig::Settings
60
+ settings = cc(:config)
61
+ expect(settings).to be_a ComplexConfig::Settings
62
+ settings = complex_config(:config)
63
+ expect(settings).to be_a ComplexConfig::Settings
64
+ end
44
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: complex_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-03 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar
@@ -135,6 +135,7 @@ extra_rdoc_files:
135
135
  - lib/complex_config/plugins/money.rb
136
136
  - lib/complex_config/plugins/uri.rb
137
137
  - lib/complex_config/provider.rb
138
+ - lib/complex_config/proxy.rb
138
139
  - lib/complex_config/rude.rb
139
140
  - lib/complex_config/settings.rb
140
141
  - lib/complex_config/shortcuts.rb
@@ -151,6 +152,7 @@ files:
151
152
  - TODO.md
152
153
  - VERSION
153
154
  - complex_config.gemspec
155
+ - config/products.yml
154
156
  - lib/complex_config.rb
155
157
  - lib/complex_config/errors.rb
156
158
  - lib/complex_config/plugins.rb
@@ -158,6 +160,7 @@ files:
158
160
  - lib/complex_config/plugins/money.rb
159
161
  - lib/complex_config/plugins/uri.rb
160
162
  - lib/complex_config/provider.rb
163
+ - lib/complex_config/proxy.rb
161
164
  - lib/complex_config/rude.rb
162
165
  - lib/complex_config/settings.rb
163
166
  - lib/complex_config/shortcuts.rb
@@ -193,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
196
  version: '0'
194
197
  requirements: []
195
198
  rubyforge_project:
196
- rubygems_version: 2.4.8
199
+ rubygems_version: 2.5.0
197
200
  signing_key:
198
201
  specification_version: 4
199
202
  summary: configuration library