qonf 0.0.2 → 0.0.3

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
2
  SHA1:
3
- metadata.gz: 8e1ef8c9bbf6a8d5f9cd0f71c5253a44424970dc
4
- data.tar.gz: 43b459ff2cb58753fe21720f1c1fe3353616d7cd
3
+ metadata.gz: 12132d8454c98e3c2a1424e86cbb6faebb0f51e2
4
+ data.tar.gz: eda1d01e2b676e61d9438492e11b819df425d961
5
5
  SHA512:
6
- metadata.gz: dbc596ae062ef2df9de3fb17a15e9840e9047e54b9c84a9568326f660674cfee5cb6d17d48e83978ba8a87048420a1a41ca1acafc851e1f27fdf82d6f302477f
7
- data.tar.gz: 4bf7a0488b3c608021176ef5a3004bac2dc14c08b20c259202e137a36ad91bdc253c8bf0424f06c173154854c134528aacb2fd3d5b885009ce5a0b52c786ed6a
6
+ metadata.gz: e401058d8f0a05b2a3ad34b75639d1599673233fc9db17a4e9f367531623c1573beed6bd8dd08f9d283ef6fd7463d37ab4c8018273ba151afb082036df278ba2
7
+ data.tar.gz: 13104477004369318b7016ee489c85d83e8df055fc28eb9f28d42deecc396ca782ef78225aad1c01f78d15efb3e116d76d2b364e757017ea817dfb5676e9bf2b
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/README.md CHANGED
@@ -2,23 +2,53 @@
2
2
 
3
3
  Versitle configuration management. Pull data from ENV or config files. Simplest use case:
4
4
 
5
- config/qonf.json
6
- ```
7
- {
8
- "host": "http://google.com"
9
- }
10
- ```
5
+ ## Usage
6
+
7
+ Qonf works well with or without Rails. If using Rails, Qonf will assume config files are in `Rails.root/config` directory. Otherwise, you'll need to set the root yourself:
8
+
9
+ Qonf.configure do
10
+ self.base_dir = "./config"
11
+ end
12
+
13
+ Qonf also allows you to use environment based keys, these will come preconfigured for Rails or could be set by other ruby applications:
14
+
15
+ Qonf.configure do
16
+ self.env = "staging" # hash under this key will be merged into top-level hash
17
+ self.environments = %w{test staging production} # these will be removed from hash if they are top-level keys
18
+ end
19
+
20
+ So, if you config is as follows:
21
+
22
+ # config/names.json
23
+ {
24
+ "test": {
25
+ "name": "Test data"
26
+ },
27
+ "staging": {
28
+ "name": "Bob Jones"
29
+ }
30
+ }
11
31
 
12
- Qonf.host # "http://google.com"
32
+ Qonf.get(:names,:name) # Bob Jones
13
33
 
14
- Additionally, you could make a file config/redis.yml
34
+ ## Config Files
15
35
 
16
- ```
17
- development:
18
- host: localhost
19
- ```
36
+ Config files can be either json or yml. They are addressed by name and must live in the `base_dir` path (for Rails, this is `Rails.root/config`)
20
37
 
21
- Qonf.get(:redis,:host) # "localhost"
38
+ # config/qonf.json
39
+ {
40
+ "host": "http://google.com"
41
+ }
42
+
43
+ Qonf.host # "http://google.com"
44
+
45
+ YAML can also be used:
46
+
47
+ # config/redis.yml
48
+ development:
49
+ host: localhost
50
+
51
+ Qonf.get(:redis,:host) # "localhost"
22
52
 
23
53
  ## Installation
24
54
 
@@ -34,9 +64,11 @@ Or install it yourself as:
34
64
 
35
65
  $ gem install qonf
36
66
 
37
- ## Usage
67
+ ## Testing
68
+
69
+ Tests are maintained by RSpec. To run test cases:
38
70
 
39
- TODO: Write usage instructions here
71
+ bundle exec rake test
40
72
 
41
73
  ## Contributing
42
74
 
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "rspec/core/rake_task"
1
3
  require "bundler/gem_tasks"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task default: :spec
