Module: Mixlib::CLI::InheritMethods

Defined in:
lib/mixlib/cli.rb

Instance Method Summary collapse

Instance Method Details

#deep_dup(object) ⇒ Object

object

Instance to clone

This method will return a “deep clone” of the provided ‘object`. If the provided `object` is an enumerable type the contents will be iterated and cloned as well.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mixlib/cli.rb', line 53

def deep_dup(object)
  cloned_object = object.respond_to?(:dup) ? object.dup : object
  if cloned_object.is_a?(Enumerable)
    if cloned_object.is_a?(Hash)
      new_hash = cloned_object.class.new
      cloned_object.each do |key, value|
        cloned_key = deep_dup(key)
        cloned_value = deep_dup(value)
        new_hash[cloned_key] = cloned_value
      end
      cloned_object.replace(new_hash)
    else
      cloned_object.map! do |shallow_instance|
        deep_dup(shallow_instance)
      end
    end
  end
  cloned_object
rescue TypeError
  # Symbol will happily provide a `#dup` method even though
  # attempts to clone it will result in an exception (atoms!).
  # So if we run into an issue of TypeErrors, just return the
  # original object as we gave our "best effort"
  object
end

#inherited(receiver) ⇒ Object



44
45
46
47
# File 'lib/mixlib/cli.rb', line 44

def inherited(receiver)
  receiver.options = deep_dup(options)
  receiver.extend(Mixlib::CLI::InheritMethods)
end