kb-config 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -0
- data/config.gemspec +23 -0
- data/lib/config/base.rb +25 -0
- data/lib/config/hash_extension.rb +20 -0
- data/lib/config/nil_extension.rb +6 -0
- data/lib/config/parser.rb +39 -0
- data/lib/config/version.rb +3 -0
- data/lib/config.rb +5 -0
- data/settings.conf +21 -0
- metadata +65 -0
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Usage
|
2
|
+
|
3
|
+
#### Open command prompt
|
4
|
+
|
5
|
+
$> gem install kb-config
|
6
|
+
$> irb
|
7
|
+
|
8
|
+
#### From irb
|
9
|
+
|
10
|
+
$> require 'config'
|
11
|
+
$> CONFIG = Config::Base.load_config(<path to settings file>, <array of overrides>)
|
12
|
+
|
13
|
+
##### Example calls and expected results
|
14
|
+
|
15
|
+
$> CONFIG.common.paid_users_size_limit
|
16
|
+
# returns 2147483648
|
17
|
+
|
18
|
+
$> CONFIG.ftp.name
|
19
|
+
# returns “hello there, ftp uploading”
|
20
|
+
|
21
|
+
$> CONFIG.http.params
|
22
|
+
# returns [“array”, “of”, “values”]
|
23
|
+
|
24
|
+
$> CONFIG.ftp.lastname
|
25
|
+
# returns nil
|
26
|
+
|
27
|
+
$> CONFIG.ftp.enabled
|
28
|
+
# returns false
|
29
|
+
|
30
|
+
$> CONFIG.ftp[:path]
|
31
|
+
# returns “/etc/var/uploads”
|
32
|
+
|
33
|
+
$> CONFIG.ftp
|
34
|
+
# returns {:name => “http uploading”, :path => “/etc/var/uploads”, :enabled => false}
|
data/config.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
|
5
|
+
require 'config/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'kb-config'
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.version = Config::VERSION
|
11
|
+
s.authors = ['Kyle Bolton']
|
12
|
+
s.email = ['kyle.bolton@gmail.com']
|
13
|
+
s.homepage = 'http://github.com/kb/config'
|
14
|
+
s.description = "Configure things. Like a boss."
|
15
|
+
s.summary = 'Library to hold configuration items for your application'
|
16
|
+
s.date = Date.today.to_s
|
17
|
+
|
18
|
+
s.rubygems_version = '>= 1.3.7'
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.require_paths = ['lib']
|
23
|
+
end
|
data/lib/config/base.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Config
|
2
|
+
class Base
|
3
|
+
def self.load_config(file_path, overrides=[])
|
4
|
+
this = self.new(file_path, overrides)
|
5
|
+
this.add_getters
|
6
|
+
this
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(file_path, overrides)
|
10
|
+
@config_hash = Config::Parser.settings(file_path, overrides)
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_getters
|
14
|
+
@config_hash.keys.each do |key|
|
15
|
+
self.class.instance_eval do
|
16
|
+
define_method(key, lambda { @config_hash[key] })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(meth, *args)
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Hash
|
2
|
+
def method_missing(method, *args)
|
3
|
+
self[method.to_s] || self[method] || nil
|
4
|
+
end
|
5
|
+
|
6
|
+
def respond_to?(sym)
|
7
|
+
if self[sym.to_s] || self[sym]
|
8
|
+
true
|
9
|
+
else
|
10
|
+
super(sym)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def symbolize_keys!
|
15
|
+
keys.each do |key|
|
16
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
17
|
+
end
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Config
|
2
|
+
module Parser
|
3
|
+
class << self
|
4
|
+
def settings(file_path, overrides)
|
5
|
+
config_hash = { }
|
6
|
+
overrides = overrides.map { |o| o.to_sym }
|
7
|
+
begin
|
8
|
+
File.open(file_path).each do |line|
|
9
|
+
group = line.match(/^\[(\S+)\]/)
|
10
|
+
kv = line.match(/(\w+)\s*=\s*["|']*([^"'\r\n]*)["|']*/)
|
11
|
+
override = line.match(/(\w+)<(\w+)>\s*=\s*["|']*([^"'\r\n]*)["|']*/)
|
12
|
+
|
13
|
+
# TODO: Should do a check for nils on matches and throw exception = malformed
|
14
|
+
if !group.nil?
|
15
|
+
config_hash[group[1]] = {}
|
16
|
+
elsif !kv.nil?
|
17
|
+
config_hash[config_hash.keys.last][kv[1]] = convert(kv[2])
|
18
|
+
elsif !override.nil?
|
19
|
+
if overrides.include? override[2].to_sym
|
20
|
+
config_hash[config_hash.keys.last][override[1].to_sym] = convert(override[3])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
config_hash[config_hash.keys.last].symbolize_keys!
|
24
|
+
end
|
25
|
+
config_hash.symbolize_keys!
|
26
|
+
rescue Exception => e
|
27
|
+
puts "Caught exception: #{e}, unable to load configuration file"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert(value)
|
32
|
+
return value.split(",") unless value.match(/(,[^\s])/).nil?
|
33
|
+
return true unless value.match(/^yes$|^true$|^1{1}[^\w]/).nil?
|
34
|
+
return false unless value.match(/^no$|^false$|^0{1}[^\w]/).nil?
|
35
|
+
return value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/config.rb
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'config', 'base')
|
2
|
+
require File.join(File.dirname(__FILE__), 'config', 'parser')
|
3
|
+
require File.join(File.dirname(__FILE__), 'config', 'hash_extension')
|
4
|
+
require File.join(File.dirname(__FILE__), 'config', 'nil_extension')
|
5
|
+
require File.join(File.dirname(__FILE__), 'config', 'version')
|
data/settings.conf
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
[common]
|
2
|
+
basic_size_limit = 26214400
|
3
|
+
student_size_limit = 52428800
|
4
|
+
paid_users_size_limit = 2147483648
|
5
|
+
path = /srv/var/tmp/
|
6
|
+
path<itscript> = /srv/tmp/
|
7
|
+
|
8
|
+
[ftp]
|
9
|
+
name = "hello there, ftp uploading"
|
10
|
+
path<production> = /srv/var/tmp/
|
11
|
+
path<staging> = /srv/uploads/
|
12
|
+
path<ubuntu> = /etc/var/uploads
|
13
|
+
enabled = no
|
14
|
+
|
15
|
+
;This is a comment
|
16
|
+
[http]
|
17
|
+
name = "http uploading"
|
18
|
+
path = /tmp/
|
19
|
+
path<production> = /srv/var/tmp/
|
20
|
+
path<staging> = /srv/uploads/; This is another comment
|
21
|
+
params = array,of,value
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kb-config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kyle Bolton
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-09-16 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Configure things. Like a boss.
|
18
|
+
email:
|
19
|
+
- kyle.bolton@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- README.md
|
28
|
+
- config.gemspec
|
29
|
+
- lib/config.rb
|
30
|
+
- lib/config/base.rb
|
31
|
+
- lib/config/hash_extension.rb
|
32
|
+
- lib/config/nil_extension.rb
|
33
|
+
- lib/config/parser.rb
|
34
|
+
- lib/config/version.rb
|
35
|
+
- settings.conf
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/kb/config
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.6.2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Library to hold configuration items for your application
|
64
|
+
test_files: []
|
65
|
+
|