lib/leap_cli/config/object.rb in leap_cli-1.7.4 vs lib/leap_cli/config/object.rb in leap_cli-1.8

- old
+ new

@@ -10,68 +10,52 @@ module LeapCli module Config # - # A proxy for Manager that binds to a particular object - # (so that we can bind to a particular environment) - # - class ManagerBinding - def initialize(manager, object) - @manager = manager - @object = object - end - - def services - @manager.env(@object.environment).services - end - - def tags - @manager.env(@object.environment).tags - end - - def provider - @manager.env(@object.environment).provider - end - - def method_missing(*args) - @manager.send(*args) - end - end - - # # This class represents the configuration for a single node, service, or tag. # Also, all the nested hashes are also of this type. # # It is called 'object' because it corresponds to an Object in JSON. # class Object < Hash + attr_reader :env attr_reader :node - def initialize(manager=nil, node=nil) - # keep a global pointer around to the config manager. used a lot in the eval strings and templates - # (which are evaluated in the context of Config::Object) - @manager = manager - - # an object that is a node as @node equal to self, otherwise all the child objects point back to the top level node. + def initialize(environment=nil, node=nil) + raise ArgumentError unless environment.nil? || environment.is_a?(Config::Environment) + @env = environment + # an object that is a node as @node equal to self, otherwise all the + # child objects point back to the top level node. @node = node || self end def manager - ManagerBinding.new(@manager, self) + @env.manager end - alias :global :manager + # + # TODO: deprecate node.global() + # + def global + @env + end + def environment=(e) self.store('environment', e) end def environment self['environment'] end + def duplicate(env) + new_object = self.deep_dup + new_object.set_environment(env, new_object) + end + # # export YAML # # We use pure ruby yaml exporter ya2yaml instead of SYCK or PSYCH because it # allows us greater compatibility regardless of installed ruby version and @@ -83,16 +67,27 @@ end # # export JSON # - def dump_json(*options) + def dump_json(options={}) evaluate(@node) - if options.include? :compact - self.to_json + if options[:format] == :compact + return self.to_json else - JSON.sorted_generate(self) + excluded = {} + if options[:exclude] + options[:exclude].each do |key| + excluded[key] = self[key] + self.delete(key) + end + end + json_str = JSON.sorted_generate(self) + if excluded.any? + self.merge!(excluded) + end + return json_str end end def evaluate(context=@node) evaluate_everything(context) @@ -184,10 +179,14 @@ self.delete('+'+key) elsif self.has_key?('-'+key) mode = :subtract old_value = self.fetch '-'+key, nil self.delete('-'+key) + elsif self.has_key?('!'+key) + mode = :replace + old_value = self.fetch '!'+key, nil + self.delete('!'+key) else mode = :normal old_value = self.fetch key, nil end @@ -195,13 +194,17 @@ new_value = true if new_value == "true" new_value = false if new_value == "false" old_value = true if old_value == "true" old_value = false if old_value == "false" + # force replace? + if mode == :replace && prefer_self + value = old_value + # merge hashes - if old_value.is_a?(Hash) || new_value.is_a?(Hash) - value = Config::Object.new(@manager, @node) + elsif old_value.is_a?(Hash) || new_value.is_a?(Hash) + value = Config::Object.new(@env, @node) old_value.is_a?(Hash) ? value.deep_merge!(old_value) : (value[key] = old_value if !old_value.nil?) new_value.is_a?(Hash) ? value.deep_merge!(new_value, prefer_self) : (value[key] = new_value if !new_value.nil?) # merge nil elsif new_value.nil? @@ -248,10 +251,20 @@ self[key] = value end self end + def set_environment(env, node) + @env = env + @node = node + self.each do |key, value| + if value.is_a?(Config::Object) + value.set_environment(env, node) + end + end + end + # # like a reverse deep merge # (self takes precedence) # def inherit_from!(object) @@ -284,11 +297,11 @@ # def evaluate_everything(context) keys.each do |key| obj = fetch_value(key, context) if is_required_value_not_set?(obj) - Util::log 0, :warning, "required key \"#{key}\" is not set in node \"#{node.name}\"." + Util::log 0, :warning, "required property \"#{key}\" is not set in node \"#{node.name}\"." elsif obj.is_a? Config::Object obj.evaluate_everything(context) end end end @@ -299,10 +312,10 @@ def late_evaluate_everything(context) if @late_eval_list @late_eval_list.each do |key, value| self[key] = context.evaluate_ruby(key, value) if is_required_value_not_set?(self[key]) - Util::log 0, :warning, "required key \"#{key}\" is not set in node \"#{node.name}\"." + Util::log 0, :warning, "required property \"#{key}\" is not set in node \"#{node.name}\"." end end end values.each do |obj| if obj.is_a? Config::Object \ No newline at end of file