Class: Utils::Properties
- Inherits:
-
Object
- Object
- Utils::Properties
- Defined in:
- lib/utils/properties.rb
Overview
Utility class that provides the ability to load java style properties files. The files can also contain references to any property using the $key format. References are evaluated only when a property’s value is requested.
Example:
Load property files. p = Utils::Properties.load_from_file(“example.properties”, true) Utils::Properties.load_from_file(“override.properties”, true, p) # Override…
Adds the property that was referenced in example.properties. Since this is not a String, it will not be eligible for evaluation. It will still be used as a reference though. p.add_property(“time_now”, Time.now)
Configuring the root logger so that trace information can be seen. console_appender_2 = LogEasy::ConsoleAppender.new # Simple console appender. console_appender_2.formatter = LogEasy::Appender::FULL_LOGGER_NAME_FORMATTER root_logger = LogEasy::Logger.root_logger # Get the handle to the root logger. The root logger’s default level is ALL. root_logger.add_appender(console_appender_2) # Add the appenders to the root logger.
Access the property values. full_name = p.full_name author = p.author information = p.information
Print the information. puts “App: #full_name” puts “Author: #author” puts “Built: #information”
Constant Summary collapse
- @@logger =
LogEasy::Logger.get_logger(name)
Class Method Summary collapse
-
.load_from_file(file_path, evaluate_properties = true, existing_properties = Properties.new(evaluate_properties)) ⇒ Object
Creates a new Properties object by loading the specified file.
Instance Method Summary collapse
-
#add_property(name, value) ⇒ Object
Saves a properties value.
-
#evaluate_all ⇒ Object
Force evaluation of all properties right now.
-
#get(key, force_evaluate = false, evaluate_property = evaluate_properties) ⇒ Object
Gets the value (as a String) of a property.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object (private)
Property values can be accessed directly using the name of the property. i.e. p.app_name (where app_name is the name of the property).
123 124 125 126 |
# File 'lib/utils/properties.rb', line 123 def method_missing(symbol, *args, &block) # Use 'get' to get the property's value. It will raise an exception if it does not exist. get(symbol, *args) end |
Class Method Details
.load_from_file(file_path, evaluate_properties = true, existing_properties = Properties.new(evaluate_properties)) ⇒ Object
Creates a new Properties object by loading the specified file. Optionally an existing properties object can be passed in to put the loaded properties into. Existing properties with the same key will be overwritten. Also the evaluate properties flag can be turned off if it is not required.
Comments (lines starting with ‘#’) and empty lines are skipped.
Returns => Properties
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 78 |
# File 'lib/utils/properties.rb', line 50 def Properties.load_from_file(file_path, evaluate_properties = true, existing_properties = Properties.new(evaluate_properties)) raise "Properties file not found!" if not File.exists?(file_path) # Read each line, check if it is empty, a comment or starts with an =. If it does then skip it and go to the next line. File.open(file_path, 'r') do |file| line_number = 0 file.read.each_line do |line| line.strip! line_number += 1 # Skip empty / comment lines. next if (line.length == 0 or line[0] == ?#) # Exception for invalid first character. raise "Invalid property format on line #{line_number}." if line[0] == ?= i = line.index("=") if i # Extract the key and value. name = line[0..i - 1].strip value = line[i + 1..-1].strip @@logger.trace("Property '#{name}=#{value} added.") existing_properties.add_property(name, value) else # Not a key-value pair. raise "Invalid property format on line #{line_number}." end end end # Return the updated / new properties. existing_properties end |
Instance Method Details
#add_property(name, value) ⇒ Object
Saves a properties value. If a property with this name already exists, it is replaced with this one. Its evaluated flag is also reset. The name is converted to a Symbol and used as the key.
82 83 84 85 86 |
# File 'lib/utils/properties.rb', line 82 def add_property(name, value) property = Property.new(name, value) key = name.to_sym props[key] = property end |
#evaluate_all ⇒ Object
Force evaluation of all properties right now.
89 90 91 |
# File 'lib/utils/properties.rb', line 89 def evaluate_all props.each_key { |key| get(key, true) } end |
#get(key, force_evaluate = false, evaluate_property = evaluate_properties) ⇒ Object
Gets the value (as a String) of a property. The key used must be a Symbol. If ‘force_evaluate’ is set to true, the property (and all referenced properties) will be re-evaluated. If ‘evaluate_property’ is set to true all nested references will be evaluated even if the global ‘evaluate_properties’ flag is false.
Returns => String
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/utils/properties.rb', line 98 def get(key, force_evaluate = false, evaluate_property = evaluate_properties) raise "Property '#{key}' not found!" if not props.has_key?(key) # The property exists. Get it. Evaluate it if required. property = props[key] property.evaluate(force_evaluate) do |ref_key| next false if not props.has_key?(ref_key) next get(ref_key, force_evaluate, evaluate_property) end if evaluate_property # Return the value of the fully evaluated property. property.evaluated_value end |