Class: Nugrant::Config
- Inherits:
-
Object
- Object
- Nugrant::Config
- Defined in:
- lib/nugrant/config.rb
Constant Summary collapse
- DEFAULT_ARRAY_MERGE_STRATEGY =
:replace- DEFAULT_PARAMS_FILENAME =
".nuparams"- DEFAULT_PARAMS_FORMAT =
:yaml- SUPPORTED_ARRAY_MERGE_STRATEGIES =
[:concat, :extend, :replace]
- SUPPORTED_PARAMS_FORMATS =
[:json, :yaml]
Instance Attribute Summary collapse
-
#array_merge_strategy ⇒ Object
Returns the value of attribute array_merge_strategy.
-
#current_path ⇒ Object
readonly
Returns the value of attribute current_path.
-
#key_error ⇒ Object
readonly
Returns the value of attribute key_error.
-
#params_filename ⇒ Object
readonly
Returns the value of attribute params_filename.
-
#params_format ⇒ Object
readonly
Returns the value of attribute params_format.
-
#parse_error ⇒ Object
readonly
Returns the value of attribute parse_error.
-
#system_path ⇒ Object
readonly
Returns the value of attribute system_path.
-
#user_path ⇒ Object
readonly
Returns the value of attribute user_path.
Class Method Summary collapse
-
.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.
-
.default_system_path ⇒ Object
Return the fully expanded path of the system parameters default location that is used in the constructor.
-
.default_user_path ⇒ Object
Return the fully expanded path of the user parameters default location that is used in the constructor.
-
.fixup_path(path, default, params_filename) ⇒ Object
Method to fix-up a received path.
-
.on_windows? ⇒ Boolean
Return true if we are currently on a Windows platform.
- .supported_array_merge_strategy(strategy) ⇒ Object
- .supported_params_format(format) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
-
#initialize(options = {}) ⇒ Config
constructor
Creates a new config object that is used to configure an instance of Nugrant::Parameters.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
- #validate ⇒ Object
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.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/nugrant/config.rb', line 131 def initialize( = {}) @params_filename = [:params_filename] || DEFAULT_PARAMS_FILENAME @params_format = [:params_format] || DEFAULT_PARAMS_FORMAT @current_path = Config.fixup_path([:current_path], ".", @params_filename) @user_path = Config.fixup_path([:user_path], Config.default_user_path(), @params_filename) @system_path = Config.fixup_path([:system_path], Config.default_system_path(), @params_filename) @array_merge_strategy = [:array_merge_strategy] || :replace @key_error = [:key_error] || Proc.new do |key| raise KeyError, "Undefined parameter '#{key}'" end @parse_error = [:parse_error] || Proc.new do |filename, error| {} end validate() end |
Instance Attribute Details
#array_merge_strategy ⇒ Object
Returns the value of attribute array_merge_strategy.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 def array_merge_strategy @array_merge_strategy end |
#current_path ⇒ Object (readonly)
Returns the value of attribute current_path.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 def current_path @current_path end |
#key_error ⇒ Object (readonly)
Returns the value of attribute key_error.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 def key_error @key_error end |
#params_filename ⇒ Object (readonly)
Returns the value of attribute params_filename.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 def params_filename @params_filename end |
#params_format ⇒ Object (readonly)
Returns the value of attribute params_format.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 def params_format @params_format end |
#parse_error ⇒ Object (readonly)
Returns the value of attribute parse_error.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 def parse_error @parse_error end |
#system_path ⇒ Object (readonly)
Returns the value of attribute system_path.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 def system_path @system_path end |
#user_path ⇒ Object (readonly)
Returns the value of attribute user_path.
12 13 14 |
# File 'lib/nugrant/config.rb', line 12 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.
23 24 25 |
# File 'lib/nugrant/config.rb', line 23 def self.convert(config = {}) return config.kind_of?(Nugrant::Config) ? config : Nugrant::Config.new(config) end |
.default_system_path ⇒ Object
Return the fully expanded path of the system parameters default location that is used in the constructor.
39 40 41 42 43 44 45 |
# File 'lib/nugrant/config.rb', line 39 def self.default_system_path() if Config.on_windows? return File.(ENV['PROGRAMDATA'] || ENV['ALLUSERSPROFILE']) end "/etc" end |
.default_user_path ⇒ Object
Return the fully expanded path of the user parameters default location that is used in the constructor.
31 32 33 |
# File 'lib/nugrant/config.rb', line 31 def self.default_user_path() File.("~") end |
.fixup_path(path, default, params_filename) ⇒ Object
Method to fix-up a received path. The fix-up do the follows the following rules:
-
If the path is callable, call it to get the value.
-
If value is nil, return default value.
-
If value is a directory, return path + params_filename to it.
-
Otherwise, return value
62 63 64 65 66 67 68 69 |
# File 'lib/nugrant/config.rb', line 62 def self.fixup_path(path, default, params_filename) path = path.call if path.respond_to?(:call) path = File.(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.
82 83 84 |
# File 'lib/nugrant/config.rb', line 82 def self.on_windows?() (RbConfig::CONFIG['host_os'].downcase =~ /mswin|mingw|cygwin/) != nil end |
.supported_array_merge_strategy(strategy) ⇒ Object
71 72 73 |
# File 'lib/nugrant/config.rb', line 71 def self.supported_array_merge_strategy(strategy) SUPPORTED_ARRAY_MERGE_STRATEGIES.include?(strategy) end |
.supported_params_format(format) ⇒ Object
75 76 77 |
# File 'lib/nugrant/config.rb', line 75 def self.supported_params_format(format) SUPPORTED_PARAMS_FORMATS.include?(format) end |
Instance Method Details
#==(other) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/nugrant/config.rb', line 152 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
159 160 161 162 163 |
# File 'lib/nugrant/config.rb', line 159 def [](key) instance_variable_get("@#{key}") rescue nil end |
#merge(other) ⇒ Object
165 166 167 168 |
# File 'lib/nugrant/config.rb', line 165 def merge(other) result = dup() result.merge!(other) end |
#merge!(other) ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/nugrant/config.rb', line 170 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_h ⇒ Object Also known as: to_hash
178 179 180 181 182 |
# File 'lib/nugrant/config.rb', line 178 def to_h() Hash[instance_variables.map do |variable| [variable[1..-1].to_sym, instance_variable_get(variable)] end] end |
#validate ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/nugrant/config.rb', line 186 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 |