yamlconfig 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yamlconfig.rb +109 -101
- data/lib/yamlconfig/version.rb +1 -1
- data/{test → spec}/data/accessor_config +0 -0
- data/spec/data/accessor_config_tmp +8 -0
- data/{test → spec}/data/complex_values +0 -0
- data/{test → spec}/data/config +0 -0
- data/{test → spec}/data/empty_file +0 -0
- data/{test → spec}/data/flatulentmonkey +0 -0
- data/{test → spec}/data/freakboy +0 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/yamlconfig_spec.rb +145 -0
- metadata +53 -43
- data/test/tc_yamlconfig.rb +0 -102
data/lib/yamlconfig.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
#
|
2
|
-
#
|
1
|
+
#
|
3
2
|
# See LICENSE for copyright information
|
4
3
|
#
|
5
4
|
|
@@ -8,117 +7,126 @@ require 'yaml'
|
|
8
7
|
# This class represents a configuration, using a YAML file,
|
9
8
|
# by providing "accessor" methods for each item in the YAML file
|
10
9
|
class YAMLConfig
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
10
|
+
attr_accessor :resource
|
11
|
+
include Enumerable
|
12
|
+
|
13
|
+
def initialize(resource)
|
14
|
+
@specialmethods = Array.new
|
15
|
+
@resource = resource
|
16
|
+
|
17
|
+
reload_from_file()
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# This method is provided for keys and other structures where
|
22
|
+
# getting at the method call is impossible.
|
23
|
+
#
|
24
|
+
# This will return 'nil' for all values if the YAML you have loaded
|
25
|
+
# is improperly formed.
|
26
|
+
#
|
27
|
+
|
28
|
+
def [](key)
|
29
|
+
if !@con.nil?
|
30
|
+
return @con[key]
|
31
|
+
end
|
32
|
+
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# This is a writer in the same spirit as the [] method. Please read
|
38
|
+
# the documentation for that.
|
39
|
+
#
|
40
|
+
|
41
|
+
def []=(key, value)
|
42
|
+
if !@con.nil?
|
43
|
+
return @con[key] = value
|
44
|
+
end
|
45
|
+
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# this should take a [] capable object, add it (keys and values) to
|
51
|
+
# self, and return self
|
52
|
+
#
|
53
|
+
|
54
|
+
def +(other)
|
55
|
+
if !@con.nil?
|
56
|
+
other.each do |k, v|
|
57
|
+
# add it to con
|
58
|
+
@con[k] = v
|
59
|
+
|
60
|
+
# add it as a method
|
61
|
+
(class << self; self; end).class_eval { define_method(k) { v }}
|
62
|
+
|
63
|
+
#add it to the @specialmethods array
|
64
|
+
@specialmethods.push k
|
65
|
+
end
|
66
|
+
end
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
#
|
71
|
+
# Rewrite and reload the yaml file. Note, that this will overwrite your current
|
72
|
+
# yaml file and reload your configuration. If you want to avoid
|
73
|
+
# doing this, provide the path to a filename for it to work with.
|
74
|
+
#
|
75
|
+
|
76
|
+
def write(io=@resource)
|
77
|
+
File.open(io, 'w+') do |f|
|
78
|
+
f << YAML::dump(@con)
|
79
|
+
end
|
80
|
+
|
81
|
+
if io == @resource
|
82
|
+
reload_from_file
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Remove all current (if any) attr_readers, then load the resource
|
87
|
+
# named in @resource into psuedo attr_readers
|
88
|
+
def reload_from_file
|
89
|
+
|
90
|
+
# first remove all the previous methods from this instance
|
78
91
|
if @specialmethods
|
79
92
|
@specialmethods.each do |meth|
|
80
|
-
(class << self; self; end).class_eval { undef_method(meth) }
|
93
|
+
(class << self; self; end).class_eval { undef_method(meth) }
|
81
94
|
end
|
82
95
|
end
|
83
96
|
|
84
|
-
|
85
|
-
@specialmethods = []
|
97
|
+
@specialmethods = []
|
86
98
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
99
|
+
# then load the new methods
|
100
|
+
# add a method to the Singleton class of this instance of Config.
|
101
|
+
# giving a private readonly accessor named "key" that returns "value" for each
|
102
|
+
# key value pair of the YAML file in @resource
|
103
|
+
@con = loadresource()
|
92
104
|
if @con
|
93
105
|
@con.each do |key,value|
|
94
106
|
(class << self; self; end).class_eval { define_method(key) { value }}
|
95
|
-
|
96
|
-
#add it to the @specialmethods array
|
97
107
|
@specialmethods.push key
|
98
108
|
end
|
99
109
|
end
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
110
|
+
end
|
111
|
+
|
112
|
+
# standard each usage, iterates over the config keys
|
113
|
+
# returning key,value
|
114
|
+
def each
|
115
|
+
@specialmethods.each do |key|
|
116
|
+
yield key, self.send(key)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
#--
|
122
|
+
# End of public documentation
|
123
|
+
private
|
124
|
+
|
125
|
+
def loadresource()
|
126
|
+
# TODO, make this take plugins or some sort of adapter type scheme for
|
127
|
+
# deciding how to load what into a hash
|
128
|
+
YAML.load_file(@resource) || { }
|
129
|
+
end
|
120
130
|
end
|
121
131
|
|
122
132
|
require 'yamlconfig/version'
|
123
|
-
|
124
|
-
# vi:sw=2 ts=2
|
data/lib/yamlconfig/version.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
YAMLConfig::VERSION = "0.
|
1
|
+
YAMLConfig::VERSION = "0.2.0"
|
File without changes
|
File without changes
|
data/{test → spec}/data/config
RENAMED
File without changes
|
File without changes
|
File without changes
|
data/{test → spec}/data/freakboy
RENAMED
File without changes
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yamlconfig'
|
3
|
+
|
4
|
+
describe YAMLConfig do
|
5
|
+
describe "with an empty file" do
|
6
|
+
it "does not raise an exception" do
|
7
|
+
lambda {
|
8
|
+
YAMLConfig.new('spec/data/empty_file')
|
9
|
+
}.should_not raise_error
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "with complex values" do
|
14
|
+
before :each do
|
15
|
+
@yc = YAMLConfig.new('spec/data/complex_values')
|
16
|
+
end
|
17
|
+
|
18
|
+
it "creates an instance" do
|
19
|
+
@yc.kind_of?(YAMLConfig).should be_true
|
20
|
+
end
|
21
|
+
|
22
|
+
it "parses array values" do
|
23
|
+
@yc.arrayval[1].should == "bar"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "parses hashes" do
|
27
|
+
@yc.hashval["foo"].should == "FOO"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#write" do
|
32
|
+
before :each do
|
33
|
+
@old_file = 'spec/data/accessor_config'
|
34
|
+
@new_file = @old_file + '_tmp'
|
35
|
+
FileUtils.cp @old_file, @new_file
|
36
|
+
|
37
|
+
@yc = YAMLConfig.new @new_file
|
38
|
+
@yc['nogoodshits'] = %W(cmaujean erikh PopeKetric)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "writes to the same filehandle and reloads" do
|
42
|
+
lambda { @yc.write }.should_not raise_error
|
43
|
+
@yc['nogoodshits'].should == %W(cmaujean erikh PopeKetric)
|
44
|
+
@yc['#nogoodshits'].should == %W(*.bar.com *.example.com)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "writes to a new filehandle and does not reload" do
|
48
|
+
FileUtils.rm @new_file rescue true # don't care if the file doesn't exist
|
49
|
+
@yc.write @new_file
|
50
|
+
|
51
|
+
@yc['nogoodshits'].should == %W(cmaujean erikh PopeKetric)
|
52
|
+
@yc['#nogoodshits'].should == %W(*.bar.com *.example.com)
|
53
|
+
end
|
54
|
+
|
55
|
+
after :each do
|
56
|
+
#FileUtils.rm @new_file rescue true # don't care if the file doesn't exist
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#[key]" do
|
61
|
+
before :each do
|
62
|
+
lambda {
|
63
|
+
@yc = YAMLConfig.new('spec/data/accessor_config')
|
64
|
+
}.should_not raise_error
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns the value for the key requested" do
|
68
|
+
@yc['#nogoodshits'].should == %W(*.bar.com *.example.com)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns nil for keys that do not exist" do
|
72
|
+
@yc['monkey'].should be_nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "#[key]=value" do
|
77
|
+
before :each do
|
78
|
+
lambda {
|
79
|
+
@yc = YAMLConfig.new('spec/data/accessor_config')
|
80
|
+
}.should_not raise_error
|
81
|
+
@yc['monkey'] = "foo"
|
82
|
+
end
|
83
|
+
|
84
|
+
it "sets key=value for new keys" do
|
85
|
+
@yc['monkey'].should == 'foo'
|
86
|
+
end
|
87
|
+
|
88
|
+
it "sets key=value for existing keys" do
|
89
|
+
@yc['monkey'] = "bar"
|
90
|
+
@yc['monkey'].should == "bar"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "dynamic accessors" do
|
95
|
+
before :each do
|
96
|
+
@freakboy = YAMLConfig.new('spec/data/freakboy')
|
97
|
+
@config = YAMLConfig.new('spec/data/config')
|
98
|
+
end
|
99
|
+
|
100
|
+
it "returns the value when a key is called as an accessor" do
|
101
|
+
@freakboy.boondongle.should == "foogalicious"
|
102
|
+
end
|
103
|
+
|
104
|
+
it "it retains different values for different instances" do
|
105
|
+
@freakboy.servername.should == 'freakboy'
|
106
|
+
@config.servername.should == 'normalfairypoof'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "reload_from_file" do
|
111
|
+
before :each do
|
112
|
+
@yc = YAMLConfig.new('spec/data/freakboy')
|
113
|
+
@yc.resource = 'spec/data/flatulentmonkey'
|
114
|
+
@yc.reload_from_file
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should throw NoMethodError for non-existent methods" do
|
118
|
+
lambda {
|
119
|
+
@yc.boondongle
|
120
|
+
}.should raise_error(NoMethodError)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should not throw errors for existing methods" do
|
124
|
+
@yc.servername.should == 'flatulentmonkey'
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#each" do
|
129
|
+
it "iterates the keys and values" do
|
130
|
+
f = YAMLConfig.new('spec/data/freakboy')
|
131
|
+
f.each do |key,value|
|
132
|
+
f[key].should == value
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#+" do
|
138
|
+
it "adds the new keys and values to itself" do
|
139
|
+
f = YAMLConfig.new('spec/data/freakboy')
|
140
|
+
y = { 'a' => '1', 'b' => '2' }
|
141
|
+
a = f + y
|
142
|
+
f.a.should == '1'
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
metadata
CHANGED
@@ -1,57 +1,67 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.4
|
3
|
-
specification_version: 1
|
4
2
|
name: yamlconfig
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
|
7
|
-
|
8
|
-
summary: YAML file based configuration object with an EASY interface
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: cmaujean@gmail.com
|
12
|
-
homepage: http://rubyforge.org/projects/ngslib
|
13
|
-
rubyforge_project: ngslib
|
14
|
-
description:
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
prerelease:
|
5
|
+
version: 0.2.0
|
25
6
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
7
|
authors:
|
30
8
|
- Christopher Maujean
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-05 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description:
|
18
|
+
email: cmaujean@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- lib/LICENSE
|
31
25
|
files:
|
32
26
|
- lib/LICENSE
|
33
|
-
- lib/yamlconfig
|
34
27
|
- lib/yamlconfig/version.rb
|
35
28
|
- lib/yamlconfig.rb
|
36
|
-
-
|
37
|
-
-
|
38
|
-
-
|
39
|
-
-
|
40
|
-
-
|
41
|
-
-
|
42
|
-
-
|
43
|
-
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
extra_rdoc_files:
|
49
|
-
- lib/LICENSE
|
50
|
-
executables: []
|
29
|
+
- spec/data/accessor_config
|
30
|
+
- spec/data/accessor_config_tmp
|
31
|
+
- spec/data/complex_values
|
32
|
+
- spec/data/config
|
33
|
+
- spec/data/empty_file
|
34
|
+
- spec/data/flatulentmonkey
|
35
|
+
- spec/data/freakboy
|
36
|
+
- spec/spec_helper.rb
|
37
|
+
- spec/yamlconfig_spec.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://rubyforge.org/projects/ngslib
|
40
|
+
licenses: []
|
51
41
|
|
52
|
-
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
53
44
|
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
54
59
|
requirements: []
|
55
60
|
|
56
|
-
|
57
|
-
|
61
|
+
rubyforge_project: ngslib
|
62
|
+
rubygems_version: 1.6.2
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: YAML file based configuration object with an EASY interface
|
66
|
+
test_files:
|
67
|
+
- spec/yamlconfig_spec.rb
|
data/test/tc_yamlconfig.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'yamlconfig'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
class TestConfig < Test::Unit::TestCase
|
6
|
-
def test_yaml_empty
|
7
|
-
assert_nothing_raised do
|
8
|
-
YAMLConfig.new('yamlconfig/test/data/empty_file')
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_yaml_complex
|
13
|
-
f = YAMLConfig.new('yamlconfig/test/data/complex_values')
|
14
|
-
assert_equal(f.arrayval[1], "bar", "array values work")
|
15
|
-
assert_equal(f.hashval["foo"], "FOO", "hash values work")
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_yaml_write
|
19
|
-
old_file = 'yamlconfig/test/data/accessor_config'
|
20
|
-
new_file = old_file + '_tmp'
|
21
|
-
FileUtils.cp old_file, new_file
|
22
|
-
|
23
|
-
# write to the same filehandle
|
24
|
-
f = nil
|
25
|
-
assert_nothing_raised do
|
26
|
-
f = YAMLConfig.new new_file
|
27
|
-
f['nogoodshits'] = %W(cmaujean erikh PopeKetric)
|
28
|
-
f.write
|
29
|
-
end
|
30
|
-
|
31
|
-
# test that it reloads properly
|
32
|
-
|
33
|
-
assert_equal f['nogoodshits'], %W(cmaujean erikh PopeKetric)
|
34
|
-
assert_equal f['#nogoodshits'], %W(*.bar.com *.example.com)
|
35
|
-
|
36
|
-
FileUtils.rm new_file
|
37
|
-
|
38
|
-
# write to a new filehandle
|
39
|
-
|
40
|
-
assert_nothing_raised do
|
41
|
-
f.write new_file
|
42
|
-
f = YAMLConfig.new new_file
|
43
|
-
end
|
44
|
-
|
45
|
-
# test that it created the right data
|
46
|
-
|
47
|
-
assert_equal f['nogoodshits'], %W(cmaujean erikh PopeKetric)
|
48
|
-
assert_equal f['#nogoodshits'], %W(*.bar.com *.example.com)
|
49
|
-
|
50
|
-
FileUtils.rm new_file
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_array_accessor
|
55
|
-
f = nil
|
56
|
-
assert_nothing_raised do
|
57
|
-
f = YAMLConfig.new('yamlconfig/test/data/accessor_config')
|
58
|
-
end
|
59
|
-
assert_equal f['#nogoodshits'], %W(*.bar.com *.example.com)
|
60
|
-
assert_nil f['monkey']
|
61
|
-
|
62
|
-
assert_nothing_raised do
|
63
|
-
f['monkey'] = "foo"
|
64
|
-
end
|
65
|
-
|
66
|
-
assert_equal f['monkey'], 'foo'
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_accessors
|
70
|
-
f = YAMLConfig.new('yamlconfig/test/data/freakboy')
|
71
|
-
g = YAMLConfig.new('yamlconfig/test/data/config')
|
72
|
-
|
73
|
-
assert_equal f.servername, 'freakboy'
|
74
|
-
assert_equal g.servername, 'normalfairypoof'
|
75
|
-
assert_equal f.boondongle, 'foogalicious'
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
def test_reload
|
81
|
-
f = YAMLConfig.new('yamlconfig/test/data/freakboy')
|
82
|
-
f.resource = 'yamlconfig/test/data/flatulentmonkey'
|
83
|
-
f.reload
|
84
|
-
begin
|
85
|
-
f.boondongle
|
86
|
-
rescue NoMethodError
|
87
|
-
assert(true, "NoMethodError was triggered for boondongle")
|
88
|
-
else
|
89
|
-
assert(false, "NoMethodError was not triggered! something is wrong, return was #{f.boondongle}")
|
90
|
-
end
|
91
|
-
assert_equal f.servername, 'flatulentmonkey'
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_each
|
95
|
-
f = YAMLConfig.new('yamlconfig/test/data/freakboy')
|
96
|
-
f.each do |key,value|
|
97
|
-
assert f[key] == value, "part of the each tests, f[#{key}] == #{value}"
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# vi:sw=2 ts=2
|