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

Instance Method Details

#_add(v: nil, dupit: nil, oc: nil, patch: {}) ⇒ 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.



110
111
112
113
114
115
116
117
118
119
120
121
122
# 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

#_ob_maybe_repl(v: nil, dupit: nil, oc: nil, patch: {}) ⇒ Object

FIXME: clean up the code a bit, this could be better structured.



95
96
97
98
99
100
101
# 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

#_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).



127
128
129
# File 'lib/deep_dive/deep_dive.rb', line 127

def _pairs?
  self.kind_of? ::Hash
end

#_replicate(dupit: true, oc: {}, patch: {}) ⇒ 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.



141
142
143
144
145
146
147
148
149
# 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