module Enumerable
For all enumerable objects, we will have to handle their situation differently.
Public Instance Methods
add a single element to the enumerable. You may pass a single parameter, or a key, value. In any case, all will added.
Here all the logic will be present to handle the “special case” enumerables. Most notedly, Hash
and Array
will require special treatment.
# File lib/deep_dive/deep_dive.rb, line 110 def _add(v: nil, dupit: nil, oc: nil, patch: {}) unless _pairs? case when self.kind_of?(::Set) when self.kind_of?(::Array) self << _ob_maybe_repl(v: v, dupit: dupit, oc: oc, patch: patch) else raise DeepDiveException.new("Don't know how to add new elements for class #{self.class}") end else self[v.first] = _ob_maybe_repl(v: v.last, dupit: dupit, oc: oc, patch: patch) end end
FIXME: clean up the code a bit, this could be better structured.
# File lib/deep_dive/deep_dive.rb, line 95 def _ob_maybe_repl(v: nil, dupit: nil, oc: nil, patch: {}) if v.respond_to? :_replicate v._replicate(oc: oc, dupit: dupit, patch: patch) else v end end
We try to determine if this enumberable will return pairs (as in the case of a Hash
) or objects (which may look like pairs but not really).
# File lib/deep_dive/deep_dive.rb, line 127 def _pairs? self.kind_of? ::Hash end
Here, with this Enumerator, we want to create a new empty instance and populate it with replicas (or references) of the contained objects.
here, a nasty problem is that there is no unified API for adding or substituting objects into the new collection, so we are going to abstract that issue to #_add.
FIXME: We will initially not handle Enumberables that have instance variables. FIXME: This issue will be addressed at a later date.
# File lib/deep_dive/deep_dive.rb, line 141 def _replicate(dupit: true, oc: {}, patch: {}) unless oc.member? self self.inject(oc[self] = self.class.new) do |copy, v| copy._add(v: v, dupit: dupit, oc: oc, patch: patch) copy end end oc[self] end