deepmap 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/deepmap/deepmap.rb +61 -0
- data/readme.md +72 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c947c80cc87828eea108ac893fbd9c789bc146d6
|
4
|
+
data.tar.gz: da3b2cfa43370a8f518246c2e36b54039400c3e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f9d0268d5acd36c6534146066eb5b3bebaf37032b89ea89cd69b748a42868473f10b8ce4b277517d70e525b662fdbf112ab052715066dec7160a7bab6cd5a1e
|
7
|
+
data.tar.gz: 9763f5bb31035ae8814ad8a22e14b937e6340d691d45a4939eb85085419951a62c0ecfedfc0c3709c36bc8199b2dc53eeec7649fe18b80dcc3876ab8b1ea4685
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Creating deep_map, key_map, val_map methods in DeepMap.
|
2
|
+
|
3
|
+
module DeepMap
|
4
|
+
def deep_map
|
5
|
+
return self if !block_given?
|
6
|
+
recurse {|x| yield x}
|
7
|
+
end
|
8
|
+
|
9
|
+
def key_map
|
10
|
+
return self if !block_given?
|
11
|
+
recurse("key") {|x| yield x}
|
12
|
+
end
|
13
|
+
|
14
|
+
def val_map
|
15
|
+
return self if !block_given?
|
16
|
+
recurse("val") {|x| yield x}
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
# Main recursive method. With the block passed in, keep recursing through the
|
21
|
+
# object, applying the block to either each key or value (or both) depending
|
22
|
+
# on the first caller of this method. This is the value stored in 't' type. If
|
23
|
+
# the key is a hash, than collect the results of calling it recursively. If
|
24
|
+
# the key is an array, then map the recursive method over each item in the
|
25
|
+
# array. If it is niether a hash or an array, then apply the block to that
|
26
|
+
# item, and return the result.
|
27
|
+
def recurse(t="both", h=self)
|
28
|
+
if h.is_a?(Hash)
|
29
|
+
|
30
|
+
Hash[ h.collect {|k, v|
|
31
|
+
case t # type
|
32
|
+
when "both", "key"
|
33
|
+
[ yield(k), recurse(t, v) {|x| yield x } ]
|
34
|
+
when "val"
|
35
|
+
[ k, recurse(t, v) {|x| yield x} ]
|
36
|
+
end
|
37
|
+
}]
|
38
|
+
|
39
|
+
elsif h.is_a?(Array)
|
40
|
+
|
41
|
+
h.map {|v| recurse(t, v) {|x| yield(x) } }
|
42
|
+
|
43
|
+
else # apply to value
|
44
|
+
|
45
|
+
t == "key" ? h : yield(h)
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
# Adding deep_map, key_map, val_map methods to Hash and Array classes.
|
53
|
+
|
54
|
+
class Hash
|
55
|
+
include DeepMap
|
56
|
+
end
|
57
|
+
|
58
|
+
class Array
|
59
|
+
include DeepMap
|
60
|
+
end
|
61
|
+
|
data/readme.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
deepmap
|
2
|
+
=======
|
3
|
+
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/deepmap.svg)](https://badge.fury.io/rb/deepmap)
|
6
|
+
[![Build Status](https://travis-ci.org/jeremywrnr/deepmap.svg?branch=master)](https://travis-ci.org/jeremywrnr/deepmap)
|
7
|
+
[![MIT](https://img.shields.io/npm/l/alt.svg?style=flat)](http://jeremywrnr.com/mit-license)
|
8
|
+
|
9
|
+
|
10
|
+
## setup
|
11
|
+
|
12
|
+
[sudo] gem install deepmap
|
13
|
+
|
14
|
+
## about
|
15
|
+
|
16
|
+
Ruby gem that adds three methods to the Hash and Array classes. Overview:
|
17
|
+
|
18
|
+
- `#deep_map` - apply block to each key and value in object
|
19
|
+
- `#key_map` - apply block to each key in object
|
20
|
+
- `#val_map` - apply block to each value in object
|
21
|
+
|
22
|
+
These may be useful when you want to apply a function to each value (or key or
|
23
|
+
pair) of a complex nested object (such as the result of parsing a YAML or JSON
|
24
|
+
file), which can have hashes or arrays as subfields. The function currently
|
25
|
+
needs to be passed in as a block (see usage).
|
26
|
+
|
27
|
+
|
28
|
+
## usage
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
irb(main):001:0> require 'deepmap'
|
32
|
+
=> true
|
33
|
+
irb(main):002:0> test = { 1 => 4, 2 => [5, 6], 3 => { 4 => [1, 2, { 5 => 10 }] } }
|
34
|
+
=> {1=>4, 2=>[5, 6], 3=>{4=>[1, 2, {5=>10}]}}
|
35
|
+
irb(main):003:0> test.deep_map {|i| i.to_i * 2 }
|
36
|
+
=> {2=>8, 4=>[10, 12], 6=>{8=>[2, 4, {10=>20}]}}
|
37
|
+
irb(main):004:0> test.key_map {|i| i.to_i * 2 }
|
38
|
+
=> {2=>4, 4=>[5, 6], 6=>{8=>[1, 2, {10=>10}]}}
|
39
|
+
irb(main):005:0> test.val_map {|i| i.to_i * 2 }
|
40
|
+
=> {1=>8, 2=>[10, 12], 3=>{4=>[2, 4, {5=>20}]}}
|
41
|
+
```
|
42
|
+
|
43
|
+
Once you `require 'deepmap'`, you can call any of the three provided functions
|
44
|
+
on any (existing or new!) hash or array, as demonstrated above.
|
45
|
+
|
46
|
+
|
47
|
+
## development / testing
|
48
|
+
|
49
|
+
First, clone this repo:
|
50
|
+
|
51
|
+
git clone https://github.com/jeremywrnr/deepmap.git
|
52
|
+
|
53
|
+
To build from source, first install the project dependencies. This project
|
54
|
+
uses `bundler`, the standard ruby gem management system. If you don't have it,
|
55
|
+
try running `gem install bundler`. Once that is done:
|
56
|
+
|
57
|
+
bundle install
|
58
|
+
|
59
|
+
Now, we should be able to build the gem locally. This will build the local
|
60
|
+
deepmap gem and link it in your path, so you can playing around with `deepmap`.
|
61
|
+
|
62
|
+
rake build
|
63
|
+
|
64
|
+
This uses `rspec` and `rake` to run a suite of unit tests. To run the suite:
|
65
|
+
|
66
|
+
rake
|
67
|
+
|
68
|
+
|
69
|
+
## todo
|
70
|
+
|
71
|
+
- support better object mapping, like `#deep_map(:method) => #deep_map {|x| x.method }`
|
72
|
+
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deepmap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Warner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: easily map functions over key/vals of nested hash/arrays objects (eg
|
42
|
+
JSON)
|
43
|
+
email: jeremywrnr@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/deepmap/deepmap.rb
|
49
|
+
- readme.md
|
50
|
+
homepage: http://github.com/jeremywrnr/deepmap
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib/deepmap
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.5.1
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: nested hash/arrap function mapping
|
74
|
+
test_files: []
|
75
|
+
has_rdoc:
|