Class: ConfigManager::JavaPropertiesLoader
- Inherits:
-
Object
- Object
- ConfigManager::JavaPropertiesLoader
- Defined in:
- lib/configmanager/loaders.rb
Overview
Loads properties from a Java style property file. The syntax is slightly different from regular property files. Important differences are :
-
Escape characters are not allowed.
-
Properties can be assigned a type (int, float, regex, string, bool).
-
Properties can be collected as arrays.
A property would look like : <key><:optional_type> = <value> For arrays, add a [] to the property : <key><:optional_type>[] = <value separated by ‘,’>
Defined Under Namespace
Classes: SyntaxError
Constant Summary collapse
- LINE_REGEX =
/^([\w\.]+)(:(\w+))?(\[\])?\s*([\s=])\s*(.*)$/
- COMMAND_REGEX =
/^@(\w+)\s+(.*)$/
- TRUE_VALUES =
%w(true yes on)
- FALSE_VALUES =
%w(false no off)
- TYPE_CONVERTERS =
{"bool" => :type_bool, "int" => :type_int, "float" => :type_float, "regex" => :type_regex, "string" => :type_string, nil => :type_string}
- COMMANDS =
{"import" => :command_import}
Class Method Summary collapse
-
.command_import(file_to_import, file_path, line_number, configuration) ⇒ NilClass
Executes the import command.
-
.load_properties(file_path, configuration) ⇒ NilClass
Loads properties from the specified Java style property file.
-
.type_bool(value, file_path, line_number) ⇒ TrueClass, FalseClass
Converts the specified string value into a boolean (TrueClass, FalseClass).
-
.type_float(value, file_path, line_number) ⇒ Float
Converts the specified string value into float.
-
.type_int(value, file_path, line_number) ⇒ Integer
Converts the specified string value into a integer.
-
.type_regex(value, file_path, line_number) ⇒ Regexp
Converts the specified string value into a regexp.
-
.type_string(value, file_path, line_number) ⇒ String
Returns the value as is.
Class Method Details
.command_import(file_to_import, file_path, line_number, configuration) ⇒ NilClass
Executes the import command.
63 64 65 66 67 68 69 |
# File 'lib/configmanager/loaders.rb', line 63 def self.command_import(file_to_import, file_path, line_number, configuration) file_to_import = file_to_import[1..-2] file_dir = File.dirname(file_path) file_to_import = File.(file_to_import, file_dir) load_properties(file_to_import, configuration) nil end |
.load_properties(file_path, configuration) ⇒ NilClass
Loads properties from the specified Java style property file.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/configmanager/loaders.rb', line 77 def self.load_properties(file_path, configuration) file_path = File.(file_path) lines = File.readlines(file_path) lines.each_with_index do |line, line_number| line = line.chomp.strip # Empty lines, ignore. # Comment lines, ignore. # Command - Call the command's method. next if line.empty? next if line.start_with?("#") match = line.match(COMMAND_REGEX) if match command_name = match[1] command_arg = match[2] raise SyntaxError.new("Unrecognized command '#{command_name}' #(#{file_path}:#{line_number})") unless COMMANDS.has_key?(command_name) send(COMMANDS[command_name], command_arg, file_path, line_number, configuration) next end # Other lines, parse as a property. # Raises an exception if the line was not valid. match = line.match(LINE_REGEX) raise SyntaxError.new("Syntax error '#{line}' (#{file_path}:#{line_number})") if match.nil? key, type, array, value = match[1], match[3], match[4], match[6] # Invalid types raise an exception. raise SyntaxError.new("Unrecognized type '#{type}' (#{file_path}:#{line_number})") unless TYPE_CONVERTERS.has_key?(type) type_converter = TYPE_CONVERTERS[type] # Split into array if required and apply the type converter. if array value = value.split(",").map { |item| send(type_converter, item.strip, file_path, line_number) } else value = send(type_converter, value, file_path, line_number) end # Finally, add to configuration. configuration.add_property(key, value) end nil end |
.type_bool(value, file_path, line_number) ⇒ TrueClass, FalseClass
Converts the specified string value into a boolean (TrueClass, FalseClass).
124 125 126 127 128 |
# File 'lib/configmanager/loaders.rb', line 124 def self.type_bool(value, file_path, line_number) return true if TRUE_VALUES.include?(value) return false if FALSE_VALUES.include?(value) raise SyntaxError.new("Invalid boolean '#{value}' (#{file_path}:#{line_number})") end |
.type_float(value, file_path, line_number) ⇒ Float
Converts the specified string value into float. Invalid values will raise an exception.
138 139 140 141 142 |
# File 'lib/configmanager/loaders.rb', line 138 def self.type_float(value, file_path, line_number) Float(value) rescue raise SyntaxError.new("Invalid float value '#{value}' (#{file_path}:#{line_number})") end |
.type_int(value, file_path, line_number) ⇒ Integer
Converts the specified string value into a integer. Invalid values will raise an exception
152 153 154 155 156 |
# File 'lib/configmanager/loaders.rb', line 152 def self.type_int(value, file_path, line_number) Integer(value) rescue raise SyntaxError.new("Invalid integer value '#{value}' (#{file_path}:#{line_number})") end |
.type_regex(value, file_path, line_number) ⇒ Regexp
Converts the specified string value into a regexp.
165 166 167 |
# File 'lib/configmanager/loaders.rb', line 165 def self.type_regex(value, file_path, line_number) Regexp.new(value) end |
.type_string(value, file_path, line_number) ⇒ String
Returns the value as is.
176 177 178 |
# File 'lib/configmanager/loaders.rb', line 176 def self.type_string(value, file_path, line_number) value end |