Class: Nugrant::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/nugrant/config.rb

Constant Summary collapse

DEFAULT_ARRAY_MERGE_STRATEGY =
:replace
DEFAULT_PARAMS_FILENAME =
".nuparams"
DEFAULT_PARAMS_FORMAT =
:yaml
DEFAULT_AUTO_EXPORT =
false
SUPPORTED_ARRAY_MERGE_STRATEGIES =
[:concat, :extend, :replace]
SUPPORTED_PARAMS_FORMATS =
[:json, :yaml]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Creates a new config object that is used to configure an instance of Nugrant::Parameters. See the list of options and how they interact with Nugrant::Parameters.

| Options

* +:params_filename+ - The filename used to fetch parameters from. This
                       will be appended to various default locations.
                       Location are system, project and current that
                       can be tweaked individually by using the options
                       below.
                         Defaults => ".nuparams"
* +:params_format+   - The format in which parameters are to be parsed.
                       Presently supporting :yaml and :json.
                         Defaults => :yaml
* +:current_path+    - The current path has the highest precedence over
                       any other location. It can be be used for many purposes
                       depending on your usage.
                        * A path from where to read project parameters
                        * A path from where to read overriding parameters for a cli tool
                        * A path from where to read user specific settings not to be committed in a VCS
                         Defaults => "./#{@params_filename}"
* +:user_path+       - The user path is the location where the user
                       parameters should resides. The parameters loaded from this
                       location have the second highest precedence.
                         Defaults => "~/#{@params_filename}"
* +:system_path+     - The system path is the location where system wide
                       parameters should resides. The parameters loaded from this
                       location have the third highest precedence.
                         Defaults => Default system path depending on OS + @params_filename
* +:array_merge_strategy+  - This option controls how array values are merged together when merging
                             two Bag instances. Possible values are:
                               * :replace => Replace current values by new ones
                               * :extend => Merge current values with new ones
                               * :concat => Append new values to current ones
                              Defaults => The strategy :replace.
* +:key_error+     - A callback method receiving one argument, the key as a symbol, and that
                     deal with the error. If the callable does not
                     raise an exception, the result of it's execution is returned.
                       Defaults => A callable that throws a KeyError exception.
* +:parse_error+   - A callback method receiving two arguments, the offending filename and
                     the error object, that deal with the error. If the callable does not
                     raise an exception, the result of it's execution is returned.
                       Defaults => A callable that returns the empty hash.
* +:auto_export+               - Automatically export configuration to the specified format :autoenv or :script
* +:auto_export_script_path+   - The path where to write the export script, defaults to "./nugrant2env.sh"


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/nugrant/config.rb', line 134

def initialize(options = {})
  @params_filename = options[:params_filename] || DEFAULT_PARAMS_FILENAME
  @params_format = options[:params_format] || DEFAULT_PARAMS_FORMAT
  @auto_export = options[:auto_export] || DEFAULT_AUTO_EXPORT
  @auto_export_script_path = options[:auto_export_script_path] || false # use default

  @current_path = Config.fixup_path(options[:current_path], ".", @params_filename)
  @user_path = Config.fixup_path(options[:user_path], Config.default_user_path(), @params_filename)
  @system_path = Config.fixup_path(options[:system_path], Config.default_system_path(), @params_filename)

  @array_merge_strategy = options[:array_merge_strategy] || :replace

  @key_error = options[:key_error] || Proc.new do |key|
    raise KeyError, "Undefined parameter '#{key}'"
  end

  @parse_error = options[:parse_error] || Proc.new do |filename, error|
    {}
  end

  validate()
end

Instance Attribute Details

#array_merge_strategyObject

Returns the value of attribute array_merge_strategy.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def array_merge_strategy
  @array_merge_strategy
end

#auto_exportObject

Returns the value of attribute auto_export.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def auto_export
  @auto_export
end

#auto_export_script_pathObject

Returns the value of attribute auto_export_script_path.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def auto_export_script_path
  @auto_export_script_path
end

#current_pathObject (readonly)

Returns the value of attribute current_path.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def current_path
  @current_path
end

#key_errorObject (readonly)

Returns the value of attribute key_error.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def key_error
  @key_error
end

#params_filenameObject (readonly)

Returns the value of attribute params_filename.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def params_filename
  @params_filename
end

#params_formatObject (readonly)

Returns the value of attribute params_format.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def params_format
  @params_format
end

#parse_errorObject (readonly)

Returns the value of attribute parse_error.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def parse_error
  @parse_error
end

#system_pathObject (readonly)

