confuse 0.1.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+
3
+ module Confuse
4
+ module Source
5
+ class << self
6
+ def types
7
+ @types ||= { }
8
+ end
9
+
10
+ def create(options = {})
11
+ path = options[:path]
12
+
13
+ type = if path
14
+ path[path.rindex('.') + 1, path.length].to_sym
15
+ end
16
+ type ||= options[:type]
17
+
18
+ if type
19
+ types[type].new(options)
20
+ else
21
+ Env.new(options)
22
+ end
23
+ end
24
+
25
+ def register(type, klass)
26
+ types[type] = klass
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ require 'confuse/source/ini'
33
+ require 'confuse/source/yaml'
34
+ require 'confuse/source/env'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ module Confuse
4
+ module Source
5
+ class Env
6
+ def initialize(options = {})
7
+ @prefix = options[:prefix]
8
+ end
9
+
10
+ def [](namespace, key)
11
+ lookup = key
12
+ lookup = prepend(namespace, lookup) if namespace
13
+ lookup = prepend(@prefix, lookup) if @prefix
14
+
15
+ ENV[lookup.upcase]
16
+ end
17
+
18
+ private
19
+
20
+ def prepend(pref, key)
21
+ "#{pref}_#{key}" if pref
22
+ end
23
+ end
24
+
25
+ register(:env, Env)
26
+ end
27
+ end
28
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+
3
+ require 'inifile'
4
+
5
+ module Confuse
6
+ module Source
7
+ class Ini
8
+ def initialize(options = {})
9
+ @ini = from_file(options[:path])
10
+ end
11
+
12
+ def from_file(file)
13
+ IniFile.load(file).to_h
14
+ rescue IniFile::Error
15
+ nil
16
+ end
17
+
18
+ def [](namespace, key)
19
+ namespace ||= :global
20
+ @ini[namespace.to_s][key.to_s]
21
+ end
22
+ end
23
+
24
+ register(:ini, Ini)
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+
3
+ require 'yaml'
4
+
5
+ module Confuse
6
+ module Source
7
+ class Yaml
8
+ def initialize(options = {})
9
+ @yaml = ::YAML.load_file(options[:path])
10
+ end
11
+
12
+ def [](namespace, key)
13
+ namespace ? @yaml[namespace][key] : @yaml[key]
14
+ end
15
+ end
16
+
17
+ register(:yml, Yaml)
18
+ register(:yaml, Yaml)
19
+ end
20
+ end
@@ -1,3 +1,6 @@
1
+ # coding: utf-8
2
+
3
+ # Confuse version
1
4
  module Confuse
2
- VERSION = '0.1.8'
5
+ VERSION = '1.0.0'
3
6
  end
@@ -1,62 +1,45 @@
1
+ # coding: utf-8
2
+
1
3
  require 'minitest/autorun'
2
- require 'confuse'
3
4
 
4
- # Test instance methods
5
5
  class TestConfig < MiniTest::Unit::TestCase
6
- def setup
7
- namespace = Confuse::Namespace.new do
8
- define :baz do
9
- default 1
10
- end
6
+ def definition
7
+ @definition ||= Confuse.define do |conf|
8
+ conf.add_item :foo
9
+ conf.add_item :bar, :default => 'default'
10
+ conf.add_item :baz, :required => true
11
11
  end
12
- @config = Confuse::Config
13
- @config.load_namespaces({ :foo_bar => namespace })
14
12
  end
15
13
 
16
- def test_find_namespace
17
- assert_equal :foo_bar, @config.find_namespace(:foo_bar_baz)
14
+ def source
15
+ @source ||= Class.new do
16
+ def [](namespace, key)
17
+ 'foo' if key == :foo
18
+ end
19
+ end.new
18
20
  end
19
21
 
20
- def test_fine_namespace_no_sub_key
21
- assert_equal :foo_bar, @config.find_namespace(:foo_bar)
22
+ def setup
23
+ @config = Confuse::Config.new(definition, source)
22
24
  end
23
25
 
24
- def test_find_namespace_doesnt_exist
25
- assert_equal nil, @config.find_namespace(:bar)
26
+ # gets a value from the source if it is defined, and in the source
27
+ def test_get_value_from_source
28
+ assert_equal 'foo', @config.foo
26
29
  end
27
30
 
28
- def test_rest_of_key
29
- assert_equal :bar, @config.rest_of_key(:foo_bar, :foo)
31
+ # returns the default value if the source returns nil
32
+ def test_get_default_from_definition
33
+ assert_equal 'default', @config.bar
30
34
  end
31
35
 
32
- def test_rest_of_key_default_namespace
33
- assert_equal :bar, @config.rest_of_key(:bar, :default)
36
+ # raises undefined if an item hasn't been set, and has no default
37
+ def test_get_default_from_definition
38
+ assert_nil @config.buz
34
39
  end
35
40
 
