goldmine 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +58 -21
- data/README.md +21 -1
- data/goldmine.gemspec +19 -0
- data/lib/goldmine/array_miner.rb +3 -2
- data/lib/goldmine/hash_miner.rb +23 -2
- data/lib/goldmine/version.rb +1 -1
- data/license.md +8 -0
- data/test/test_goldmine.rb +47 -0
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2da17da9ef071e7861b17cce9d420675d9a33113
|
4
|
+
data.tar.gz: e5694ec828554a1e0330256e0cb203bbca7004fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb3e37ca75c3dda52415bc671f2e334db814b2755d639654c37b72a69a2c1f606cc9910407d524b5abbd043319de776fb35d284df8945bfabbc6153a42e5b7c3
|
7
|
+
data.tar.gz: 76cc1f317fbde8bdddf364ed3ea7b9734c2df8700dc80beecfb1457118f41aa31bc3ceddceb05814a77344260a56a49526e04d9e65763730a6e2e0469ec60579
|
data/Gemfile.lock
CHANGED
@@ -1,35 +1,72 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
goldmine (1.
|
4
|
+
goldmine (1.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
binding_of_caller (0.7.2)
|
10
|
+
debug_inspector (>= 0.0.1)
|
11
|
+
byebug (4.0.5)
|
12
|
+
columnize (= 0.9.0)
|
13
|
+
coderay (1.1.0)
|
14
|
+
columnize (0.9.0)
|
15
|
+
coveralls (0.8.1)
|
16
|
+
json (~> 1.8)
|
17
|
+
rest-client (>= 1.6.8, < 2)
|
18
|
+
simplecov (~> 0.10.0)
|
19
|
+
term-ansicolor (~> 1.3)
|
20
|
+
thor (~> 0.19.1)
|
21
|
+
debug_inspector (0.0.2)
|
15
22
|
docile (1.1.5)
|
16
|
-
|
17
|
-
|
23
|
+
domain_name (0.5.24)
|
24
|
+
unf (>= 0.0.5, < 1.0.0)
|
25
|
+
http-cookie (1.0.2)
|
26
|
+
domain_name (~> 0.5)
|
27
|
+
interception (0.5)
|
28
|
+
json (1.8.2)
|
29
|
+
method_source (0.8.2)
|
30
|
+
mime-types (2.6.1)
|
31
|
+
netrc (0.10.3)
|
18
32
|
os (0.9.6)
|
19
|
-
pry
|
33
|
+
pry (0.10.1)
|
34
|
+
coderay (~> 1.1.0)
|
35
|
+
method_source (~> 0.8.1)
|
36
|
+
slop (~> 3.4)
|
37
|
+
pry-byebug (3.1.0)
|
38
|
+
byebug (~> 4.0)
|
39
|
+
pry (~> 0.10)
|
40
|
+
pry-rescue (1.4.2)
|
41
|
+
interception (>= 0.5)
|
42
|
+
pry
|
43
|
+
pry-stack_explorer (0.4.9.2)
|
44
|
+
binding_of_caller (>= 0.7)
|
45
|
+
pry (>= 0.9.11)
|
46
|
+
pry-test (0.6.4)
|
20
47
|
os
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
48
|
+
pry
|
49
|
+
pry-byebug
|
50
|
+
pry-rescue
|
51
|
+
pry-stack_explorer
|
52
|
+
rake (10.4.2)
|
53
|
+
rest-client (1.8.0)
|
54
|
+
http-cookie (>= 1.0.2, < 2.0)
|
55
|
+
mime-types (>= 1.16, < 3.0)
|
56
|
+
netrc (~> 0.7)
|
57
|
+
simplecov (0.10.0)
|
25
58
|
docile (~> 1.1.0)
|
26
|
-
|
27
|
-
simplecov-html (~> 0.
|
28
|
-
simplecov-html (0.
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
59
|
+
json (~> 1.8)
|
60
|
+
simplecov-html (~> 0.10.0)
|
61
|
+
simplecov-html (0.10.0)
|
62
|
+
slop (3.6.0)
|
63
|
+
term-ansicolor (1.3.0)
|
64
|
+
tins (~> 1.0)
|
65
|
+
thor (0.19.1)
|
66
|
+
tins (1.5.2)
|
67
|
+
unf (0.1.4)
|
68
|
+
unf_ext
|
69
|
+
unf_ext (0.0.7.1)
|
33
70
|
|
34
71
|
PLATFORMS
|
35
72
|
ruby
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
[![Lines of Code](http://img.shields.io/badge/lines_of_code-
|
1
|
+
[![Lines of Code](http://img.shields.io/badge/lines_of_code-89-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
|
2
2
|
[![Code Status](http://img.shields.io/codeclimate/github/hopsoft/goldmine.svg?style=flat)](https://codeclimate.com/github/hopsoft/goldmine)
|
3
3
|
[![Dependency Status](http://img.shields.io/gemnasium/hopsoft/goldmine.svg?style=flat)](https://gemnasium.com/hopsoft/goldmine)
|
4
4
|
[![Build Status](http://img.shields.io/travis/hopsoft/goldmine.svg?style=flat)](https://travis-ci.org/hopsoft/goldmine)
|
5
5
|
[![Coverage Status](https://img.shields.io/coveralls/hopsoft/goldmine.svg?style=flat)](https://coveralls.io/r/hopsoft/goldmine?branch=master)
|
6
|
+
[![Downloads](http://img.shields.io/gem/dt/goldmine.svg?style=flat)](http://rubygems.org/gems/goldmine)
|
6
7
|
|
7
8
|
# Goldmine
|
8
9
|
|
@@ -141,3 +142,22 @@ end
|
|
141
142
|
}
|
142
143
|
```
|
143
144
|
|
145
|
+
# Returning pivots in tabular format
|
146
|
+
|
147
|
+
This feature is useful when you need to do things like export to CSV or build user interfaces.
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
# using the stacked pivot example above
|
151
|
+
mined.to_a
|
152
|
+
# result:
|
153
|
+
[
|
154
|
+
["Name has an 'e'", ">= 21 years old"],
|
155
|
+
[false, true, 2],
|
156
|
+
[true, true, 2],
|
157
|
+
[true, false, 1]
|
158
|
+
]
|
159
|
+
```
|
160
|
+
|
161
|
+
The first entry is the header row.
|
162
|
+
Subsequent entries are data rows.
|
163
|
+
The last value in each data row indicates the number of matches.
|
data/goldmine.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path("../lib/goldmine/version", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = "goldmine"
|
5
|
+
gem.license = "MIT"
|
6
|
+
gem.version = Goldmine::VERSION
|
7
|
+
gem.authors = ["Nathan Hopkins"]
|
8
|
+
gem.email = ["natehop@gmail.com"]
|
9
|
+
gem.homepage = "https://github.com/hopsoft/goldmine"
|
10
|
+
gem.summary = "Extract a wealth of information from Arrays and Hashes"
|
11
|
+
gem.description = "Extract a wealth of information from Arrays and Hashes"
|
12
|
+
|
13
|
+
gem.files = Dir["lib/**/*.rb", "bin/*", "[A-Z]*"]
|
14
|
+
gem.test_files = Dir["test/**/*.rb"]
|
15
|
+
|
16
|
+
gem.add_development_dependency "rake"
|
17
|
+
gem.add_development_dependency "pry-test"
|
18
|
+
gem.add_development_dependency "coveralls"
|
19
|
+
end
|
data/lib/goldmine/array_miner.rb
CHANGED
@@ -2,9 +2,10 @@ require "delegate"
|
|
2
2
|
|
3
3
|
module Goldmine
|
4
4
|
class ArrayMiner < SimpleDelegator
|
5
|
+
attr_reader :source_data
|
5
6
|
|
6
7
|
def initialize(array=[])
|
7
|
-
super array
|
8
|
+
super @source_data = array
|
8
9
|
end
|
9
10
|
|
10
11
|
# Pivots the Array into a Hash of mined data.
|
@@ -46,7 +47,7 @@ module Goldmine
|
|
46
47
|
# @yield [Object] Yields once for each item in the Array
|
47
48
|
# @return [Hash] The pivoted Hash of data.
|
48
49
|
def pivot(name=nil, &block)
|
49
|
-
reduce(HashMiner.new) do |memo, item|
|
50
|
+
reduce(HashMiner.new(source_data: source_data)) do |memo, item|
|
50
51
|
value = yield(item)
|
51
52
|
|
52
53
|
if value.is_a?(Array)
|
data/lib/goldmine/hash_miner.rb
CHANGED
@@ -2,8 +2,10 @@ require "delegate"
|
|
2
2
|
|
3
3
|
module Goldmine
|
4
4
|
class HashMiner < SimpleDelegator
|
5
|
+
attr_reader :source_data
|
5
6
|
|
6
|
-
def initialize(hash={})
|
7
|
+
def initialize(hash={}, source_data: nil)
|
8
|
+
@source_data = source_data || hash
|
7
9
|
super hash
|
8
10
|
end
|
9
11
|
|
@@ -32,7 +34,7 @@ module Goldmine
|
|
32
34
|
def pivot(name=nil, &block)
|
33
35
|
return self unless goldmine
|
34
36
|
|
35
|
-
reduce(HashMiner.new) do |memo, item|
|
37
|
+
reduce(HashMiner.new(source_data: source_data)) do |memo, item|
|
36
38
|
key = item.first
|
37
39
|
value = Goldmine.miner(item.last)
|
38
40
|
value.pivot(name, &block).each do |k, v|
|
@@ -69,5 +71,24 @@ module Goldmine
|
|
69
71
|
goldmine_key ||= key
|
70
72
|
end
|
71
73
|
|
74
|
+
# Returns the pivot keys.
|
75
|
+
# @return [Array]
|
76
|
+
def pivoted_keys
|
77
|
+
first.first.keys
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns pivoted data as a tabular Array that can be used to build CSVs or user interfaces.
|
81
|
+
# @return [Array] Tabular pivot data
|
82
|
+
def to_a
|
83
|
+
rows = map do |pair|
|
84
|
+
[].tap do |row|
|
85
|
+
row.concat pair.first.values
|
86
|
+
row << pair.last.size
|
87
|
+
end
|
88
|
+
end
|
89
|
+
rows.insert 0, pivoted_keys
|
90
|
+
rows
|
91
|
+
end
|
92
|
+
|
72
93
|
end
|
73
94
|
end
|
data/lib/goldmine/version.rb
CHANGED
data/license.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
data/test/test_goldmine.rb
CHANGED
@@ -43,6 +43,52 @@ class TestGoldmine < PryTest::Test
|
|
43
43
|
assert data == expected
|
44
44
|
end
|
45
45
|
|
46
|
+
test "pivoted_keys" do
|
47
|
+
list = [1,2,3,4,5,6,7,8,9]
|
48
|
+
list = Goldmine::ArrayMiner.new(list)
|
49
|
+
data = list.pivot("less than 5") { |i| i < 5 }
|
50
|
+
expected = ["less than 5"]
|
51
|
+
assert data.pivoted_keys == expected
|
52
|
+
end
|
53
|
+
|
54
|
+
test "to_a tabular data" do
|
55
|
+
list = [
|
56
|
+
{ :name => "Sally", :age => 21 },
|
57
|
+
{ :name => "John", :age => 28 },
|
58
|
+
{ :name => "Stephen", :age => 37 },
|
59
|
+
{ :name => "Emily", :age => 32 },
|
60
|
+
{ :name => "Joe", :age => 18 }
|
61
|
+
]
|
62
|
+
list = Goldmine::ArrayMiner.new(list)
|
63
|
+
mined = list.pivot("Name has an 'e'") do |record|
|
64
|
+
!!record[:name].match(/e/i)
|
65
|
+
end
|
66
|
+
mined = mined.pivot(">= 21 years old") do |record|
|
67
|
+
record[:age] >= 21
|
68
|
+
end
|
69
|
+
|
70
|
+
expected = [["Name has an 'e'", ">= 21 years old"], [false, true, 2], [true, true, 2], [true, false, 1]]
|
71
|
+
assert mined.to_a == expected
|
72
|
+
end
|
73
|
+
|
74
|
+
test "source_data" do
|
75
|
+
list = [
|
76
|
+
{ :name => "Sally", :age => 21 },
|
77
|
+
{ :name => "John", :age => 28 },
|
78
|
+
{ :name => "Stephen", :age => 37 },
|
79
|
+
{ :name => "Emily", :age => 32 },
|
80
|
+
{ :name => "Joe", :age => 18 }
|
81
|
+
]
|
82
|
+
list = Goldmine::ArrayMiner.new(list)
|
83
|
+
mined = list.pivot("Name has an 'e'") do |record|
|
84
|
+
!!record[:name].match(/e/i)
|
85
|
+
end
|
86
|
+
mined = mined.pivot(">= 21 years old") do |record|
|
87
|
+
record[:age] >= 21
|
88
|
+
end
|
89
|
+
assert mined.source_data == list
|
90
|
+
end
|
91
|
+
|
46
92
|
test "pivot of list values" do
|
47
93
|
list = [
|
48
94
|
{ :name => "one", :list => [1] },
|
@@ -155,6 +201,7 @@ class TestGoldmine < PryTest::Test
|
|
155
201
|
}
|
156
202
|
|
157
203
|
assert data == expected
|
204
|
+
assert data.source_data == list
|
158
205
|
end
|
159
206
|
|
160
207
|
test "named chained pivots" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: goldmine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Hopkins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: Extract a wealth of information from Arrays
|
55
|
+
description: Extract a wealth of information from Arrays and Hashes
|
56
56
|
email:
|
57
57
|
- natehop@gmail.com
|
58
58
|
executables: []
|
@@ -63,10 +63,12 @@ files:
|
|
63
63
|
- Gemfile.lock
|
64
64
|
- README.md
|
65
65
|
- Rakefile
|
66
|
+
- goldmine.gemspec
|
66
67
|
- lib/goldmine.rb
|
67
68
|
- lib/goldmine/array_miner.rb
|
68
69
|
- lib/goldmine/hash_miner.rb
|
69
70
|
- lib/goldmine/version.rb
|
71
|
+
- license.md
|
70
72
|
- test/test_goldmine.rb
|
71
73
|
homepage: https://github.com/hopsoft/goldmine
|
72
74
|
licenses:
|
@@ -88,9 +90,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
90
|
version: '0'
|
89
91
|
requirements: []
|
90
92
|
rubyforge_project:
|
91
|
-
rubygems_version: 2.
|
93
|
+
rubygems_version: 2.4.5
|
92
94
|
signing_key:
|
93
95
|
specification_version: 4
|
94
|
-
summary: Extract a wealth of information from Arrays
|
96
|
+
summary: Extract a wealth of information from Arrays and Hashes
|
95
97
|
test_files:
|
96
98
|
- test/test_goldmine.rb
|
99
|
+
has_rdoc:
|