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.


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

def deep_dup(object)
  cloned_object = object.respond_to?(:dup) ? object.dup : object
  if cloned_object.kind_of?(Enumerable)
    if cloned_object.kind_of?(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


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

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