yaml_configuration 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +7 -0
- data/lib/yaml_configuration.rb +10 -0
- data/lib/yaml_configuration/configuration.rb +47 -0
- data/lib/yaml_configuration/loader.rb +27 -0
- data/lib/yaml_configuration/version.rb +3 -0
- data/tests/tests_setup.rb +15 -0
- data/tests/yaml_configuration/configuration_tests.rb +41 -0
- data/yaml_configuration.gemspec +25 -0
- metadata +100 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module YamlConfiguration
|
2
|
+
class Configuration
|
3
|
+
def initialize(hash_configuration, parent=nil, name=nil)
|
4
|
+
@config = hash_configuration
|
5
|
+
@name = name
|
6
|
+
@parent = parent
|
7
|
+
end
|
8
|
+
|
9
|
+
def full_name(setting_name)
|
10
|
+
(hierarchy + [setting_name]).join('.')
|
11
|
+
end
|
12
|
+
|
13
|
+
def hierarchy
|
14
|
+
return @parent.hierarchy + [@name] unless @parent.nil?
|
15
|
+
[]
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(method, *args, &block)
|
19
|
+
method_name = method.to_s
|
20
|
+
if asking_if_setting_exist(method_name)
|
21
|
+
setting_exist?(method_name)
|
22
|
+
elsif setting_exist?(method_name)
|
23
|
+
return_value_of_setting(method_name)
|
24
|
+
else
|
25
|
+
raise "The setting '#{full_name(method_name)}' is not present in the configuration"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def asking_if_setting_exist(setting_name)
|
31
|
+
setting_name =~ /^(.*)+\?$/
|
32
|
+
end
|
33
|
+
|
34
|
+
def setting_exist?(setting_name)
|
35
|
+
@config.keys.include?(setting_name.gsub('?', ''))
|
36
|
+
end
|
37
|
+
|
38
|
+
def return_value_of_setting(setting_name)
|
39
|
+
value = @config[setting_name]
|
40
|
+
if value.respond_to?(:keys)
|
41
|
+
return Configuration.new(value, self, setting_name)
|
42
|
+
else
|
43
|
+
return value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module YamlConfiguration
|
2
|
+
class Loader
|
3
|
+
def load(*config_files)
|
4
|
+
combined_configuration = {}
|
5
|
+
config_files.each do |config_file|
|
6
|
+
loaded_config = load_yaml_config(config_file)
|
7
|
+
combined_configuration.deep_merge(loaded_config)
|
8
|
+
end
|
9
|
+
Configuration.new(combined_configuration)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def load_yaml_config(*path)
|
14
|
+
full_path = File.expand_path(File.join(*path) + '.yml')
|
15
|
+
if File.file?(full_path)
|
16
|
+
config = YAML.load(File.read(full_path))
|
17
|
+
raise "The file '#{full_path}' is an invalid yaml config" unless config.respond_to?(:keys)
|
18
|
+
config
|
19
|
+
else
|
20
|
+
puts "Warning: The yaml config file '#{full_path}' does not exist."
|
21
|
+
{}
|
22
|
+
end
|
23
|
+
rescue Psych::SyntaxError => exception
|
24
|
+
raise "Error parsing YAML file '#{full_path}'\n Original cause: \n #{exception.message}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../lib/yaml_configuration.rb'
|
2
|
+
require 'turn'
|
3
|
+
|
4
|
+
def assert_exception(exception, message=nil)
|
5
|
+
begin
|
6
|
+
yield
|
7
|
+
assert false, "The block was expected to throw an exception but it didn't"
|
8
|
+
rescue Exception => ex
|
9
|
+
unless message.nil?
|
10
|
+
assert_equal message, ex.message, "The exception message is not the expected one."
|
11
|
+
end
|
12
|
+
assert_equal exception.to_s, ex.class.to_s, "The block was expected to throw a #{exception} but it did throw #{ex.class}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative '../tests_setup.rb'
|
2
|
+
|
3
|
+
module YamlConfiguration
|
4
|
+
class ConfigurationTests < MiniTest::Unit::TestCase
|
5
|
+
def test_that_configuration_responds_to_existing_configuration_value
|
6
|
+
configuration = Configuration.new({'site_name' => 'dancingonice', 'environment' => 'PRODUCTION'})
|
7
|
+
assert_equal 'dancingonice', configuration.site_name
|
8
|
+
assert_equal 'PRODUCTION', configuration.environment
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_that_good_error_message_returned_for_missing_configuration
|
12
|
+
configuration = Configuration.new({'site_name' => 'dancingonice'})
|
13
|
+
|
14
|
+
assert_exception(RuntimeError, "The setting 'method_that_does_not_exist' is not present in the configuration") do
|
15
|
+
configuration.method_that_does_not_exist
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_that_nested_configuration_return_correct_values
|
20
|
+
configuration = Configuration.new({'database' => {'username' => 'david', 'password' => 'xxxx'}})
|
21
|
+
|
22
|
+
assert_equal 'david', configuration.database.username
|
23
|
+
assert_equal 'xxxx', configuration.database.password
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_that_when_nested_configuration_has_missing_key_exception_mention_parent
|
27
|
+
configuration = Configuration.new({'database' => {'user' => { 'name' => 'david'}, 'password' => 'xxxx'}})
|
28
|
+
|
29
|
+
assert_exception(RuntimeError, "The setting 'database.user.method_that_does_not_exist' is not present in the configuration") do
|
30
|
+
configuration.database.user.method_that_does_not_exist
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_that_asking_for_the_existence_of_a_settings_returns_true_or_false
|
35
|
+
configuration = Configuration.new({'site' => {'name' => 'downtonabbey'}})
|
36
|
+
|
37
|
+
assert configuration.site.name?
|
38
|
+
refute configuration.site.database?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "yaml_configuration/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "yaml_configuration"
|
7
|
+
s.version = YamlConfiguration::VERSION
|
8
|
+
s.authors = ["David Santoro"]
|
9
|
+
s.email = ["soulnafein@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/soulnafein/yaml_configuration"
|
11
|
+
s.summary = %q{A simple wrapper for a yaml based configuration}
|
12
|
+
s.description = %q{Load, merge and provide sensible error messages when using yaml based configuration}
|
13
|
+
|
14
|
+
s.rubyforge_project = "yaml_configuration"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "minitest"
|
22
|
+
s.add_development_dependency "turn"
|
23
|
+
s.add_development_dependency "ansi"
|
24
|
+
s.add_runtime_dependency "hash-deep-merge"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml_configuration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Santoro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-05 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: &85835120 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *85835120
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: turn
|
27
|
+
requirement: &85834810 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *85834810
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: ansi
|
38
|
+
requirement: &85818520 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *85818520
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: hash-deep-merge
|
49
|
+
requirement: &85818310 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *85818310
|
58
|
+
description: Load, merge and provide sensible error messages when using yaml based
|
59
|
+
configuration
|
60
|
+
email:
|
61
|
+
- soulnafein@gmail.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- .gitignore
|
67
|
+
- Gemfile
|
68
|
+
- Rakefile
|
69
|
+
- lib/yaml_configuration.rb
|
70
|
+
- lib/yaml_configuration/configuration.rb
|
71
|
+
- lib/yaml_configuration/loader.rb
|
72
|
+
- lib/yaml_configuration/version.rb
|
73
|
+
- tests/tests_setup.rb
|
74
|
+
- tests/yaml_configuration/configuration_tests.rb
|
75
|
+
- yaml_configuration.gemspec
|
76
|
+
homepage: http://github.com/soulnafein/yaml_configuration
|
77
|
+
licenses: []
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project: yaml_configuration
|
96
|
+
rubygems_version: 1.8.10
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: A simple wrapper for a yaml based configuration
|
100
|
+
test_files: []
|