36
- def test_load_namespaces
37
- namespace = Confuse::Namespace.new do
38
- define :foo do
39
- default 1
40
- end
41
- end
42
-
43
- @config.load_namespaces({ :foo_bar => namespace })
44
-
45
- assert @config[:foo_bar][:foo]
46
- assert @config[:foo_bar][:baz]
41
+ # raises an error if any items are required and don't have defaults
42
+ def test_check
43
+ assert_raises(Confuse::Errors::Undefined) { @config.check }
47
44
  end
48
-
49
- def test_to_hash
50
- config = Class.new(Confuse::ConfigBase) do
51
- define :foo do
52
- description 'Foo'
53
- type :integer
54
- default 1
55
- end
56
- end
57
- assert_nil config.new.to_hash[:default_foo]
58
- refute_nil config.new.to_hash[:foo]
59
- end
60
-
61
45
  end
62
-
@@ -0,0 +1,13 @@
1
+ # coding: utf-8
2
+
3
+ require 'minitest/autorun'
4
+
5
+ class TestConfuse < MiniTest::Unit::TestCase
6
+ def create_definition
7
+ definition = Confuse.define do |d|
8
+ d.add_item :foo, :default => 1, :description => 'Foo'
9
+ end
10
+
11
+ assert definition.instance_of Confuse::Definition
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+
3
+ module Confuse
4
+ class TestDefinition < MiniTest::Unit::TestCase
5
+ def setup
6
+ @definition = Confuse::Definition.new do |d|
7
+ d.add_item :foo, :default => 1
8
+ d.add_namespace :bar do |n|
9
+ n.add_item(:foo, :default => 1, :description => 'test')
10
+ end
11
+ end
12
+ end
13
+
14
+ # can define a configuration item
15
+ def test_define_item
16
+ assert @definition.defines? :foo
17
+ end
18
+
19
+ # can define a namespace
20
+ def test_define_namespace
21
+ assert @definition.defines? :bar_foo
22
+ end
23
+
24
+ def test_default
25
+ assert_equal 1, @definition.default(nil, :foo)
26
+ end
27
+
28
+ def test_default_for_namespaced_item
29
+ assert_equal 1, @definition.default(:bar, :foo)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ class TestItem < MiniTest::Unit::TestCase
4
+ def setup
5
+ @item = Confuse::Item.new(:foo, :default => 1, :description => 'Description')
6
+ end
7
+
8
+ def test_sets_the_default_value
9
+ assert_equal 1, @item.default
10
+ end
11
+
12
+ def test_sets_the_description
13
+ assert_equal 'Description', @item.description
14
+ end
15
+
16
+ def test_required_item
17
+ item = Confuse::Item.new(:foo, :description => 'required!',
18
+ :required => true)
19
+ assert_raises(Confuse::Errors::Undefined) { item.default }
20
+ end
21
+ end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+
3
+ require 'minitest/autorun'
4
+
5
+ class TestKeySplitter < MiniTest::Unit::TestCase
6
+ def test_key_with_no_underscore
7
+ key_splitter = Confuse::KeySplitter.new(:foo)
8
+ assert_equal 0, key_splitter.possible_namespaces.count
9
+ end
10
+
11
+ def test_key_with_one_underscore
12
+ key_splitter = Confuse::KeySplitter.new(:foo_bar)
13
+ assert_equal 1, key_splitter.possible_namespaces.count
14
+ assert key_splitter.possible_namespaces.include? :foo
15
+ end
16
+
17
+ def test_key_with_many_underscores
18
+ key_splitter = Confuse::KeySplitter.new(:foo_1_2_3_4_5_6_7_8_9)
19
+ assert_equal 9, key_splitter.possible_namespaces.count
20
+ assert key_splitter.possible_namespaces.include? :foo
21
+ assert key_splitter.possible_namespaces.include? :foo_1
22
+ assert key_splitter.possible_namespaces.include? :foo_1_2
23
+ assert key_splitter.possible_namespaces.include? :foo_1_2_3
24
+ assert key_splitter.possible_namespaces.include? :foo_1_2_3_4
25
+ assert key_splitter.possible_namespaces.include? :foo_1_2_3_4_5
26
+ assert key_splitter.possible_namespaces.include? :foo_1_2_3_4_5_6
27
+ assert key_splitter.possible_namespaces.include? :foo_1_2_3_4_5_6_7
28
+ assert key_splitter.possible_namespaces.include? :foo_1_2_3_4_5_6_7_8
29
+ end
30
+
31
+ def test_rest_of_key
32
+ key_splitter = Confuse::KeySplitter.new(:foo_bar)
33
+ assert_equal :bar, key_splitter.rest_of_key(:foo)
34
+ end
35
+
36
+ def test_rest_of_key_return_nil_if_namespace_doesnt_match
37
+ key_splitter = Confuse::KeySplitter.new(:foo_bar)
38
+ assert_nil key_splitter.rest_of_key(:baz)
39
+ end
40
+
41
+ def test_split
42
+ key_splitter = Confuse::KeySplitter.new(:foo_1_2_3_4_5_6_7_8_9)
43
+ assert key_splitter.split.include? [:foo, :"1_2_3_4_5_6_7_8_9"]
44
+ assert key_splitter.split.include? [:foo_1, :"2_3_4_5_6_7_8_9"]
45
+ assert key_splitter.split.include? [:foo_1_2, :"3_4_5_6_7_8_9"]
46
+ assert key_splitter.split.include? [:foo_1_2_3, :"4_5_6_7_8_9"]
47
+ assert key_splitter.split.include? [:foo_1_2_3_4, :"5_6_7_8_9"]
48
+ assert key_splitter.split.include? [:foo_1_2_3_4_5, :"6_7_8_9"]
49
+ assert key_splitter.split.include? [:foo_1_2_3_4_5_6, :"7_8_9"]
50
+ assert key_splitter.split.include? [:foo_1_2_3_4_5_6_7, :"8_9"]
51
+ assert key_splitter.split.include? [:foo_1_2_3_4_5_6_7_8, :"9"]
52
+ end
53
+ end
@@ -1,25 +1,16 @@
1
+ # coding: utf-8
2
+
1
3
  require 'minitest/autorun'
