sparkr 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/README.md +2 -0
- data/lib/sparkr.rb +2 -0
- data/lib/sparkr/cli.rb +2 -0
- data/lib/sparkr/sparkline.rb +78 -4
- data/lib/sparkr/version.rb +3 -1
- data/test/cli_test.rb +2 -0
- data/test/sparkr_test.rb +31 -1
- data/test/test_helper.rb +5 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8dd99340dc0555b7c2a29e091f62e406fbe5233
|
4
|
+
data.tar.gz: 2bda5d1efce7cca0959a8a03805a20b20b9266ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f886432e12a6623ec1bf5554518d482c9beeabbf80781c6ab18df3439aa10811366c161aad33f9d212bd842196728e8ecd4b3cf316f918fb905591a86bbf88cb
|
7
|
+
data.tar.gz: 257c9d14e88ac0abb66de3118c92fe73cc548a064987ea296d61b50ccd2712f32b5e3d725445ca945f17ce2435eb5b518c3cf3bf6d8986dea3d0d4cf503571fb
|
data/.travis.yml
ADDED
data/README.md
CHANGED
data/lib/sparkr.rb
CHANGED
data/lib/sparkr/cli.rb
CHANGED
data/lib/sparkr/sparkline.rb
CHANGED
@@ -1,19 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module Sparkr
|
2
4
|
class Sparkline
|
3
5
|
TICKS = %w(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
|
4
6
|
DEFAULT_SEPARATOR = ''
|
5
7
|
|
6
8
|
def initialize(_numbers)
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
@original_numbers = _numbers
|
10
|
+
|
11
|
+
numbers = normalize_numbers(_numbers)
|
12
|
+
one_step = step_height(numbers)
|
10
13
|
|
11
14
|
@ticks = numbers.map do |n|
|
12
|
-
index = (
|
15
|
+
index = (n / one_step).to_i
|
13
16
|
TICKS[index]
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
20
|
+
# Formats all the ticks in the Sparkline with a given block,
|
21
|
+
# returns itself
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
#
|
25
|
+
# Let's say you have a list of open and closed issues and want
|
26
|
+
# to format it so the open ones are red and the closed ones are
|
27
|
+
# green, so you can quickly see how you are doing. Let's further
|
28
|
+
# suppose you use a gem that adds a #color method to String
|
29
|
+
# for ANSI coloring.
|
30
|
+
#
|
31
|
+
# list = [open_issue_count, closed_issue_count]
|
32
|
+
# sparkline = Sparkr::Sparkline.new(list)
|
33
|
+
# sparkline.format do |tick, count, index|
|
34
|
+
# if index == 0
|
35
|
+
# tick.color(:red)
|
36
|
+
# else
|
37
|
+
# tick.color(:green)
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# sparkline.to_s
|
42
|
+
# # => "▁█" (colored, which you can't see)
|
43
|
+
#
|
44
|
+
# @return [Sparkline] itself
|
45
|
+
def format(&block)
|
46
|
+
new_ticks = []
|
47
|
+
@ticks.each_with_index do |tick, index|
|
48
|
+
if block.arity == 2
|
49
|
+
new_ticks << yield(tick, @original_numbers[index])
|
50
|
+
elsif block.arity == 3
|
51
|
+
new_ticks << yield(tick, @original_numbers[index], index)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
@ticks = new_ticks
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns the normalized equivalent of a given list
|
59
|
+
#
|
60
|
+
# normalize_numbers([3,4,7])
|
61
|
+
# # => [0,1,4]
|
62
|
+
#
|
63
|
+
# @return [Fixnum] the normalized equivalent of the given +_numbers+
|
64
|
+
def normalize_numbers(_numbers)
|
65
|
+
numbers = _numbers.map(&:to_i)
|
66
|
+
min = numbers.min
|
67
|
+
numbers.map do |n|
|
68
|
+
n - min
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [Float] the numerical "height" of a single bar "step"
|
73
|
+
def step_height(_numbers)
|
74
|
+
min, max = _numbers.minmax
|
75
|
+
actual_height = max - min
|
76
|
+
step = actual_height / steps.to_f
|
77
|
+
if step == 0
|
78
|
+
1
|
79
|
+
else
|
80
|
+
step
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# @return [Fixnum] the count of steps between the smallest and highest bar
|
85
|
+
def steps
|
86
|
+
TICKS.size - 1
|
87
|
+
end
|
88
|
+
|
89
|
+
# @param sep [String,nil] separator used to join the bars of the sparkline
|
90
|
+
# @return [String] the sparkline, seperated by +sep+ (defaults to '')
|
17
91
|
def to_s(sep = nil)
|
18
92
|
@ticks.join(sep || DEFAULT_SEPARATOR)
|
19
93
|
end
|
data/lib/sparkr/version.rb
CHANGED
data/test/cli_test.rb
CHANGED
data/test/sparkr_test.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
4
|
|
3
5
|
describe ::Sparkr do
|
4
|
-
it "should find work" do
|
6
|
+
it "should find work like holman/spark" do
|
5
7
|
assert_sparkline '▁▂▃▄▅▆▇█', [1,2,3,4,5,6,7,8]
|
6
8
|
assert_sparkline '▁▂█▅▂', [1,5,22,13,5]
|
7
9
|
assert_sparkline '▁█', [5.5,20]
|
@@ -11,4 +13,32 @@ describe ::Sparkr do
|
|
11
13
|
assert_sparkline '▁▂▃▄▂█', [0,30,55,80,33,150]
|
12
14
|
assert_sparkline '▁▂▄▆█', [1,2,3,4,5]
|
13
15
|
end
|
16
|
+
|
17
|
+
it "should find work with equal numbers" do
|
18
|
+
assert_sparkline '▁▁', [10, 10]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "format should work with arity == 2" do
|
22
|
+
sparkline = Sparkr::Sparkline.new([5.5,20])
|
23
|
+
sparkline.format do |tick, number|
|
24
|
+
if number < 6.0
|
25
|
+
tick = tick + "-"
|
26
|
+
else
|
27
|
+
tick
|
28
|
+
end
|
29
|
+
end
|
30
|
+
assert_equal '▁-█', sparkline.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
it "format should work with arity == 3" do
|
34
|
+
sparkline = Sparkr::Sparkline.new([5.5,20])
|
35
|
+
sparkline.format do |tick, number, index|
|
36
|
+
if index == 1
|
37
|
+
tick = tick + "-"
|
38
|
+
else
|
39
|
+
tick
|
40
|
+
end
|
41
|
+
end
|
42
|
+
assert_equal '▁█-', sparkline.to_s
|
43
|
+
end
|
14
44
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'simplecov'
|
2
4
|
SimpleCov.start
|
3
5
|
|
@@ -12,6 +14,8 @@ def assert_sparkline(expected, numbers)
|
|
12
14
|
actual = Sparkr.sparkline(numbers)
|
13
15
|
|
14
16
|
assert actual.include?('▁'), "there must be a minimum"
|
15
|
-
|
17
|
+
unless numbers.uniq.size == 1 # all numbers are the same
|
18
|
+
assert actual.include?('█'), "there must be a maximum"
|
19
|
+
end
|
16
20
|
assert_equal expected, actual
|
17
21
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sparkr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- René Föhring
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- .gitignore
|
50
50
|
- .ruby-gemset
|
51
51
|
- .ruby-version
|
52
|
+
- .travis.yml
|
52
53
|
- Gemfile
|
53
54
|
- LICENSE.txt
|
54
55
|
- README.md
|