to_bar_graph 0.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/bar_graph.rb +53 -0
- data/lib/bucketizer.rb +42 -0
- data/lib/stdout_print.rb +71 -0
- data/lib/to_bar_graph.rb +13 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e22264826b2a8f699c2d1cbda335530cb379233
|
4
|
+
data.tar.gz: 133b2984e02e72394f6e72054980a6c80273d42e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b2f18f26df18b341ca06e6483c2a64cc8af77b37081fb0e0b3b217bd7ed7678d0225f3816e0d68267b1e6dbe46587d798f8c1e38ede80d8d7cca5a8dd6b974c3
|
7
|
+
data.tar.gz: dcca6a48c325ef5ade4da636d2acac3c60f3d493826b8895ba6051262e4e95b0b5fb558b5309a768eec86ffec723db2a93d6dba63bf108105df92b95543af3e0
|
data/lib/bar_graph.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require_relative './bucketizer'
|
2
|
+
|
3
|
+
module ToBarGraph
|
4
|
+
|
5
|
+
class BarGraph
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def initialize(array)
|
9
|
+
bucketizer = Bucketizer.new(array)
|
10
|
+
@buckets = bucketizer.create_buckets
|
11
|
+
end
|
12
|
+
attr_reader :buckets
|
13
|
+
|
14
|
+
def each(&block)
|
15
|
+
@buckets.each do |b|
|
16
|
+
yield b
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
return @buckets[key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def length
|
25
|
+
return @buckets.length
|
26
|
+
end
|
27
|
+
|
28
|
+
def min_value
|
29
|
+
return @buckets.min_by { |k, v| @buckets[k] }[1]
|
30
|
+
end
|
31
|
+
|
32
|
+
def max_value
|
33
|
+
return (@buckets.max_by { |k, v| @buckets[k] })[1]
|
34
|
+
end
|
35
|
+
|
36
|
+
def bucket_contents_length
|
37
|
+
@buckets.reduce(0) { |sum, x| sum + x[1] }
|
38
|
+
end
|
39
|
+
|
40
|
+
def longest_category_name
|
41
|
+
longest = ''
|
42
|
+
|
43
|
+
@buckets.each { |h| longest = h[0] if (h[0].length > longest.length) }
|
44
|
+
|
45
|
+
return (longest == '') ? 0 : longest
|
46
|
+
end
|
47
|
+
|
48
|
+
def inspect
|
49
|
+
return "class: #{self.class} object_id: #{self.object_id}"
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/lib/bucketizer.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module ToBarGraph
|
2
|
+
|
3
|
+
class Bucketizer
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@data = data
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_buckets
|
10
|
+
return create_buckets_from_hash if(@data.is_a?(Hash))
|
11
|
+
|
12
|
+
return create_buckets_from_array if(@data.is_a?(Array))
|
13
|
+
|
14
|
+
raise ArgumentError, "You need to provide either an Array or a Hash"
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def create_buckets_from_array
|
19
|
+
buckets = {}
|
20
|
+
|
21
|
+
@data.each do |e|
|
22
|
+
buckets[e] ||= 0
|
23
|
+
buckets[e] += 1
|
24
|
+
end
|
25
|
+
|
26
|
+
return buckets
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_buckets_from_hash
|
30
|
+
buckets = {}
|
31
|
+
|
32
|
+
@data.each { |k, v| buckets[k] = v }
|
33
|
+
|
34
|
+
if(buckets.any? { |k, v| v.is_a?(String) })
|
35
|
+
return buckets.map { |k, v| [k, v.to_i] }.to_h
|
36
|
+
end
|
37
|
+
|
38
|
+
return buckets
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/lib/stdout_print.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module ToBarGraph
|
2
|
+
|
3
|
+
class StdoutPrint
|
4
|
+
|
5
|
+
def initialize(bar_graph, stdout=$stdout)
|
6
|
+
@bar_graph = bar_graph
|
7
|
+
@stdout = stdout
|
8
|
+
end
|
9
|
+
|
10
|
+
def invoke
|
11
|
+
if @bar_graph.length == 0
|
12
|
+
@stdout.puts "You have no bar graph data"
|
13
|
+
return
|
14
|
+
end
|
15
|
+
|
16
|
+
unless @bar_graph.buckets.is_a?(Hash)
|
17
|
+
@stdout.puts "The data you have provided is not bargraph-able"
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
21
|
+
print_header
|
22
|
+
|
23
|
+
print_body
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def print_header
|
28
|
+
@stdout.puts "\n**************************************************************"
|
29
|
+
@stdout.puts "Results for #to_bar_graph(print_info: true)"
|
30
|
+
@stdout.puts "\n"
|
31
|
+
|
32
|
+
@stdout.puts "Data set Size: #{@bar_graph.bucket_contents_length} items"
|
33
|
+
|
34
|
+
@stdout.puts "Min Value: #{@bar_graph.min_value}, Max Value: #{@bar_graph.max_value}"
|
35
|
+
@stdout.puts "**************************************************************\n\n"
|
36
|
+
end
|
37
|
+
|
38
|
+
def print_body
|
39
|
+
cata = '%-20s'
|
40
|
+
freq = '%-20s'
|
41
|
+
prec = '%-30s'
|
42
|
+
star = '%-20s'
|
43
|
+
cata_sig = '%.20s'
|
44
|
+
|
45
|
+
longest_category_name = @bar_graph.longest_category_name.length
|
46
|
+
if longest_category_name >= 50
|
47
|
+
cata = '%-100s'
|
48
|
+
cata_sig = '%.100s'
|
49
|
+
freq = '%-15s'
|
50
|
+
prec = '%-15s'
|
51
|
+
else
|
52
|
+
cata = '%-20s'
|
53
|
+
cata_sig = '%.20s'
|
54
|
+
end
|
55
|
+
|
56
|
+
@stdout.printf("#{cata} #{freq} #{prec} #{star} \n\n", "Category", "Frequency", " Percentage", "Bar Graph (each * =~ 1%)")
|
57
|
+
|
58
|
+
@bar_graph.each do |k, v|
|
59
|
+
category = "#{k}"
|
60
|
+
frequency = v
|
61
|
+
percentage = ((frequency.to_f / @bar_graph.bucket_contents_length) * 100)
|
62
|
+
stars = ''
|
63
|
+
|
64
|
+
percentage.ceil.times { |x| stars << '*' }
|
65
|
+
|
66
|
+
@stdout.printf("#{cata} | #{freq} | #{prec} | #{star} \n", ("#{cata_sig}" % category), frequency, ('%.4f' % percentage), stars)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
data/lib/to_bar_graph.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative './bar_graph'
|
2
|
+
require_relative './stdout_print'
|
3
|
+
|
4
|
+
['Array', 'Hash'].each do |c|
|
5
|
+
eval "class #{Object.const_get(c)};
|
6
|
+
def to_bar_graph(print_info: true);
|
7
|
+
bar_graph = ToBarGraph::BarGraph.new(self);
|
8
|
+
stdout_print = ToBarGraph::StdoutPrint.new(bar_graph);
|
9
|
+
stdout_print.invoke if print_info;
|
10
|
+
return bar_graph;
|
11
|
+
end;
|
12
|
+
end"
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_bar_graph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brett Sykes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: 'Adds #to_bar_graph to Array and to Hash and prints a simple bar graph
|
14
|
+
to stdout.'
|
15
|
+
email: brettcsykes@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/bar_graph.rb
|
21
|
+
- lib/bucketizer.rb
|
22
|
+
- lib/stdout_print.rb
|
23
|
+
- lib/to_bar_graph.rb
|
24
|
+
homepage: https://github.com/bretts/to_bar_graph
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.4.5
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Creates a simple bar graph on the command line
|
48
|
+
test_files: []
|