2
4
  require 'confuse'
3
5
 
6
+ # Test {Confuse::Namespace}
4
7
  class TestNamespace < MiniTest::Unit::TestCase
5
8
  def setup
6
- @namespace = Confuse::Namespace.new do
7
- define :foo do
8
- default 1
9
- end
10
- end
9
+ @namespace = Confuse::Namespace.new
11
10
  end
12
11
 
13
- def test_merge!
14
- namespace = Confuse::Namespace.new do
15
- define :bar do
16
- default 1
17
- end
18
- end
19
-
20
- @namespace.merge!(namespace)
21
-
22
- assert @namespace[:foo]
23
- assert @namespace[:bar]
12
+ def test_add_an_item
13
+ @namespace.add_item(:foo, :default => :foo, :description => 'Test')
14
+ refute_nil @namespace[:foo]
24
15
  end
25
16
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confuse
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
- - 0
8
7
  - 1
9
- - 8
10
- version: 0.1.8
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom Chipchase
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-02-07 00:00:00 Z
18
+ date: 2014-09-15 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: inifile
@@ -60,6 +60,20 @@ dependencies:
60
60
  version: "0"
61
61
  type: :development
62
62
  version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rake
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ version_requirements: *id004
63
77
  description: Add nested configuration to an application
64
78
  email:
65
79
  - tom@mediasp.com
@@ -77,22 +91,28 @@ files:
77
91
  - README.md
78
92
  - Rakefile
79
93
  - confuse.gemspec
80
- - example/001_simple_example.ini
81
- - example/001_simple_example.rb
82
- - example/002_namespace_example.rb
83
- - example/003_config_subclass_instance.ini
84
- - example/003_config_subclass_instance_example.rb
85
- - example/004_configured_by.rb
94
+ - example/env_example.rb
95
+ - example/example.ini
96
+ - example/example.yaml
97
+ - example/ini_example.rb
98
+ - example/yaml_example.rb
86
99
  - lib/confuse.rb
87
100
  - lib/confuse/config.rb
88
- - lib/confuse/config_item.rb
89
- - lib/confuse/config_mixin.rb
90
- - lib/confuse/configurable.rb
91
- - lib/confuse/dsl.rb
101
+ - lib/confuse/definition.rb
102
+ - lib/confuse/errors.rb
103
+ - lib/confuse/item.rb
104
+ - lib/confuse/key_splitter.rb
92
105
  - lib/confuse/namespace.rb
106
+ - lib/confuse/source.rb
107
+ - lib/confuse/source/env.rb
108
+ - lib/confuse/source/ini.rb
109
+ - lib/confuse/source/yaml.rb
93
110
  - lib/confuse/version.rb
94
111
  - test/test_config.rb
95
- - test/test_config_base.rb
112
+ - test/test_confuse.rb
113
+ - test/test_definition.rb
114
+ - test/test_item.rb
115
+ - test/test_key_splitter.rb
96
116
  - test/test_namespace.rb
97
117
  homepage: https://github.com/mediasp/confuse
98
118
  licenses:
@@ -129,6 +149,9 @@ specification_version: 3
129
149
  summary: A DSL for defining configuration options in classes
130
150
  test_files:
131
151
  - test/test_config.rb
132
- - test/test_config_base.rb
152
+ - test/test_confuse.rb
153
+ - test/test_definition.rb
154
+ - test/test_item.rb
155
+ - test/test_key_splitter.rb
133
156
  - test/test_namespace.rb
134
157
  has_rdoc: