nextract 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/nextract.rb +123 -0
- metadata +46 -0
data/lib/nextract.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
#/
|
2
|
+
# @name nextract
|
3
|
+
# @description Extract an array or hash of desired keys from an array of hashes or objects.
|
4
|
+
# @params
|
5
|
+
# - First argument is always the array
|
6
|
+
# - Second argument can be a configuration Hash, which will look like
|
7
|
+
# {
|
8
|
+
# format: Array or Hash (default Array)
|
9
|
+
# flatten: true or false (default false)
|
10
|
+
# default: defaultValue (default nil)
|
11
|
+
# }
|
12
|
+
# - Remaining arguments can either be strings or symbols representing the keys to be extracted.
|
13
|
+
# - The keys can also be passed in as an Array.
|
14
|
+
# - If flatten is set to true and there is only one key to be extracted, the function will return
|
15
|
+
# a 1D array.
|
16
|
+
# Examples: `nextract([{ a: 1, b: 2 }, { a: 2, c: 5 }], { default: 0 }, ['a', 'b'])`
|
17
|
+
# `[[1, 2], [2, 0]]`
|
18
|
+
# `nextract([{ a: 5, b: 2 }, { a: 6, c: 7 }], { format: Hash }, 'a', 'b')`
|
19
|
+
# `[{ a: 5, b: 2 }, { a: 6, b: nil }]`
|
20
|
+
# `nextract([{ a: 5, b: 2 }, { a: 1, b: 1 }], 'a')`
|
21
|
+
# `[5, 1]`
|
22
|
+
# @returns An Array or Hash or desired keys.
|
23
|
+
#/
|
24
|
+
def nextract *args
|
25
|
+
if not args[0].kind_of? Array
|
26
|
+
raise "Must supply an Array as first argument."
|
27
|
+
end
|
28
|
+
arr = args[0]
|
29
|
+
if args[1].kind_of? Hash
|
30
|
+
if args[1].has_key? 'default'
|
31
|
+
default = args[1]['default']
|
32
|
+
elsif args[1].has_key? :default
|
33
|
+
default = args[1][:default]
|
34
|
+
else
|
35
|
+
default = nil
|
36
|
+
end
|
37
|
+
if args[1].has_key? 'format'
|
38
|
+
format = args[1]['format']
|
39
|
+
elsif args[1].has_key? :format
|
40
|
+
format = args[1][:format]
|
41
|
+
else
|
42
|
+
format = Array
|
43
|
+
end
|
44
|
+
if args[1].has_key? 'flatten'
|
45
|
+
flatten = args[1]['flatten']
|
46
|
+
elsif args[1].has_key? :flatten
|
47
|
+
flatten = args[1][:flatten]
|
48
|
+
else
|
49
|
+
flatten = false
|
50
|
+
end
|
51
|
+
args = args[2..-1]
|
52
|
+
else
|
53
|
+
default = nil
|
54
|
+
format = Array
|
55
|
+
args = args[1..-1]
|
56
|
+
end
|
57
|
+
if args.length === 1 and args[0].kind_of? Array
|
58
|
+
args = args[0]
|
59
|
+
end
|
60
|
+
ret = Array.new
|
61
|
+
# state variable
|
62
|
+
checked = 0x00
|
63
|
+
arr.each { |v|
|
64
|
+
if flatten and args.length === 1
|
65
|
+
if v.kind_of? Hash
|
66
|
+
if not v.has_key? args[0]
|
67
|
+
ret.push default
|
68
|
+
else
|
69
|
+
ret.push v[args[0]]
|
70
|
+
end
|
71
|
+
else
|
72
|
+
if not v.respond_to? args[0]
|
73
|
+
ret.push default
|
74
|
+
else
|
75
|
+
ret.push v.send(args[0])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
else
|
79
|
+
if format == Array
|
80
|
+
ret.push Array.new
|
81
|
+
else
|
82
|
+
ret.push Hash.new
|
83
|
+
end
|
84
|
+
args.each { |u|
|
85
|
+
if checked & 0x01 and not (u.kind_of? Symbol or u.kind_of? String)
|
86
|
+
raise "Arguments must be of type String or Symbol"
|
87
|
+
end
|
88
|
+
if v.kind_of? Hash
|
89
|
+
if not v.has_key? u
|
90
|
+
if format == Array
|
91
|
+
ret[ret.length - 1].push default
|
92
|
+
else
|
93
|
+
ret[ret.length - 1][u] = default
|
94
|
+
end
|
95
|
+
else
|
96
|
+
if format == Array
|
97
|
+
ret[ret.length - 1].push v[u]
|
98
|
+
else
|
99
|
+
ret[ret.length - 1][u] = v[u]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
else
|
103
|
+
if not v.respond_to? u
|
104
|
+
if format == Array
|
105
|
+
ret[ret.length - 1].push default
|
106
|
+
else
|
107
|
+
ret[ret.length - 1][u] = default
|
108
|
+
end
|
109
|
+
else
|
110
|
+
if format == Array
|
111
|
+
ret[ret.length - 1].push v.send(u)
|
112
|
+
else
|
113
|
+
ret[ret.length - 1][u] = v.send(u)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
}
|
118
|
+
#define CHECKED_ARGUMENTS 0x01
|
119
|
+
checked |= 0x01
|
120
|
+
end
|
121
|
+
}
|
122
|
+
return ret
|
123
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nextract
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Raymond Pulver IV
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-07-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A function to extract n-dimensional data from an array of Hashes or Objects.
|
15
|
+
email: raymond.pulver_iv@uconn.edu
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/nextract.rb
|
21
|
+
homepage: http://github.com/raypulver/nextract
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.23
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Extract n-dimensional data from an array of Hashes or Objects.
|
46
|
+
test_files: []
|