configmanager 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ #
2
+ # 16 Jul 2012
3
+ #
4
+
5
+ require "options_arg"
6
+
7
+ require "configmanager/configuration"
8
+ require "configmanager/loaders"
9
+
10
+ # General purpose configuration manager.
11
+ # Loads properties from YAML files and makes them available through a same hash like interface.
12
+ # Supports ${} and $() style references. All references are evaluated only when a property's value is requested.
13
+ module ConfigManager
14
+
15
+ # Loads properties from the specified file.
16
+ #
17
+ # @param [String] file_path the file to load properties from.
18
+ # @param [Object] loader any object that can respond to the 'load_properties(file_path, configuration)' method and return a hash.
19
+ # @param [ConfigManager::Configuration] configuration the configuration to load the properties into.
20
+ #
21
+ # @return [ConfigManager::Configuration] the configuration object wrapping the properties loaded from the file.
22
+ def self.load_from_file(file_path, loader = YAMLPropertiesLoader, configuration = ConfigManager::Configuration.new())
23
+ loader.load_properties(file_path, configuration)
24
+ configuration
25
+ end
26
+
27
+ end
@@ -0,0 +1,66 @@
1
+ #
2
+ # 16 Jul 2012
3
+ #
4
+
5
+ # Provides a few methods to work with hashes.
6
+ module Kernel
7
+
8
+ # Raised if a required option has not been set.
9
+ class OptionNotSetError < Exception
10
+ end
11
+
12
+ private
13
+
14
+ # Get a value from the specified hash.
15
+ #
16
+ # @param [Hash] options the hash.
17
+ # @param [Symbol] key the key whose value is required.
18
+ # @param [TrueClass, FalseClass] required flag to indicate if it is an error if the key does not exist.
19
+ # @param [Object] default the default value if the key does not exist. Only used if 'required' is false.
20
+ #
21
+ # @return [Object] the value from the hash, or the default value.
22
+ def get_option(options, key, required = false, default = nil)
23
+ if options.has_key?(key)
24
+ options[key]
25
+ elsif required
26
+ raise OptionNotSetError.new("Option '#{key}' has not been set!")
27
+ else
28
+ default
29
+ end
30
+ end
31
+
32
+ # Pops (delete) a value from the specified hash.
33
+ #
34
+ # @param [Hash] options the hash.
35
+ # @param [Symbol] key the key whose value is required.
36
+ # @param [TrueClass, FalseClass] required flag to indicate if it is an error if the key does not exist.
37
+ # @param [Object] default the default value if the key does not exist. Only used if 'required' is false.
38
+ #
39
+ # @return [Object] the value from the hash, or the default value.
40
+ def pop_option(options, key, required = false, default = nil)
41
+ if options.has_key?(key)
42
+ options.delete(key)
43
+ elsif required
44
+ raise OptionNotSetError.new("Option '#{key}' has not been set!")
45
+ else
46
+ default
47
+ end
48
+ end
49
+
50
+ # Sets a value in the specified hash.
51
+ #
52
+ # @param [Hash] options the hash.
53
+ # @param [Symbol] key the key whose value is to be set.
54
+ # @param [Object] value the value to set.
55
+ # @param [TrueClass, FalseClass] overwrite flag to indicate if existing values should be overwritten.
56
+ #
57
+ # @return [Object] the value in the hash at the end of the operation.
58
+ def set_option(options, key, value, overwrite)
59
+ if not options.has_key?(key) or overwrite
60
+ options[key] = value
61
+ end
62
+ # Return whatever is in the hash now.
63
+ options[key]
64
+ end
65
+
66
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # 20 Jul 2012
3
+ #
4
+
5
+ $LOAD_PATH << "."
6
+
7
+ require "configmanager"
8
+ require "configmanager/combined_configuration"
9
+ require "configmanager/loaders"
10
+
11
+ properties_configuration = ConfigManager.load_from_file("samples/test 1.properties", ConfigManager::JavaPropertiesLoader)
12
+ yaml_configuration = ConfigManager.load_from_file("samples/test 1.yaml", ConfigManager::YAMLPropertiesLoader)
13
+
14
+ test_configuration = ConfigManager::CombinedConfiguration.new()
15
+ test_configuration.add_configuration("properties", properties_configuration)
16
+ test_configuration.add_configuration("yaml", yaml_configuration)
17
+
18
+ keys = ["sample_string", "sample_int", "sample_float", "sample_array", "sample_int_array", "sample_reference",
19
+ "sample_reference_array"]
20
+
21
+ puts ".properties"
22
+ keys.each { |key| puts test_configuration["test.#{key}"].inspect }
23
+
24
+ puts ""
25
+ puts ".yaml"
26
+ keys.each { |key| puts test_configuration["test_yaml.#{key}"].inspect }
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configmanager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Suraj Vijayakumar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-20 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ConfigManager can load properties from YAML or Java style property files.
15
+ Properties can have references to other properties that are resolved at run-time.
16
+ Inspired, in part by Apache's configuration API.
17
+ email:
18
+ - vijayakumar.suraj@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/configmanager.rb
24
+ - lib/options_arg.rb
25
+ - lib/test_configmanager.rb
26
+ homepage:
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.7.2
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: An easy configuration manager for managing an applications configuration
50
+ files.
51
+ test_files: []