Module: Deep

Defined in:
lib/vex/base/enumerable/deep.rb

Defined Under Namespace

Modules: Etest

Constant Summary collapse

KEYS =
0x01
VALUES =
0x02

Class Method Summary collapse

Class Method Details

.map(o, mode, &block) ⇒ Object

deep mapping


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vex/base/enumerable/deep.rb', line 7

def self.map(o, mode, &block)
  case o
  when Hash
    o.inject({}) do |hash, (k,v)|
      k = yield(k) if (mode & KEYS) != 0
      v = map(v, mode, &block)
      hash.update k => v
      hash
    end
  when Array
    if mode & VALUES
      o.map do |v|
        map(v, mode, &block)
      end
    else
      o.dup
    end
  else
    if (mode & VALUES) != 0
      yield o 
    else
      o
    end
  end
end

.reject(o, mode, &block) ⇒ Object

deep reject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vex/base/enumerable/deep.rb', line 35

def self.reject(o, mode, &block)
  case o
  when Hash
    o.inject({}) do |hash, (k,v)|
      next hash if (mode & KEYS) && yield(k)
      v = reject v, mode, &block
      next hash if (mode & VALUES) && yield(v)
      hash.update k => v
      hash
    end
  when Array
    if mode & VALUES
      o.inject([]) do |r, v|
        v = reject(v, mode, &block)
        r << v unless yield(v)
      end
    else
      o.dup
    end
  else
    o
  end
end