Module: Enumerable
- Defined in:
- lib/deep_dive/deep_dive.rb
Overview
For all enumerable objects, we will have to handle their situation differently.
Instance Method Summary collapse
-
#_add(v: nil, dupit: nil, oc: nil) ⇒ Object
add a single element to the enumerable.
-
#_ob_maybe_repl(v: nil, dupit: nil, oc: nil) ⇒ Object
FIXME: clean up the code a bit, this could be better structured.
-
#_pairs? ⇒ Boolean
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).
-
#_replicate(dupit: true, oc: {}) ⇒ Object
Here, with this Enumerator, we want to create a new empty instance and populate it with replicas (or references) of the contained objects.
Instance Method Details
#_add(v: nil, dupit: nil, oc: nil) ⇒ Object
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.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/deep_dive/deep_dive.rb', line 83 def _add(v: nil, dupit: nil, oc: nil) unless _pairs? case when self.kind_of?(::Set) when self.kind_of?(::Array) self << _ob_maybe_repl(v: v, dupit: dupit, oc: oc) 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) end end |
#_ob_maybe_repl(v: nil, dupit: nil, oc: nil) ⇒ Object
FIXME: clean up the code a bit, this could be better structured.
68 69 70 71 72 73 74 |
# File 'lib/deep_dive/deep_dive.rb', line 68 def _ob_maybe_repl(v: nil, dupit: nil, oc: nil) if v.respond_to? :_replicate v._replicate(oc: oc, dupit: dupit) else v end end |
#_pairs? ⇒ Boolean
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).
100 101 102 |
# File 'lib/deep_dive/deep_dive.rb', line 100 def _pairs? self.kind_of? ::Hash end |
#_replicate(dupit: true, oc: {}) ⇒ Object
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.
114 115 116 117 118 119 120 121 122 |
# File 'lib/deep_dive/deep_dive.rb', line 114 def _replicate(dupit: true, oc: {}) unless oc.member? self self.inject(oc[self] = self.class.new) do |copy, v| copy._add(v: v, dupit: dupit, oc: oc) copy end end oc[self] end |