Class: CaRuby::Properties
- Inherits:
-
Hash
- Object
- Hash
- CaRuby::Properties
- Defined in:
- lib/caruby/helpers/properties.rb
Overview
A Properties instance encapsulates a properties file accessor. The properties are stored in YAML format.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the property value for the key.
-
#[]=(key, value) ⇒ Object
Returns the property value for the key.
-
#delete(key) ⇒ Object
Deletes the entry for the given property key or its alternate.
-
#has_property?(key) ⇒ Boolean
Returns whether the property key or its alternate is defined.
-
#initialize(file = nil, options = nil) ⇒ Properties
constructor
Creates a new Properties object.
-
#load_properties(file) ⇒ Object
Loads the specified properties file, replacing any existing properties.
-
#symbolize ⇒ Object
Returns a new Hash which associates this Properties’ keys converted to symbols to the respective values.
Constructor Details
#initialize(file = nil, options = nil) ⇒ Properties
Creates a new Properties object. If the file argument is given, then the properties are loaded from that file.
Supported options include the following:
-
:merge - the properties which are merged rather than replaced when loaded from the property files
-
:required - the properties which must be set when the property files are loaded
-
:array - the properties whose comma-separated String input value is converted to an array value
21 22 23 24 25 26 27 |
# File 'lib/caruby/helpers/properties.rb', line 21 def initialize(file=nil, =nil) super() @merge_properties = Options.get(:merge, , []).to_set @required_properties = Options.get(:required, , []).to_set @array_properties = Options.get(:array, , []).to_set load_properties(file) if file end |
Instance Method Details
#[](key) ⇒ Object
Returns the property value for the key. If there is no String key entry but there is a alternate key entry, then this method returns the alternate key value.
41 42 43 |
# File 'lib/caruby/helpers/properties.rb', line 41 def [](key) super(key) or super(alternate_key(key)) end |
#[]=(key, value) ⇒ Object
Returns the property value for the key. If there is no key entry but there is an alternate key entry, then alternate key entry is set.
47 48 49 50 51 |
# File 'lib/caruby/helpers/properties.rb', line 47 def []=(key, value) return super if has_key?(key) alt = alternate_key(key) has_key?(alt) ? super(alt, value) : super end |
#delete(key) ⇒ Object
Deletes the entry for the given property key or its alternate.
54 55 56 57 |
# File 'lib/caruby/helpers/properties.rb', line 54 def delete(key) key = alternate_key(key) unless has_key?(key) super end |
#has_property?(key) ⇒ Boolean
Returns whether the property key or its alternate is defined.
35 36 37 |
# File 'lib/caruby/helpers/properties.rb', line 35 def has_property?(key) has_key?(key) or has_key?(alternate_key(key)) end |
#load_properties(file) ⇒ Object
Loads the specified properties file, replacing any existing properties.
If a key is included in this Properties merge_properties array, then the old value for that key will be merged with the new value for that key rather than replaced.
This method reloads a property file that has already been loaded.
Raises ConfigurationError if file doesn’t exist or couldn’t be parsed.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/caruby/helpers/properties.rb', line 68 def load_properties(file) raise ConfigurationError.new("Properties file not found: #{File.(file)}") unless File.exists?(file) properties = {} begin YAML.load_file(file).each { |key, value| properties[key.to_sym] = value } rescue raise ConfigurationError.new("Could not read properties file #{file}: " + $!) end # Uncomment the following line to print detail properties. #logger.debug { "#{file} properties:\n#{properties.pp_s}" } # parse comma-delimited string values of array properties into arrays @array_properties.each do |key| value = properties[key] if String === value then properties[key] = value.split(/,\s*/) end end # if the key is a merge property key, then perform a deep merge. # otherwise, do a shallow merge of the property value into this property hash. deep, shallow = properties.split { |key, value| @merge_properties.include?(key) } merge!(deep, :deep) merge!(shallow) end |
#symbolize ⇒ Object
Returns a new Hash which associates this Properties’ keys converted to symbols to the respective values.
30 31 32 |
# File 'lib/caruby/helpers/properties.rb', line 30 def symbolize Hash.new end |