deepmap 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/deepmap/deepmap.rb +21 -18
- data/readme.md +1 -0
- metadata +21 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1245989b50aa651e1aa4b29a6f03f1ee61ba9122
|
4
|
+
data.tar.gz: ab4ad7b7c7474934558411dedfcc972883ccb4aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 245795f12fcb97d4e9e754cd136b3bdcc45f22174d484e3189284d8dc6fdeb8f3d671f539793e874fe142a14786fc64775e7eb38417f19e36241aa352d992986
|
7
|
+
data.tar.gz: 51fad525dbec284057e03d60fabcee58c97a5f2be1d21ed31727e9df8c691fdec0eaf4bad116843b1239fb4dd178982588fbf1708dbdf094f8c17bf681b6f98e
|
data/lib/deepmap/deepmap.rb
CHANGED
@@ -3,20 +3,21 @@
|
|
3
3
|
module DeepMap
|
4
4
|
def deep_map
|
5
5
|
return self if !block_given?
|
6
|
-
recurse {|x| yield x}
|
6
|
+
recurse { |x| yield x }
|
7
7
|
end
|
8
8
|
|
9
9
|
def key_map
|
10
10
|
return self if !block_given?
|
11
|
-
recurse("key") {|x| yield x}
|
11
|
+
recurse("key") { |x| yield x }
|
12
12
|
end
|
13
13
|
|
14
14
|
def val_map
|
15
15
|
return self if !block_given?
|
16
|
-
recurse("val") {|x| yield x}
|
16
|
+
recurse("val") { |x| yield x }
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
|
+
|
20
21
|
# Main recursive method. With the block passed in, keep recursing through the
|
21
22
|
# object, applying the block to either each key or value (or both) depending
|
22
23
|
# on the first caller of this method. This is the value stored in 't' type. If
|
@@ -24,30 +25,33 @@ module DeepMap
|
|
24
25
|
# the key is an array, then map the recursive method over each item in the
|
25
26
|
# array. If it is niether a hash or an array, then apply the block to that
|
26
27
|
# item, and return the result.
|
27
|
-
def recurse(t="both", h=self)
|
28
|
-
if h.is_a?(Hash)
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
[ yield(k), recurse(t, v) {|x| yield x } ]
|
34
|
-
when "val"
|
35
|
-
[ k, recurse(t, v) {|x| yield x} ]
|
36
|
-
end
|
37
|
-
}]
|
29
|
+
def recurse(t = "both", h = self)
|
30
|
+
if h.is_a?(Hash)
|
31
|
+
hash_collect(t, h) { |x| yield x }
|
38
32
|
|
39
33
|
elsif h.is_a?(Array)
|
40
|
-
|
41
|
-
h.map {|v| recurse(t, v) {|x| yield(x) } }
|
34
|
+
h.map { |v| recurse(t, v) { |x| yield x } }
|
42
35
|
|
43
36
|
else # apply to value
|
44
|
-
|
45
37
|
t == "key" ? h : yield(h)
|
46
38
|
|
47
39
|
end
|
48
40
|
end
|
49
|
-
end
|
50
41
|
|
42
|
+
def hash_collect(t, h)
|
43
|
+
Hash[h.collect { |k, v|
|
44
|
+
case t # type
|
45
|
+
when "both", "key"
|
46
|
+
[yield(k), recurse(t, v) { |x| yield x }]
|
47
|
+
|
48
|
+
when "val"
|
49
|
+
[k, recurse(t, v) { |x| yield x }]
|
50
|
+
|
51
|
+
end
|
52
|
+
}]
|
53
|
+
end
|
54
|
+
end
|
51
55
|
|
52
56
|
# Adding deep_map, key_map, val_map methods to Hash and Array classes.
|
53
57
|
|
@@ -58,4 +62,3 @@ end
|
|
58
62
|
class Array
|
59
63
|
include DeepMap
|
60
64
|
end
|
61
|
-
|
data/readme.md
CHANGED
@@ -4,6 +4,7 @@ deepmap
|
|
4
4
|
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/deepmap.svg)](https://badge.fury.io/rb/deepmap)
|
6
6
|
[![Build Status](https://travis-ci.org/jeremywrnr/deepmap.svg?branch=master)](https://travis-ci.org/jeremywrnr/deepmap)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/jeremywrnr/deepmap/badges/gpa.svg)](https://codeclimate.com/github/jeremywrnr/deepmap)
|
7
8
|
[![MIT](https://img.shields.io/npm/l/alt.svg?style=flat)](http://jeremywrnr.com/mit-license)
|
8
9
|
|
9
10
|
|
metadata
CHANGED
@@ -1,41 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deepmap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Warner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rufo
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
55
|
description: Map functions over nested hash/arrays objects (e.g. JSON).
|
@@ -66,9 +80,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
80
|
version: '0'
|
67
81
|
requirements: []
|
68
82
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
83
|
+
rubygems_version: 2.6.14
|
70
84
|
signing_key:
|
71
85
|
specification_version: 4
|
72
86
|
summary: Nested hash/array function mapping.
|
73
87
|
test_files: []
|
74
|
-
has_rdoc:
|