Class: Utils::Property
- Inherits:
-
Object
- Object
- Utils::Property
- Defined in:
- lib/utils/properties.rb
Overview
Represents a single property value from a properties file.
Constant Summary collapse
- REFERENCE_REGEX =
/\$\{[^}]+}/
- @@logger =
LogEasy::Logger.get_logger(name)
Instance Attribute Summary collapse
-
#evaluated_value ⇒ Object
readonly
Returns the value of attribute evaluated_value.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#evaluate(force_evaluate = false) ⇒ Object
Each reference present in this property is passed to the given block.
-
#initialize(name, value) ⇒ Property
constructor
Creates a new property.
Constructor Details
#initialize(name, value) ⇒ Property
Creates a new property.
142 143 144 145 146 147 148 |
# File 'lib/utils/properties.rb', line 142 def initialize(name, value) @name = name @value = value @evaluated_value = value @evaluating = false @evaluated = false end |
Instance Attribute Details
#evaluated_value ⇒ Object
Returns the value of attribute evaluated_value.
139 140 141 |
# File 'lib/utils/properties.rb', line 139 def evaluated_value @evaluated_value end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
137 138 139 |
# File 'lib/utils/properties.rb', line 137 def name @name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
138 139 140 |
# File 'lib/utils/properties.rb', line 138 def value @value end |
Instance Method Details
#evaluate(force_evaluate = false) ⇒ Object
Each reference present in this property is passed to the given block. If no block is given the method returns immediately. The method will return immediately if this property has already been evaluated too.
If a block is given its result replaces each reference. A false may be returned to keep the original reference as is.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/utils/properties.rb', line 155 def evaluate(force_evaluate = false) if not force_evaluate and self.evaluated # Property has already been evaluated. Return immediately. # The property has been evaluated. Do not evaluate again. @@logger.fine("Already evaluated '#{name}'.") elsif self.evaluating # The property is currently being evaluated and has been referenced again. Circular reference. raise "Circular reference detected starting at property #{name}=#{value}." elsif block_given? # This property is being evaluated. If a call to evaluate is made while evaluating is true an exception is raised. @@logger.fine("Evaluating '#{name}'...") self.evaluating = true self.evaluated_value = value.gsub(REFERENCE_REGEX) do |match| # Extract the key from the ${key} match and pass it to the block. If there is no block then do nothing. key = match[2..-2].to_sym @@logger.fine("Found reference '#{key}' in '#{name}'.") replacement = yield key # Return the original match as is if the replacement value is false. next match if not replacement # Otherwise return the replacement. @@logger.fine("Replacing '#{key}' in '#{name}' with '#{replacement}'.") next replacement end if value.instance_of?(String) # Only do this for Strings. # The property has been evaluated. Do not evaluate again. @@logger.debug("Final value for '#{name}' is '#{evaluated_value}'.") self.evaluating = false self.evaluated = true end end |