in_columns 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +5 -5
- data/lib/in_columns.rb +1 -1
- data/lib/in_columns/array_ext.rb +1 -1
- data/lib/in_columns/columnizer.rb +42 -10
- data/lib/in_columns/version.rb +1 -1
- data/test/in_columns/array_ext_test.rb +5 -2
- data/test/in_columns/columnizer_test.rb +97 -13
- data/test/in_columns_test.rb +4 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YjFjYjY4YWJmYzAzYmQ3NWU0YTI3ZTEzYzBkZDAxMTBiMmE2OGE4NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTdhMzJhNDg3YzM0NWU3YWNhYTJmYjU4ZmJhMzc2ZDBmMTllZWFiMQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzQ3Y2I1ZjhlYmQ5MjFhN2FmYzMyMWY2NWNjMmQ2N2YwMjQ4OTNlMWMwMzVk
|
10
|
+
NWNkNDYzMWY1NmRhNTgyY2Q0ZWZlMzcyMWUwZmU4ZThhMTVkNzY0NTFlZTFm
|
11
|
+
ZmQwNTc4YzlmYTk2NmUzZjY4NjNiMTYxYjFhZjA0ODZhNmM4MGI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjExNzY4YWU0ODRiNzY0NDMxZTA4Y2JmODVlOGU4ZTk3NmI5OTdlMjY5N2Uz
|
14
|
+
ZmY0Nzg2NTRlYTI4MzIzNDVlMzYxM2EwNThlYTA2OGY5YzkyODU2YjQzMDky
|
15
|
+
ZWVjNDNmZmZiMTdiYmE5MjYzZjFkZjAwNzdjY2UyODI2MmRmZWY=
|
data/README.md
CHANGED
@@ -10,11 +10,11 @@ columns - say for displaying it to an end user.
|
|
10
10
|
## Usage example
|
11
11
|
|
12
12
|
list = ['a', 'b', 'c', 'd', 'e']
|
13
|
-
InColumns.columnize(list, 2)
|
14
|
-
#=> [['a', 'c', 'e'], ['b', 'd']]
|
15
|
-
|
16
13
|
InColumns.columnize(list, 3)
|
17
|
-
#=> [['a', '
|
14
|
+
#=> [['a', 'c', 'e'], ['b', 'd', nil]]
|
15
|
+
|
16
|
+
InColumns.columnize(list, 2)
|
17
|
+
#=> [['a', 'd'], ['b', 'e'], ['c', nil]]
|
18
18
|
|
19
19
|
## Extension to Array
|
20
20
|
|
@@ -26,7 +26,7 @@ wherever you need columnizability (yes, that's totally a word):
|
|
26
26
|
end
|
27
27
|
|
28
28
|
['a', 'b', 'c'].in_columns(2)
|
29
|
-
#=> [['a', 'c'], ['b']]
|
29
|
+
#=> [['a', 'c'], ['b', nil]]
|
30
30
|
|
31
31
|
## Installation
|
32
32
|
|
data/lib/in_columns.rb
CHANGED
data/lib/in_columns/array_ext.rb
CHANGED
@@ -4,25 +4,57 @@ module InColumns
|
|
4
4
|
@elements = elements
|
5
5
|
end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
# Returns the necessary rows to distribute elements into the desired number
|
8
|
+
# of columns
|
9
|
+
#
|
10
|
+
# The result is a multidimensional array, so that:
|
11
|
+
#
|
12
|
+
# Columnizer.new(['a', 'b', 'c']).distribute(2)
|
13
|
+
# #=> [['a', 'c'], ['b', nil]]
|
14
|
+
def distribute(number_of_columns)
|
15
|
+
return [] if elements.empty?
|
16
|
+
distribute_elements(number_of_columns)
|
10
17
|
end
|
11
18
|
|
12
19
|
private
|
13
20
|
|
14
21
|
attr_reader :elements
|
15
22
|
|
16
|
-
def
|
17
|
-
(
|
23
|
+
def distribute_elements(number_of_columns)
|
24
|
+
number_of_rows = (number_of_elements.to_f / number_of_columns).ceil
|
25
|
+
counts = row_counts(number_of_columns)
|
26
|
+
|
27
|
+
columns = []
|
28
|
+
elements_to_distribute = elements.dup
|
29
|
+
|
30
|
+
while !elements_to_distribute.empty?
|
31
|
+
size_of_column = counts[columns.size]
|
32
|
+
elements_for_column = elements_to_distribute.shift(size_of_column)
|
33
|
+
columns << elements_for_column
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convert the columns to rows
|
37
|
+
first_column = columns.first
|
38
|
+
rest_columns = columns[1..-1]
|
39
|
+
columns = first_column.zip(*rest_columns)
|
18
40
|
end
|
19
41
|
|
20
|
-
def
|
21
|
-
elements.
|
22
|
-
|
23
|
-
|
42
|
+
def number_of_elements
|
43
|
+
elements.size
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns an array with an element for each column containing the number of
|
47
|
+
# rows for that column
|
48
|
+
def row_counts(number_of_columns)
|
49
|
+
per_column = (number_of_elements / number_of_columns).floor
|
50
|
+
counts = [per_column] * number_of_columns
|
51
|
+
|
52
|
+
left_overs = number_of_elements % number_of_columns
|
53
|
+
left_overs.times { |n|
|
54
|
+
counts[n] = counts[n] + 1
|
24
55
|
}
|
25
|
-
|
56
|
+
|
57
|
+
counts
|
26
58
|
end
|
27
59
|
end
|
28
60
|
end
|
data/lib/in_columns/version.rb
CHANGED
@@ -6,10 +6,13 @@ class ArrayExtensionTest < MiniTest::Unit::TestCase
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def test_distributes_array_into_columns
|
9
|
-
assert_equal [
|
9
|
+
assert_equal [
|
10
|
+
['a', 'c', 'e'],
|
11
|
+
['b', 'd', nil]
|
12
|
+
], ['a', 'b', 'c', 'd', 'e'].in_columns(3)
|
10
13
|
end
|
11
14
|
|
12
15
|
def test_works_for_empty_array
|
13
|
-
assert_equal [
|
16
|
+
assert_equal [], [].in_columns(3)
|
14
17
|
end
|
15
18
|
end
|
@@ -2,35 +2,119 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ColumnizerTest < MiniTest::Unit::TestCase
|
4
4
|
|
5
|
-
def
|
6
|
-
|
5
|
+
def test_distribute_1_into_3
|
6
|
+
input = ['a']
|
7
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
8
|
+
assert_equal [
|
9
|
+
['a']
|
10
|
+
], output
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_distribute_2_into_3
|
14
|
+
input = ['a', 'b']
|
15
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
16
|
+
assert_equal [
|
17
|
+
['a', 'b']
|
18
|
+
], output
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_distribute_4_into_3
|
22
|
+
input = ['a', 'b', 'c', 'd']
|
23
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
24
|
+
assert_equal [
|
25
|
+
['a', 'c', 'd'],
|
26
|
+
['b', nil, nil]
|
27
|
+
], output
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_distribute_5_into_3
|
31
|
+
input = ['a', 'b', 'c', 'd', 'e']
|
32
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
33
|
+
assert_equal [
|
34
|
+
['a', 'c', 'e'],
|
35
|
+
['b', 'd', nil]
|
36
|
+
], output
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_distribute_6_into_3
|
40
|
+
input = ['a', 'b', 'c', 'd', 'e', 'f']
|
41
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
42
|
+
assert_equal [
|
43
|
+
['a', 'c', 'e'],
|
44
|
+
['b', 'd', 'f']
|
45
|
+
], output
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_distribute_10_into_3
|
49
|
+
input = ('a'..'j').to_a
|
50
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
51
|
+
assert_equal [
|
52
|
+
['a', 'e', 'h'],
|
53
|
+
['b', 'f', 'i'],
|
54
|
+
['c', 'g', 'j'],
|
55
|
+
['d', nil, nil]
|
56
|
+
], output
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_distribute_nil_values
|
60
|
+
input = ['a', nil]
|
61
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
62
|
+
assert_equal [
|
63
|
+
['a', nil]
|
64
|
+
], output
|
7
65
|
end
|
8
66
|
|
9
67
|
def test_does_not_modify_input
|
10
68
|
input = ['a', 'b', 'c']
|
11
|
-
InColumns::Columnizer.new(input).
|
69
|
+
InColumns::Columnizer.new(input).distribute(2)
|
12
70
|
assert_equal ['a', 'b', 'c'], input
|
13
71
|
end
|
14
72
|
|
73
|
+
def test_returns_empty_array
|
74
|
+
assert_equal [], InColumns::Columnizer.new([]).distribute(3)
|
75
|
+
end
|
76
|
+
|
15
77
|
def test_distribute_exact_elements
|
16
78
|
input = ['a', 'b']
|
17
|
-
output = InColumns::Columnizer.new(input).
|
18
|
-
assert_equal [
|
79
|
+
output = InColumns::Columnizer.new(input).distribute(2)
|
80
|
+
assert_equal [
|
81
|
+
['a', 'b']
|
82
|
+
], output
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_distribute_into_one_column
|
86
|
+
input = ['a', 'b']
|
87
|
+
output = InColumns::Columnizer.new(input).distribute(1)
|
88
|
+
assert_equal [
|
89
|
+
['a'],
|
90
|
+
['b']
|
91
|
+
], output
|
19
92
|
end
|
20
93
|
|
21
94
|
def test_distribute_too_many_elements
|
22
95
|
input = ['a', 'b', 'c', 'd', 'e']
|
23
|
-
output = InColumns::Columnizer.new(input).
|
24
|
-
assert_equal [
|
96
|
+
output = InColumns::Columnizer.new(input).distribute(2)
|
97
|
+
assert_equal [
|
98
|
+
['a', 'd'],
|
99
|
+
['b', 'e'],
|
100
|
+
['c', nil]
|
101
|
+
], output
|
102
|
+
end
|
25
103
|
|
26
|
-
|
27
|
-
|
104
|
+
def test_columns_should_be_of_equalish_height
|
105
|
+
input = ('a'..'j').to_a
|
106
|
+
output = InColumns::Columnizer.new(input).distribute(3)
|
107
|
+
assert_equal [
|
108
|
+
['a', 'e', 'h'],
|
109
|
+
['b', 'f', 'i'],
|
110
|
+
['c', 'g', 'j'],
|
111
|
+
['d', nil, nil]
|
112
|
+
], output
|
28
113
|
end
|
29
114
|
|
30
|
-
def
|
115
|
+
def test_can_be_run_multiple_times
|
31
116
|
input = ['a', 'b']
|
32
|
-
|
33
|
-
assert_equal
|
117
|
+
columnizer = InColumns::Columnizer.new(input)
|
118
|
+
assert_equal columnizer.distribute(3), columnizer.distribute(3)
|
34
119
|
end
|
35
|
-
|
36
120
|
end
|
data/test/in_columns_test.rb
CHANGED
@@ -2,6 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class InColumnsTest < MiniTest::Unit::TestCase
|
4
4
|
def test_distributes_arrays_into_columns
|
5
|
-
assert_equal [
|
5
|
+
assert_equal [
|
6
|
+
['a', 'c', 'e'],
|
7
|
+
['b', 'd', nil]
|
8
|
+
], InColumns.columnize(['a', 'b', 'c', 'd', 'e'], 3)
|
6
9
|
end
|
7
10
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: in_columns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jakob Skjerning
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
81
|
version: '0'
|
82
82
|
requirements: []
|
83
83
|
rubyforge_project:
|
84
|
-
rubygems_version: 2.0.
|
84
|
+
rubygems_version: 2.0.6
|
85
85
|
signing_key:
|
86
86
|
specification_version: 4
|
87
87
|
summary: Distributes array elements into columns.
|
@@ -90,4 +90,3 @@ test_files:
|
|
90
90
|
- test/in_columns/columnizer_test.rb
|
91
91
|
- test/in_columns_test.rb
|
92
92
|
- test/test_helper.rb
|
93
|
-
has_rdoc:
|