yaml_config 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Changelog ADDED
@@ -0,0 +1,3 @@
1
+ == v.0.1
2
+
3
+ Initial version: YamlConfig class added.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :gemcutter
2
+
3
+ group :development, :test do
4
+ gem "rspec", ">=2.5.0"
5
+ end
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ == Description
2
+
3
+ Simple wrapper for YAML config files.
4
+
5
+ == Simple example
6
+
7
+ # File config/app_config.yml
8
+ # development:
9
+ # app_host:
10
+ # name: google.com
11
+ # port: 80
12
+ # End of config/app_config.yml
13
+ require 'yaml_config'
14
+
15
+ config = YamlConfig.new(File.join(Rails.root, "config/app_config.yml"), :root => RAILS_ENV)
16
+ config.get(:app_host)[:name] # -> "google.com"
17
+ config.get(:app_host)[:port] # -> 80
18
+ config.get(:app_host)[:some] # -> NullProperty
19
+
20
+ == Different initializations
21
+
22
+ # using filename
23
+ filename = File.join(Rails.root, "config/app_config.yml")
24
+ config = YamlConfig.new(filename, :root => RAILS_ENV)
25
+ # using open stream
26
+ File.open(filename, "r") do |file|
27
+ config = YamlConfig.new(file, :root => RAILS_ENV)
28
+ end
29
+ # using data string
30
+ config = YamlConfig.new(File.read(filename), :root => RAILS_ENV)
31
+
32
+ == Singleton version
33
+
34
+ In many cases (rails application is the best example) developer needs only one file for whole project.
35
+ To accomplish this it possible to use singleton version of YamlConfig.
36
+
37
+ # environment.rb example:
38
+ require "yaml_config"
39
+ ...
40
+ filename = File.join(Rails.root, "config/app_config.yml")
41
+ config = AppYamlConfig.instance.init!(filename, :root => RAILS_ENV)
42
+ ...
43
+ # Usage
44
+ config.get(:username)
45
+
46
+ == Known issues
47
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'yaml_config'
@@ -0,0 +1,4 @@
1
+ require "yaml_config/null_property"
2
+ require "yaml_config/yaml_config"
3
+ require "yaml_config/app_yaml_config"
4
+
@@ -0,0 +1,22 @@
1
+ require "yaml_config/yaml_config"
2
+ require "singleton"
3
+
4
+ class AppYamlConfig
5
+ include Singleton
6
+
7
+ def init!(file, options = {})
8
+ @yaml_config = YamlConfig.new(file, options)
9
+ self
10
+ end
11
+
12
+ def respond_to?(name)
13
+ super || @yaml_config.respond_to?(name)
14
+ end
15
+
16
+ def method_missing(name, *params)
17
+ @yaml_config.respond_to?(name) ?
18
+ @yaml_config.send(name, *params) :
19
+ super
20
+ end
21
+
22
+ end
@@ -0,0 +1,46 @@
1
+ class NullProperty
2
+ @@base_values = [{}, [], "", 0, 0.0, nil]
3
+
4
+ def to_s
5
+ ""
6
+ end
7
+
8
+ def to_i
9
+ 0
10
+ end
11
+
12
+ def to_f
13
+ 0.0
14
+ end
15
+
16
+ def to_a
17
+ []
18
+ end
19
+
20
+ def nil?
21
+ true
22
+ end
23
+
24
+ def ==(val)
25
+ super || @@base_values.any? { |v| v == val }
26
+ end
27
+
28
+ def [](key)
29
+ NullProperty.new
30
+ end
31
+
32
+ def instance_of?(klass)
33
+ super || @@base_values.any? { |v| v.instance_of?(klass) }
34
+ end
35
+
36
+ def respond_to?(method)
37
+ super || @@base_values.any? { |v| v.respond_to?(method) }
38
+ end
39
+
40
+ def method_missing(name, *params)
41
+ @@base_values.each do |val|
42
+ return val.send(name, *params) if val.respond_to?(name)
43
+ end
44
+ super
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+ require "yaml_config/null_property"
2
+
3
+ class YamlConfig
4
+ def initialize(file, options = {})
5
+ @root = options.delete(:root).to_s
6
+ @yaml = if file.respond_to?(:read)
7
+ YAML.load(file.read)
8
+ elsif file.is_a?(String)
9
+ File.exists?(file) ? YAML.load_file(file) : YAML.load(file)
10
+ end
11
+
12
+ raise Exception.new("File is not valid YAML") unless @yaml.is_a?(Hash)
13
+ raise NameError.new("Root :#{@root} doesn't exist in given YAML") unless @root.empty? || @yaml.has_key?(@root)
14
+ end
15
+
16
+ def get(key)
17
+ (@root.empty? ? @yaml[key.to_s] : @yaml[@root][key.to_s]) || NullProperty.new
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ development;url;protocol;host;port
2
+ ;;"http";"localhost";8080
@@ -0,0 +1,21 @@
1
+ development:
2
+ url:
3
+ protocol: "http"
4
+ host: "localhost"
5
+ port: 8080
6
+ username: "Dev"
7
+ duration: 0.0
8
+ test:
9
+ url:
10
+ protocol: "https"
11
+ host: "test.host"
12
+ port: 3000
13
+ username: "Test"
14
+ duration: 0.1
15
+ production:
16
+ url:
17
+ protocol: "http"
18
+ host: "lunich.com.ua"
19
+ port: 80
20
+ username: "Prod"
21
+ duration: 8.0
@@ -0,0 +1,43 @@
1
+ require "yaml_config/null_property"
2
+ require "yaml_config/yaml_config"
3
+ require "yaml_config/app_yaml_config"
4
+
5
+ shared_examples_for "Yaml without root" do
6
+ it { config.get(:development).should be_instance_of(Hash) }
7
+ it { config.get(:development).keys.should == ["duration", "username", "url"] }
8
+
9
+ it { config.get(:development)["username"].should be_instance_of(String) }
10
+ it { config.get(:development)["username"].should == "Dev" }
11
+
12
+ it { config.get(:development)["url"].should be_instance_of(Hash) }
13
+ it { config.get(:development)["url"]["protocol"].should be_instance_of(String) }
14
+ it { config.get(:development)["url"]["protocol"].should == "http" }
15
+ it { config.get(:development)["url"]["host"].should be_instance_of(String) }
16
+ it { config.get(:development)["url"]["host"].should == "localhost" }
17
+ it { config.get(:development)["url"]["port"].should be_instance_of(Fixnum) }
18
+ it { config.get(:development)["url"]["port"].should == 8080 }
19
+
20
+ it { config.get(:development)["duration"].should be_instance_of(Float) }
21
+ it { config.get(:development)["duration"].should == 0.0 }
22
+
23
+ it { config.get(:trunk).should be_instance_of(NullProperty) }
24
+ end
25
+
26
+ shared_examples_for "Yaml with :root => :test" do
27
+ it { config.get(:username).should be_instance_of(String) }
28
+ it { config.get(:username).should == "Test" }
29
+
30
+ it { config.get(:url).should be_instance_of(Hash) }
31
+ it { config.get(:url)["protocol"].should be_instance_of(String) }
32
+ it { config.get(:url)["protocol"].should == "https" }
33
+ it { config.get(:url)["host"].should be_instance_of(String) }
34
+ it { config.get(:url)["host"].should == "test.host" }
35
+ it { config.get(:url)["port"].should be_instance_of(Fixnum) }
36
+ it { config.get(:url)["port"].should == 3000 }
37
+
38
+ it { config.get(:duration).should be_instance_of(Float) }
39
+ it { config.get(:duration).should == 0.1 }
40
+
41
+ it { config.get(:trunk).should be_instance_of(NullProperty) }
42
+ end
43
+
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe AppYamlConfig do
4
+ describe "should be singleton" do
5
+ it "have no initialize method" do
6
+ lambda do
7
+ AppYamlConfig.new
8
+ end.should raise_error
9
+ end
10
+ it "should have :instance method" do
11
+ AppYamlConfig.instance.should be_instance_of(AppYamlConfig)
12
+ end
13
+ end
14
+ describe "with valid yaml" do
15
+ let :filename do
16
+ File.join(File.dirname(__FILE__), "../files/valid_config.yml")
17
+ end
18
+ describe "with data" do
19
+ let :data do
20
+ File.read(filename)
21
+ end
22
+ describe "without :root" do
23
+ let :config do
24
+ AppYamlConfig.instance.init!(data)
25
+ end
26
+ it { config.should be_instance_of(AppYamlConfig) }
27
+ it_should_behave_like "Yaml without root"
28
+ end
29
+ describe "with :root => :test" do
30
+ let :config do
31
+ AppYamlConfig.instance.init!(data, :root => :test)
32
+ end
33
+ it { config.should be_instance_of(AppYamlConfig) }
34
+ it_should_behave_like "Yaml with :root => :test"
35
+ end
36
+ describe "with open file" do
37
+ let :file do
38
+ File.open(filename, "r")
39
+ end
40
+ describe "without :root" do
41
+ let :config do
42
+ AppYamlConfig.instance.init!(file)
43
+ end
44
+ it { config.should be_instance_of(AppYamlConfig) }
45
+ it_should_behave_like "Yaml without root"
46
+ end
47
+ describe "with :root => :test" do
48
+ let :config do
49
+ AppYamlConfig.instance.init!(file, :root => :test)
50
+ end
51
+ it { config.should be_instance_of(AppYamlConfig) }
52
+ it_should_behave_like "Yaml with :root => :test"
53
+ end
54
+ end
55
+ describe "without :root" do
56
+ let :config do
57
+ AppYamlConfig.instance.init!(filename)
58
+ end
59
+ it { config.should be_instance_of(AppYamlConfig) }
60
+ it_should_behave_like "Yaml without root"
61
+ end
62
+ describe "with :root => :test" do
63
+ let :config do
64
+ AppYamlConfig.instance.init!(filename, :root => :test)
65
+ end
66
+ it { config.should be_instance_of(AppYamlConfig) }
67
+ it_should_behave_like "Yaml with :root => :test"
68
+ end
69
+ describe "with invalid root" do
70
+ it "should raise error" do
71
+ lambda do
72
+ AppYamlConfig.instance.init!(filename, :root => :demo)
73
+ end.should raise_exception(NameError)
74
+ end
75
+ end
76
+ end
77
+ describe "with invalid yaml" do
78
+ let :filename do
79
+ File.join(File.dirname(__FILE__), "../files/invalid_config.yml")
80
+ end
81
+ it "should raise error" do
82
+ lambda do
83
+ AppYamlConfig.instance.init!(filename, :root => :demo)
84
+ end.should raise_exception(Exception)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe NullProperty do
4
+ let :property do
5
+ NullProperty.new
6
+ end
7
+ [{}, [], "", 0, 0.0, nil].each do |obj|
8
+ describe "as #{obj.class} object" do
9
+ it { property.should == obj }
10
+ it { property.should be_instance_of(obj.class) }
11
+ obj.methods.each do |method|
12
+ it { property.should respond_to(method) }
13
+ end
14
+ end
15
+ end
16
+ describe "[] method" do
17
+ describe "should return NullProperty object" do
18
+ it "with string key" do
19
+ property["property"].should be_instance_of(NullProperty)
20
+ end
21
+ it "with symbol key" do
22
+ property[:property].should be_instance_of(NullProperty)
23
+ end
24
+ it "with integer key" do
25
+ property[0].should be_instance_of(NullProperty)
26
+ end
27
+ end
28
+ end
29
+ it { property.to_s.should == "" }
30
+ it { property.to_f.should == 0.0 }
31
+ it { property.to_i.should == 0 }
32
+ it { property.to_a.should == [] }
33
+ it { property.should be_nil }
34
+ it { property.should be_empty }
35
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe YamlConfig do
4
+ describe "with valid yaml" do
5
+ let :filename do
6
+ File.join(File.dirname(__FILE__), "../files/valid_config.yml")
7
+ end
8
+ describe "with data" do
9
+ let :data do
10
+ File.read(filename)
11
+ end
12
+ describe "without :root" do
13
+ let :config do
14
+ YamlConfig.new(data)
15
+ end
16
+ it_should_behave_like "Yaml without root"
17
+ end
18
+ describe "with :root => :test" do
19
+ let :config do
20
+ YamlConfig.new(data, :root => :test)
21
+ end
22
+ it_should_behave_like "Yaml with :root => :test"
23
+ end
24
+ end
25
+ describe "with open file" do
26
+ let :file do
27
+ File.open(filename, "r")
28
+ end
29
+ describe "without :root" do
30
+ let :config do
31
+ YamlConfig.new(file)
32
+ end
33
+ it_should_behave_like "Yaml without root"
34
+ end
35
+ describe "with :root => :test" do
36
+ let :config do
37
+ YamlConfig.new(file, :root => :test)
38
+ end
39
+ it_should_behave_like "Yaml with :root => :test"
40
+ end
41
+ end
42
+ describe "without :root" do
43
+ let :config do
44
+ YamlConfig.new(filename)
45
+ end
46
+ it_should_behave_like "Yaml without root"
47
+ end
48
+ describe "with :root => :test" do
49
+ let :config do
50
+ YamlConfig.new(filename, :root => :test)
51
+ end
52
+ it_should_behave_like "Yaml with :root => :test"
53
+ end
54
+ describe "with invalid root" do
55
+ it "should raise error" do
56
+ lambda do
57
+ YamlConfig.new(filename, :root => :demo)
58
+ end.should raise_exception(NameError)
59
+ end
60
+ end
61
+ end
62
+ describe "with invalid yaml" do
63
+ let :filename do
64
+ File.join(File.dirname(__FILE__), "../files/invalid_config.yml")
65
+ end
66
+ it "should raise error" do
67
+ lambda do
68
+ YamlConfig.new(filename, :root => :demo)
69
+ end.should raise_exception(Exception)
70
+ end
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml_config
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Dima Lunich
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-29 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: This gem reads and manage YAML config files
36
+ email:
37
+ - dima.lunich@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - README.rdoc
44
+ files:
45
+ - lib/yaml_config/app_yaml_config.rb
46
+ - lib/yaml_config/null_property.rb
47
+ - lib/yaml_config/yaml_config.rb
48
+ - lib/yaml_config.rb
49
+ - spec/files/invalid_config.yml
50
+ - spec/files/valid_config.yml
51
+ - spec/spec_helper.rb
52
+ - spec/yaml_config/app_yaml_config_spec.rb
53
+ - spec/yaml_config/null_preperty_spec.rb
54
+ - spec/yaml_config/yaml_config_spec.rb
55
+ - README.rdoc
56
+ - Changelog
57
+ - Gemfile
58
+ - init.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/lunich/yaml_config
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --main
66
+ - README.rdoc
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project: ""
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: yaml_config-1.0.0
94
+ test_files:
95
+ - spec/files/invalid_config.yml
96
+ - spec/files/valid_config.yml
97
+ - spec/spec_helper.rb
98
+ - spec/yaml_config/app_yaml_config_spec.rb
99
+ - spec/yaml_config/null_preperty_spec.rb
100
+ - spec/yaml_config/yaml_config_spec.rb