korben 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/lib/korben.rb +1 -1
- data/lib/korben/k_config.rb +61 -0
- data/lib/korben/version.rb +1 -1
- data/spec/korben/k_config_spec.rb +38 -0
- metadata +5 -5
- data/lib/korben/kc_config.rb +0 -60
- data/spec/korben/kc_config_spec.rb +0 -38
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Korben
|
2
2
|
|
3
|
-
|
3
|
+
A really simple configuration tool
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,9 +18,9 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
Add configuration to initializers files
|
21
|
+
Add configuration to initializers files k_config.rb
|
22
22
|
|
23
|
-
|
23
|
+
KConfig.setup do |config|
|
24
24
|
config.const_name = "MyConf"
|
25
25
|
end
|
26
26
|
MyConf.load!( { foo: 'bar' } )
|
data/lib/korben.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require "korben/version"
|
2
|
-
require "korben/
|
2
|
+
require "korben/k_config"
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
|
3
|
+
module KConfig
|
4
|
+
# ensures the setup only gets run once
|
5
|
+
@@_ran_once = false
|
6
|
+
|
7
|
+
mattr_accessor :const_name
|
8
|
+
@@const_name = "KSettings"
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
def setup
|
13
|
+
yield self if @@_ran_once == false
|
14
|
+
@@_ran_once = true
|
15
|
+
end
|
16
|
+
|
17
|
+
def load!(settings = {})
|
18
|
+
Kernel.send(:remove_const, KConfig.const_name) if Kernel.const_defined?(KConfig.const_name)
|
19
|
+
k_wrapper = KConfig::KWrapper.new
|
20
|
+
k_wrapper.load!(settings)
|
21
|
+
Kernel.const_set(KConfig.const_name, k_wrapper)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class KWrapper
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@settings = {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def load!(settings = {})
|
32
|
+
settings.each { |key, value| set(key, value) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def set(key, value)
|
36
|
+
define_accessor(key) { value }
|
37
|
+
@settings[key] = value
|
38
|
+
end
|
39
|
+
|
40
|
+
def exist?(key)
|
41
|
+
@settings.key?(key)
|
42
|
+
end
|
43
|
+
|
44
|
+
def get(key)
|
45
|
+
@settings[key]
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def define_accessor(name, &block)
|
51
|
+
singleton_class.class_eval { define_method(name, &block) } if !respond_to?(name) || exist?(name)
|
52
|
+
end
|
53
|
+
|
54
|
+
def singleton_class
|
55
|
+
class << self
|
56
|
+
self
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/lib/korben/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe KConfig do
|
4
|
+
|
5
|
+
context "Simple Configuration" do
|
6
|
+
before { KConfig.load!( { foo: 'bar' } ) }
|
7
|
+
it { KSettings.get(:foo).should eql 'bar' }
|
8
|
+
it { KSettings.foo.should eql 'bar' }
|
9
|
+
end
|
10
|
+
|
11
|
+
context "Custom Configuration" do
|
12
|
+
it "should have the default settings constant as 'KSettings'" do
|
13
|
+
KConfig.const_name.should eql "KSettings"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be able to assign a different settings constant" do
|
17
|
+
KConfig.setup { |config| config.const_name = "AnotherSettingConstantName" }
|
18
|
+
KConfig.const_name.should == "AnotherSettingConstantName"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "KConfig::KWrapper" do
|
23
|
+
let(:wrapper) { KConfig::KWrapper.new }
|
24
|
+
before { wrapper.set(:key, 'value') }
|
25
|
+
subject { wrapper.get(:key) }
|
26
|
+
|
27
|
+
it "should have the value" do
|
28
|
+
should eql 'value'
|
29
|
+
end
|
30
|
+
|
31
|
+
context "have method" do
|
32
|
+
subject { wrapper }
|
33
|
+
its(:key) { should eql 'value' }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: korben
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -92,9 +92,9 @@ files:
|
|
92
92
|
- init.rb
|
93
93
|
- korben.gemspec
|
94
94
|
- lib/korben.rb
|
95
|
-
- lib/korben/
|
95
|
+
- lib/korben/k_config.rb
|
96
96
|
- lib/korben/version.rb
|
97
|
-
- spec/korben/
|
97
|
+
- spec/korben/k_config_spec.rb
|
98
98
|
- spec/spec_helper.rb
|
99
99
|
homepage: https://github.com/joel/korben
|
100
100
|
licenses: []
|
@@ -121,5 +121,5 @@ signing_key:
|
|
121
121
|
specification_version: 3
|
122
122
|
summary: Simple helper for Rails App configuration
|
123
123
|
test_files:
|
124
|
-
- spec/korben/
|
124
|
+
- spec/korben/k_config_spec.rb
|
125
125
|
- spec/spec_helper.rb
|
data/lib/korben/kc_config.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
-
|
3
|
-
module KcConfig
|
4
|
-
# ensures the setup only gets run once
|
5
|
-
@@_ran_once = false
|
6
|
-
|
7
|
-
mattr_accessor :const_name
|
8
|
-
@@const_name = "KcSettings"
|
9
|
-
|
10
|
-
class << self
|
11
|
-
|
12
|
-
def setup
|
13
|
-
yield self if @@_ran_once == false
|
14
|
-
@@_ran_once = true
|
15
|
-
end
|
16
|
-
|
17
|
-
def load!(settings = {})
|
18
|
-
Kernel.send(:remove_const, KcConfig.const_name) if Kernel.const_defined?(KcConfig.const_name)
|
19
|
-
kc_wrapper = KcWrapper.new
|
20
|
-
kc_wrapper.load!(settings)
|
21
|
-
Kernel.const_set(KcConfig.const_name, kc_wrapper)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class KcWrapper
|
27
|
-
|
28
|
-
def initialize
|
29
|
-
@settings = {}
|
30
|
-
end
|
31
|
-
|
32
|
-
def load!(settings = {})
|
33
|
-
settings.each { |key, value| set(key, value) }
|
34
|
-
end
|
35
|
-
|
36
|
-
def set(key, value)
|
37
|
-
define_accessor(key) { value }
|
38
|
-
@settings[key] = value
|
39
|
-
end
|
40
|
-
|
41
|
-
def exist?(key)
|
42
|
-
@settings.key?(key)
|
43
|
-
end
|
44
|
-
|
45
|
-
def get(key)
|
46
|
-
@settings[key]
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def define_accessor(name, &block)
|
52
|
-
singleton_class.class_eval { define_method(name, &block) } if !respond_to?(name) || exist?(name)
|
53
|
-
end
|
54
|
-
|
55
|
-
def singleton_class
|
56
|
-
class << self
|
57
|
-
self
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe KcConfig do
|
4
|
-
|
5
|
-
context "Simple Configuration" do
|
6
|
-
before { KcConfig.load!( { foo: 'bar' } ) }
|
7
|
-
it { KcSettings.get(:foo).should eql 'bar' }
|
8
|
-
it { KcSettings.foo.should eql 'bar' }
|
9
|
-
end
|
10
|
-
|
11
|
-
context "Custom Configuration" do
|
12
|
-
it "should have the default settings constant as 'KcSettings'" do
|
13
|
-
KcConfig.const_name.should eql "KcSettings"
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should be able to assign a different settings constant" do
|
17
|
-
KcConfig.setup { |config| config.const_name = "AnotherSettingConstantName" }
|
18
|
-
KcConfig.const_name.should == "AnotherSettingConstantName"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context "KcWrapper" do
|
23
|
-
let(:wrapper) { KcWrapper.new }
|
24
|
-
before { wrapper.set(:key, 'value') }
|
25
|
-
subject { wrapper.get(:key) }
|
26
|
-
|
27
|
-
it "should have the value" do
|
28
|
-
should eql 'value'
|
29
|
-
end
|
30
|
-
|
31
|
-
context "have method" do
|
32
|
-
subject { wrapper }
|
33
|
-
its(:key) { should eql 'value' }
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|