Class: ConfigManager::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/configmanager/configuration.rb

Overview

Represents a collection of properties stored as a hash.

Direct Known Subclasses

CombinedConfiguration

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Creates a new empty configuration.

Parameters:

  • options (Hash) (defaults to: {})

    configuration options. Supported keys are: :interpolator - an object that can respond to the ‘interpolate’ method. Default is the DefaultInterpolator. :use_interpolator - global flag to specify if the interpolator should be used for this configuration. Default is true.



18
19
20
21
22
# File 'lib/configmanager/configuration.rb', line 18

def initialize(options = {})
  @root = {}
  @use_interpolator = get_option(options, :use_interpolator, false, true)
  @interpolator = get_option(options, :interpolator, false, ConfigManager::DefaultInterpolator)
end

Instance Method Details

#add_properties(values, at = "", options = {}) ⇒ NilClass

Adds a hash of properties to this configuration.

Parameters:

  • values (Hash)

    the values to add to this configuration.

  • at (String) (defaults to: "")

    the node under which to add these properties. Default is the root node.

  • options (Hash) (defaults to: {})

    configuration options. Supported key are :overwrite - true to overwrite existing properties, false to raise an exception if a property exists. Default is false.

Returns:

  • (NilClass)


32
33
34
35
36
37
38
39
40
41
# File 'lib/configmanager/configuration.rb', line 32

def add_properties(values, at = "", options = {})
  # Add each item in the hash - Nested hashes call this method recursively.
  at_array = at.split(".")
  values.each_pair do |key, value|
    full_key = (at_array + [key]).join(".")
    value.kind_of?(Hash) ? add_properties(value, full_key, options) : add_property(full_key, value, options)
  end

  nil
end

#add_property(key, value, options = {}) ⇒ NilClass Also known as: []=, set_property

Adds a property to this configuration. The key is a ‘.’ separated string that fully qualifies this property. So the property a.b identifies the property ‘b’ inside the group ‘a’. Similarly a.b.c identifies the property ‘c’ inside the group ‘b’ which is inside the group ‘a’.

Parameters:

  • key (String)

    the unique key to identify this property.

  • value (Object)

    the value of this property.

  • options (Hash) (defaults to: {})

    configuration options. Supported keys are: :overwrite - true to overwrite existing properties, false to raise an exception if a property exists. Default is false. :at - an optional node’s name under which to add this property. Default is the root node.

Returns:

  • (NilClass)

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/configmanager/configuration.rb', line 54

def add_property(key, value, options = {})
  # Default options.
  at = get_option(options, :at, false, "")
  overwrite = get_option(options, :overwrite, false, false)
  # Create and add the property.
  key_array = key.split(".")
  at_array = at.split(".")
  leaf = key_array[-1]
  at_array = at_array + key_array[0..-2]
  at_node = get_node(at_array, create = true)
  # Got the node, now add the property, unless it already exists.
  raise ConfigManager::KeyError.new("Cannot add - '#{at_array.join(".")}.#{leaf}' already exists!") if at_node.has_key?(leaf) and not overwrite
  at_node[leaf] = value

  nil
end

#get_property(key, options = {}) ⇒ Object Also known as: []

Get the value of the specified property. The key is a ‘.’ separated string that fully qualifies this property. So the property a.b identifies the property ‘b’ inside the group ‘a’. Similarly a.b.c identifies the property ‘c’ inside the group ‘b’ which is inside the group ‘a’.

If enabled, the property’s value is interpolated. Finally the list of post-processors are invoked and the value is returned.

Note : Interpolator implementations may support additional options too.

Parameters:

  • key (String)

    the property to get.

  • options (Hash) (defaults to: {})

    configuration options. Supported keys are: :use_interpolator - flag to indicate if the interpolator should be used. Uses the value specified when creating this configuration by default. :interpolator - the interpolator to use. Uses the value specified when creating this configuration by default. :post_processors - an array of callable procs. Each one is invoked with the output of the previous one. The first one is invoked with the fully interpolated value. :replacements - a hash containing replacement values for references. References are looked up in this before being looked up in this configuration. :tolerate_missing_references: a flag to indicate if missing references should raise an exception.

Returns:

  • (Object)

    the property’s value.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/configmanager/configuration.rb', line 89

def get_property(key, options = {})
  use_interpolator = get_option(options, :use_interpolator, false, @use_interpolator)
  interpolator = get_option(options, :interpolator, false, @interpolator)
  post_processors = pop_option(options, :post_processors, false, [])
  # Get the required property's raw value.
  key_array = key.split(".")
  create = false
  raw_value = get_node(key_array, create)
  # Interpolate.
  raw_value = interpolator.interpolate(key, raw_value, self, options) if use_interpolator
  # Post-process.
  final_value = raw_value
  post_processors.each { |processor| final_value = processor.call(final_value) }
  # All done.
  final_value
end

#has_property?(key) ⇒ TrueClass, FalseClass

Check to see if the specified property is defined for this configuration. This method will throw an exception if the key is invalid.

Parameters:

  • key (String)

    the property to check.

Returns:

  • (TrueClass, FalseClass)

    true if the property exists, false otherwise.



112
113
114
115
116
117
118
119
# File 'lib/configmanager/configuration.rb', line 112

def has_property?(key)
  key_array = key.split(".")
  create = false
  get_node(key_array, create)
  return true
rescue ConfigManager::PropertyNotFoundError
  return false
end