8
+ task test: :spec
@@ -0,0 +1,8 @@
1
+ {
2
+ "test": {
3
+ "name": "Matt"
4
+ },
5
+ "production": {
6
+ "name": "Steve"
7
+ }
8
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "name": "Bob Jones"
3
+ }
data/lib/qonf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Qonf
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/qonf.rb CHANGED
@@ -3,7 +3,33 @@ require "qonf/version"
3
3
  # A simple module to get information quickly out of config files
4
4
  module Qonf
5
5
  @@cache = {} # note, we're caching the config in memory
6
- @@environments = Dir.glob("./config/environments/*.rb").map { |filename| File.basename(filename, ".rb") } # for extraction
6
+
7
+ module Config
8
+ @@environments = []
9
+ @@base_dir = nil
10
+ @@env = nil
11
+ @@use_cache = true
12
+
13
+ [:environments,:env,:base_dir,:use_cache].each do |var|
14
+ self.class_eval("def self.#{var}=(value); @@#{var} = value; end")
15
+ self.class_eval("def self.#{var}; @@#{var}; end")
16
+ end
17
+
18
+ def self.load_defaults
19
+ if defined?(Rails) # Rails defaults
20
+ self.environments = Dir.glob("#{Rails.root}/config/environments/*.rb").map { |filename| File.basename(filename, ".rb") } # for extraction
21
+ self.base_dir = "#{Rails.root}/config"
22
+ self.env = Rails.env
23
+ self.use_cache = Rails.env != "development"
24
+ end
25
+ end
26
+
27
+ load_defaults # load defaults by default
28
+ end
29
+
30
+ def self.configure(&block)
31
+ Qonf::Config.instance_eval(&block)
32
+ end
7
33
 
8
34
  # Qonf.name -> Qonf.get(:qonf, [:name]) for quick short-hand
9
35
  def self.method_missing(method)
@@ -26,23 +52,31 @@ module Qonf
26
52
  raise "Invalid config" unless config =~ /^\w+$/
27
53
  config = config.to_sym
28
54
 
29
- if !Rails.env.development? && @@cache[config.to_sym] # don't cache in development
55
+ # Do we just use cached version?
56
+ if !Qonf::Config.use_cache || !@@cache.has_key?(config.to_sym)
57
+ base_path = "#{Qonf::Config.base_dir}/#{config}"
30
58
 
31
- else
32
- base_path = "#{Rails.root}/config/#{config}"
33
-
34
- formats = {
35
- yml: ->(f){ YAML.load(f) },
36
- json: ->(f){ JSON(f.read) }
37
- }
59
+ formats={}
60
+ formats[:yml] = ->(f){ YAML.load(f) } if defined?(YAML)
61
+ formats[:json] = ->(f){ JSON(f.read) } if defined?(JSON)
62
+ raise "Must include JSON or YAML parser" if formats.keys.count == 0
38
63
 
39
64
  formats.each do |ext,parser|
40
65
  if File.exists?("#{base_path}.#{ext}")
41
66
  cache = parser.call(File.open("#{base_path}.#{ext}"))
42
67
  raise "Invalid Qonf; must be hash" unless cache.is_a?(Hash)
43
- cache = cache.deep_symbolize_keys! # Let's do this as symbols
44
- cache.deep_merge!(cache.delete(Rails.env.to_sym)) if cache.has_key?(Rails.env.to_sym) # Now merge anything under current env
45
- @@environments.each { |env| cache.delete(env.to_sym) } # And remove any other env keys
68
+ cache = symbolize_keys(cache) # Let's do this as symbols
69
+
70
+ # Do we use env key? (e.g. "development")
71
+ if Qonf::Config.env
72
+ # TODO: This should be a deep merge?
73
+ cache.merge!(cache.delete(Qonf::Config.env.to_sym)) if cache.has_key?(Qonf::Config.env.to_sym) # Now merge anything under current env
74
+ end
75
+
76
+ # Remove any other envs (e.g. "staging")
77
+ Qonf::Config.environments.each do |environment|
78
+ cache.delete(environment.to_sym)
79
+ end
46
80
 
47
81
  @@cache[config.to_sym] = cache # Store
48
82
 
@@ -50,10 +84,10 @@ module Qonf
50
84
  end
51
85
  end
52
86
 
53
- raise "Unable to find config for #{config} in #{Rails.root}/config/*.{#{formats.keys.join(',')}}" if @@cache[config.to_sym].nil?
87
+ raise "Unable to find config for #{config} in #{base_path}/*.{#{formats.keys.join(',')}}" if @@cache[config.to_sym].nil?
54
88
  end
55
89
 
56
- return get_route(@@cache[config.to_sym], route)
90
+ return get_route(@@cache[config.to_sym], route)
57
91
  end
58
92
 
59
93
  def self.reset!
@@ -68,4 +102,19 @@ module Qonf
68
102
  # TODO: Do we want to accept _ for -?
69
103
  return Qonf.get_route(hash[route.shift.to_sym], route) # Recursive, yo!
70
104
  end
105
+
106
+ def self.symbolize_keys(hash)
107
+ hash.inject({}) do |result, (key, value)|
108
+ new_key = case key
109
+ when String then key.to_sym
110
+ else key
111
+ end
112
+ new_value = case value
113
+ when Hash then symbolize_keys(value)
114
+ else value
115
+ end
116
+ result[new_key] = new_value
117
+ result
118
+ end
119
+ end
71
120
  end
data/qonf.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  end
data/spec/qonf_spec.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe 'qonf examples' do
5
+
6
+ it 'should configure correctly' do
7
+ Qonf.configure do
8
+ self.base_dir = "."
9
+ end
10
+
11
+ expect(Qonf::Config.base_dir).to eq(".")
12
+ expect(Qonf::Config.environments).to eq([])
13
+ end
14
+
15
+ it 'should use Rails config if available' do
16
+ begin
17
+ Rails = double("rails", root: 'root', env: 'staging')
18
+ Qonf::Config.load_defaults
19
+
20
+ expect(Qonf::Config.base_dir).to eq("root/config")
21
+ expect(Qonf::Config.environments).to eq([]) # TODO: Test "environments"
22
+ expect(Qonf::Config.env).to eq("staging")
23
+ expect(Qonf::Config.use_cache).to eq(true)
24
+ ensure
25
+ Object.send(:remove_const, 'Rails')
26
+ end
27
+ end
28
+
29
+ it 'should read from correct file path' do
30
+ Qonf.configure do
31
+ self.base_dir = "./examples"
32
+ end
33
+
34
+ expect(Qonf.get(:simple, :name)).to eq("Bob Jones")
35
+ end
36
+
37
+ it 'should work with environments' do
38
+ Qonf.configure do
39
+ self.base_dir = "./examples"
40
+ self.environments = ['test','production']
41
+ self.env = 'production'
42
+ end
43
+
44
+ expect(Qonf.get(:environed, :name)).to eq("Steve")
45
+ end
46
+
47
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'qonf'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qonf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Hayes
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Qonf is a simple configuration management tool
42
56
  email:
43
57
  - hayesgm@gmail.com
@@ -46,13 +60,18 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - .gitignore
63
+ - .rspec
49
64
  - Gemfile
50
65
  - LICENSE.txt
51
66
  - README.md
52
67
  - Rakefile
68
+ - examples/environed.json
69
+ - examples/simple.json
53
70
  - lib/qonf.rb
54
71
  - lib/qonf/version.rb
55
72
  - qonf.gemspec
73
+ - spec/qonf_spec.rb
74
+ - spec/spec_helper.rb
56
75
  homepage: ''
57
76
  licenses:
58
77
  - MIT
@@ -78,4 +97,6 @@ signing_key:
78
97
  specification_version: 4
79
98
  summary: Simplest use case, add config/qonf.json or config/qonf.yaml and call Qonf.key
80
99
  to pull key from config file. And much more.
81
- test_files: []
100
+ test_files:
101
+ - spec/qonf_spec.rb
102
+ - spec/spec_helper.rb