Returns the value of attribute system_path.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def system_path
  @system_path
end

#user_pathObject (readonly)

Returns the value of attribute user_path.



13
14
15
# File 'lib/nugrant/config.rb', line 13

def user_path
  @user_path
end

Class Method Details

.convert(config = {}) ⇒ Object

Convenience method to easily accept either a hash that will be converted to a Nugrant::Config object or directly a config object.



24
25
26
# File 'lib/nugrant/config.rb', line 24

def self.convert(config = {})
  return config.kind_of?(Nugrant::Config) ? config : Nugrant::Config.new(config)
end

.default_system_pathObject

Return the fully expanded path of the system parameters default location that is used in the constructor.



40
41
42
43
44
45
46
# File 'lib/nugrant/config.rb', line 40

def self.default_system_path()
  if Config.on_windows?
    return File.expand_path(ENV['PROGRAMDATA'] || ENV['ALLUSERSPROFILE'])
  end

  "/etc"
end

.default_user_pathObject

Return the fully expanded path of the user parameters default location that is used in the constructor.



32
33
34
# File 'lib/nugrant/config.rb', line 32

def self.default_user_path()
  File.expand_path("~")
end

.fixup_path(path, default, params_filename) ⇒ Object

Method to fix-up a received path. The fix-up do the follows the following rules:

  1. If the path is callable, call it to get the value.

  2. If value is nil, return default value.

  3. If value is a directory, return path + params_filename to it.

  4. Otherwise, return value

Parameters:

  • path

    The path parameter received.

  • default

    The default path to use, can be a directory.

  • params_filename

    The params filename to append if path is a directory

Returns:

  • The fix-up path following rules defined above.



63
64
65
66
67
68
69
70
# File 'lib/nugrant/config.rb', line 63

def self.fixup_path(path, default, params_filename)
  path = path.call if path.respond_to?(:call)

  path = File.expand_path(path || default)
  path = "#{path}/#{params_filename}" if ::File.directory?(path)

  path
end

.on_windows?Boolean

Return true if we are currently on a Windows platform.

Returns:

  • (Boolean)


83
84
85
# File 'lib/nugrant/config.rb', line 83

def self.on_windows?()
  (RbConfig::CONFIG['host_os'].downcase =~ /mswin|mingw|cygwin/) != nil
end

.supported_array_merge_strategy(strategy) ⇒ Object



72
73
74
# File 'lib/nugrant/config.rb', line 72

def self.supported_array_merge_strategy(strategy)
  SUPPORTED_ARRAY_MERGE_STRATEGIES.include?(strategy)
end

.supported_params_format(format) ⇒ Object



76
77
78
# File 'lib/nugrant/config.rb', line 76

def self.supported_params_format(format)
  SUPPORTED_PARAMS_FORMATS.include?(format)
end

Instance Method Details

#==(other) ⇒ Object



157
158
159
160
161
162
# File 'lib/nugrant/config.rb', line 157

def ==(other)
  self.class.equal?(other.class) &&
  instance_variables.all? do |variable|
    instance_variable_get(variable) == other.instance_variable_get(variable)
  end
end

#[](key) ⇒ Object



164
165
166
167
168
# File 'lib/nugrant/config.rb', line 164

def [](key)
  instance_variable_get("@#{key}")
rescue
  nil
end

#merge(other) ⇒ Object



170
171
172
173
# File 'lib/nugrant/config.rb', line 170

def merge(other)
  result = dup()
  result.merge!(other)
end

#merge!(other) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/nugrant/config.rb', line 175

def merge!(other)
  other.instance_variables.each do |variable|
    instance_variable_set(variable, other.instance_variable_get(variable)) if instance_variables.include?(variable)
  end

  self
end

#to_hObject Also known as: to_hash



183
184
185
186
187
# File 'lib/nugrant/config.rb', line 183

def to_h()
  Hash[instance_variables.map do |variable|
    [variable[1..-1].to_sym, instance_variable_get(variable)]
  end]
end

#validateObject

Raises:

  • (ArgumentError)


191
192
193
194
195
196
197
198
199
# File 'lib/nugrant/config.rb', line 191

def validate()
  raise ArgumentError,
    "Invalid value for :params_format. \
     The format [#{@params_format}] is currently not supported." if not Config.supported_params_format(@params_format)

  raise ArgumentError,
      "Invalid value for :array_merge_strategy. \
       The array merge strategy [#{@array_merge_strategy}] is currently not supported." if not Config.supported_array_merge_strategy(@array_merge_strategy)
end