yamlconfig 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2006, Christopher Maujean and project contributors
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * Neither the name of the NGSLib nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/lib/yamlconfig.rb ADDED
@@ -0,0 +1,116 @@
1
+ # $Id: yamlconfig.rb 44 2006-08-13 23:18:33Z cmaujean $
2
+ # See LICENSE for copyright information
3
+ require 'yaml'
4
+
5
+ # This class represents a configuration, using a YAML file
6
+ # by providing "accessor" methods for each item in the YAML file
7
+ class YAMLConfig
8
+ attr_accessor :resource
9
+ include Enumerable
10
+
11
+ def initialize(resource)
12
+ @specialmethods = Array.new
13
+ @resource = resource
14
+
15
+ # initial method load
16
+ reload()
17
+ end
18
+
19
+ #
20
+ # This method is provided for keys and other structures where
21
+ # getting at the method call is impossible.
22
+ #
23
+ # This will return 'nil' for all values if the YAML you have loaded
24
+ # is improperly formed.
25
+ #
26
+
27
+ def [](key)
28
+ if !@con.nil?
29
+ return @con[key]
30
+ end
31
+
32
+ nil
33
+ end
34
+
35
+ #
36
+ # This is a writer in the same spirit as the [] method. Please read
37
+ # the documentation for that.
38
+ #
39
+
40
+ def []=(key, value)
41
+ if !@con.nil?
42
+ return @con[key] = value
43
+ end
44
+
45
+ nil
46
+ end
47
+
48
+ #
49
+ # Rewrite and reload the yaml file. Note, that this will overwrite your current
50
+ # yaml file and reload your configuration. If you want to avoid
51
+ # doing this, provide the path to a filename for it to work with.
52
+ #
53
+
54
+ def write(io=@resource)
55
+ # Some duck-typing checks to make sure we're dealing with
56
+ # something that looks like an IO object.
57
+
58
+ f = File.open(io, 'w')
59
+
60
+ f << YAML::dump(@con)
61
+
62
+ if io == @resource
63
+ f.reopen(io, 'r')
64
+ reload
65
+ end
66
+
67
+ true
68
+ end
69
+
70
+ # Remove all current (if any) attr_readers, then load the resource
71
+ # named in @resource into psuedo attr_readers
72
+ def reload
73
+ # first remove all the previous methods from this instance
74
+ @specialmethods.each do |meth|
75
+ (class << self; self; end).class_eval { undef_method(meth) }
76
+ end
77
+
78
+ # wipe the old list
79
+ @specialmethods = []
80
+
81
+ # then load the new methods
82
+ # add a method to the Singleton class of this instance of Config.
83
+ # giving a private readonly accessor named "key" that returns "value" for each
84
+ # key value pair of the YAML file named in @filename
85
+ @con = loadresource()
86
+ @con.each do |key,value|
87
+ (class << self; self; end).class_eval { define_method(key) { value }}
88
+
89
+ #add it to the @specialmethods array
90
+ @specialmethods.push key
91
+ end
92
+
93
+
94
+ end
95
+
96
+ # standard each usage, iterates over the config keys
97
+ # returning key,value
98
+ def each
99
+ @specialmethods.each do |key|
100
+ yield key, self.send(key)
101
+ end
102
+ end
103
+
104
+
105
+ #--
106
+ # End of public documentation
107
+ private
108
+
109
+ def loadresource()
110
+ # TODO, make this take plugins or some sort of adapter type scheme for
111
+ # deciding how to load what into a hash
112
+ YAML.load_file(@resource)
113
+ end
114
+ end
115
+
116
+ # vi:sw=2 ts=2
@@ -0,0 +1 @@
1
+ '#nogoodshits': [ '*.bar.com', '*.example.com' ]
data/test/data/config ADDED
@@ -0,0 +1 @@
1
+ servername: normalfairypoof
@@ -0,0 +1 @@
1
+ servername: flatulentmonkey
@@ -0,0 +1,2 @@
1
+ servername: freakboy
2
+ boondongle: foogalicious
@@ -0,0 +1,90 @@
1
+ require 'test/unit'
2
+ require 'yamlconfig'
3
+ require 'fileutils'
4
+
5
+ class TestConfig < Test::Unit::TestCase
6
+ def test_yaml_write
7
+ old_file = 'yamlconfig/test/data/accessor_config'
8
+ new_file = old_file + '_tmp'
9
+ FileUtils.cp old_file, new_file
10
+
11
+ # write to the same filehandle
12
+ f = nil
13
+ assert_nothing_raised do
14
+ f = YAMLConfig.new new_file
15
+ f['nogoodshits'] = %W(cmaujean erikh PopeKetric)
16
+ f.write
17
+ end
18
+
19
+ # test that it reloads properly
20
+
21
+ assert_equal f['nogoodshits'], %W(cmaujean erikh PopeKetric)
22
+ assert_equal f['#nogoodshits'], %W(*.bar.com *.example.com)
23
+
24
+ FileUtils.rm new_file
25
+
26
+ # write to a new filehandle
27
+
28
+ assert_nothing_raised do
29
+ f.write new_file
30
+ f = YAMLConfig.new new_file
31
+ end
32
+
33
+ # test that it created the right data
34
+
35
+ assert_equal f['nogoodshits'], %W(cmaujean erikh PopeKetric)
36
+ assert_equal f['#nogoodshits'], %W(*.bar.com *.example.com)
37
+
38
+ FileUtils.rm new_file
39
+
40
+ end
41
+
42
+ def test_array_accessor
43
+ f = nil
44
+ assert_nothing_raised do
45
+ f = YAMLConfig.new('yamlconfig/test/data/accessor_config')
46
+ end
47
+ assert_equal f['#nogoodshits'], %W(*.bar.com *.example.com)
48
+ assert_nil f['monkey']
49
+
50
+ assert_nothing_raised do
51
+ f['monkey'] = "foo"
52
+ end
53
+
54
+ assert_equal f['monkey'], 'foo'
55
+ end
56
+
57
+ def test_accessors
58
+ f = YAMLConfig.new('yamlconfig/test/data/freakboy')
59
+ g = YAMLConfig.new('yamlconfig/test/data/config')
60
+
61
+ assert_equal f.servername, 'freakboy'
62
+ assert_equal g.servername, 'normalfairypoof'
63
+ assert_equal f.boondongle, 'foogalicious'
64
+
65
+ end
66
+
67
+
68
+ def test_reload
69
+ f = YAMLConfig.new('yamlconfig/test/data/freakboy')
70
+ f.resource = 'yamlconfig/test/data/flatulentmonkey'
71
+ f.reload
72
+ begin
73
+ f.boondongle
74
+ rescue NoMethodError
75
+ assert(true, "NoMethodError was triggered for boondongle")
76
+ else
77
+ assert(false, "NoMethodError was not triggered! something is wrong, return was #{f.boondongle}")
78
+ end
79
+ assert_equal f.servername, 'flatulentmonkey'
80
+ end
81
+
82
+ def test_each
83
+ f = YAMLConfig.new('yamlconfig/test/data/freakboy')
84
+ f.each do |key,value|
85
+ assert f[key] == value, "part of the each tests, f[#{key}] == #{value}"
86
+ end
87
+ end
88
+ end
89
+
90
+ # vi:sw=2 ts=2
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: yamlconfig
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-08-13 00:00:00 -07:00
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:
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:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Christopher Maujean
31
+ files:
32
+ - lib/yamlconfig.rb
33
+ - lib/LICENSE
34
+ - test/data
35
+ - test/tc_yamlconfig.rb
36
+ - test/data/config
37
+ - test/data/freakboy
38
+ - test/data/flatulentmonkey
39
+ - test/data/accessor_config
40
+ test_files:
41
+ - test/tc_yamlconfig.rb
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+