Module: Nugrant::Mixin::Parameters
- Included in:
- Parameters, Vagrant::V2::Config::User
- Defined in:
- lib/nugrant/mixin/parameters.rb
Overview
Mixin module so it’s possible to share parameters logic between default Parameters class and Vagrant implementation.
This module delegates method missing to the final bag hierarchy (@__all). This means that even if the class including this module doesn’t inherit Bag directly, it act exactly like one.
When including this module, you must respect an important constraint.
The including class must call ‘setup!` before starting using parameters retrieval. This is usually performed in the `initialize` method directly but could be in a different place depending on the including class lifecycle. The call to `setup!` is important to initialize all required instance variables.
Here an example where ‘setup!` is called in constructor. Your constructor does not need to have these arguments, they are there as an example.
“‘
def initialize(defaults = {}, config = {}, = {})
setup!(defaults, config, )
end
“‘
Instance Attribute Summary collapse
-
#__all ⇒ Object
readonly
Returns the value of attribute __all.
-
#__config ⇒ Object
readonly
Returns the value of attribute __config.
-
#__current ⇒ Object
readonly
Returns the value of attribute __current.
-
#__defaults ⇒ Object
readonly
Returns the value of attribute __defaults.
-
#__system ⇒ Object
readonly
Returns the value of attribute __system.
-
#__user ⇒ Object
readonly
Returns the value of attribute __user.
Instance Method Summary collapse
- #array_merge_strategy ⇒ Object
-
#array_merge_strategy=(strategy) ⇒ Object
Change the current array merge strategy for this parameters.
- #auto_export ⇒ Object
- #auto_export=(auto_export) ⇒ Object
- #auto_export_script_path ⇒ Object
- #auto_export_script_path=(path) ⇒ Object
-
#compute_all! ⇒ Object
Recompute the correct precedences by merging the various bag in the right order and return the result as a Nugrant::Bag object.
- #defaults ⇒ Object
-
#defaults=(elements) ⇒ Object
Set the new default values for the various parameters contain by this instance.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
-
#setup!(defaults = {}, config = {}, options = {}) ⇒ Object
Setup instance variables of the mixin.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/nugrant/mixin/parameters.rb', line 38 def method_missing(method, *args, &block) case when @__all.class.method_defined?(method) @__all.send(method, *args, &block) else @__all[method] end end |
Instance Attribute Details
#__all ⇒ Object (readonly)
Returns the value of attribute __all.
36 37 38 |
# File 'lib/nugrant/mixin/parameters.rb', line 36 def __all @__all end |
#__config ⇒ Object (readonly)
Returns the value of attribute __config.
36 37 38 |
# File 'lib/nugrant/mixin/parameters.rb', line 36 def __config @__config end |
#__current ⇒ Object (readonly)
Returns the value of attribute __current.
36 37 38 |
# File 'lib/nugrant/mixin/parameters.rb', line 36 def __current @__current end |
#__defaults ⇒ Object (readonly)
Returns the value of attribute __defaults.
36 37 38 |
# File 'lib/nugrant/mixin/parameters.rb', line 36 def __defaults @__defaults end |
#__system ⇒ Object (readonly)
Returns the value of attribute __system.
36 37 38 |
# File 'lib/nugrant/mixin/parameters.rb', line 36 def __system @__system end |
#__user ⇒ Object (readonly)
Returns the value of attribute __user.
36 37 38 |
# File 'lib/nugrant/mixin/parameters.rb', line 36 def __user @__user end |
Instance Method Details
#array_merge_strategy ⇒ Object
47 48 49 |
# File 'lib/nugrant/mixin/parameters.rb', line 47 def array_merge_strategy @__config.array_merge_strategy end |
#array_merge_strategy=(strategy) ⇒ Object
Change the current array merge strategy for this parameters.
63 64 65 66 67 68 69 70 |
# File 'lib/nugrant/mixin/parameters.rb', line 63 def array_merge_strategy=(strategy) return if not Nugrant::Config.supported_array_merge_strategy(strategy) @__config.array_merge_strategy = strategy # When array_merge_strategy change, we need to recompute parameters hierarchy compute_all!() end |
#auto_export ⇒ Object
51 52 53 |
# File 'lib/nugrant/mixin/parameters.rb', line 51 def auto_export @__config.auto_export end |
#auto_export=(auto_export) ⇒ Object
72 73 74 |
# File 'lib/nugrant/mixin/parameters.rb', line 72 def auto_export=(auto_export) @__config.auto_export = auto_export end |
#auto_export_script_path ⇒ Object
55 56 57 |
# File 'lib/nugrant/mixin/parameters.rb', line 55 def auto_export_script_path @__config.auto_export_script_path end |
#auto_export_script_path=(path) ⇒ Object
76 77 78 |
# File 'lib/nugrant/mixin/parameters.rb', line 76 def auto_export_script_path=(path) @__config.auto_export_script_path = path end |
#compute_all! ⇒ Object
Recompute the correct precedences by merging the various bag in the right order and return the result as a Nugrant::Bag object.
169 170 171 172 173 174 175 |
# File 'lib/nugrant/mixin/parameters.rb', line 169 def compute_all!() @__all = Bag.new({}, @__config) @__all.merge!(@__defaults) @__all.merge!(@__system) @__all.merge!(@__user) @__all.merge!(@__current) end |
#defaults ⇒ Object
80 81 82 |
# File 'lib/nugrant/mixin/parameters.rb', line 80 def defaults() @__defaults end |
#defaults=(elements) ⇒ Object
Set the new default values for the various parameters contain by this instance. This will call ‘compute_all!` to recompute correct precedences.
| Attributes
* +elements+ - The new default elements
93 94 95 96 97 98 |
# File 'lib/nugrant/mixin/parameters.rb', line 93 def defaults=(elements) @__defaults = Bag.new(elements, @__config) # When defaults change, we need to recompute parameters hierarchy compute_all!() end |
#merge(other) ⇒ Object
100 101 102 103 |
# File 'lib/nugrant/mixin/parameters.rb', line 100 def merge(other) result = dup() result.merge!(other) end |
#merge!(other) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/nugrant/mixin/parameters.rb', line 105 def merge!(other) @__config.merge!(other.__config) # Updated Bags' config @__current.config = @__config @__user.config = @__config @__system.config = @__config @__defaults.config = @__config # Merge other into Bags @__current.merge!(other.__current, :array_merge_strategy => :replace) @__user.merge!(other.__user, :array_merge_strategy => :replace) @__system.merge!(other.__system, :array_merge_strategy => :replace) @__defaults.merge!(other.__defaults, :array_merge_strategy => :replace) # Recompute all from merged Bags compute_all!() self end |
#setup!(defaults = {}, config = {}, options = {}) ⇒ Object
Setup instance variables of the mixin. It will compute all parameters bags (current, user, system, default and all) and stored them to these respective instance variables:
* @__current
* @__user
* @__system
* @__defaults
| Arguments
* `defaults`
A hash that is used as the initial data for the defaults bag. Defaults
to an empty hash.
* `config`
A Nugrant::Config object or hash passed to Nugrant::Config
convert method. Used to determine where to find the various
bag data sources and other configuration options.
Passed to nested structures that requires a Nugrant::Config object
like the Bag object and Helper::Bag module.
* `options`
Options hash used by this method exclusively. No options yet, added
for future improvements.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/nugrant/mixin/parameters.rb', line 153 def setup!(defaults = {}, config = {}, = {}) @__config = Nugrant::Config::convert(config); @__defaults = Bag.new(defaults, @__config) @__current = Helper::Bag.read(@__config.current_path, @__config.params_format, @__config) @__user = Helper::Bag.read(@__config.user_path, @__config.params_format, @__config) @__system = Helper::Bag.read(@__config.system_path, @__config.params_format, @__config) compute_all!() end |