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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWE2ZjQ5NjNmMTBlYjU1NzVkYzAxZjE2MGY2NTY4NGVlOGY3MWUzNw==
4
+ YjFjYjY4YWJmYzAzYmQ3NWU0YTI3ZTEzYzBkZDAxMTBiMmE2OGE4NQ==
5
5
  data.tar.gz: !binary |-
6
- ZGI3Nzg2ZjQxNzA2ZGVkZjczYjRhY2ZmNzhkZWUxNGQ5N2IwZTkyNQ==
6
+ ZTdhMzJhNDg3YzM0NWU3YWNhYTJmYjU4ZmJhMzc2ZDBmMTllZWFiMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YzQ1MTg0Yzk0OWUzNDcxNGMzODAyMjBmYzNmODRlMTg4MzJkODJlODE4NDJk
10
- MzNmNTJlZGUwY2Q3Y2E4NjNkY2I3YmU5YzMzOGEwNWMxMmZmN2U3NzczM2Q1
11
- YTNkOTgzN2QwMjViNmZiMDNhZmIxM2MyZjNiNjhmN2JkODQ1MzY=
9
+ MzQ3Y2I1ZjhlYmQ5MjFhN2FmYzMyMWY2NWNjMmQ2N2YwMjQ4OTNlMWMwMzVk
10
+ NWNkNDYzMWY1NmRhNTgyY2Q0ZWZlMzcyMWUwZmU4ZThhMTVkNzY0NTFlZTFm
11
+ ZmQwNTc4YzlmYTk2NmUzZjY4NjNiMTYxYjFhZjA0ODZhNmM4MGI=
12
12
  data.tar.gz: !binary |-
13
- NWU1ZDgxZDAzMjZhMTFjN2MzZTQxYzc0NDVmNDI3OTk3MGFkZmU5YzU4MmUw
14
- MDY0OGY0NWNkNGUyOTI0NjRjNmIwZjViODhmOTcxNDg5OWUxMDE3ZGY3YmM0
15
- NWIxMDUxMjg0ODkyNDc2NjVjZDg2ZWExN2U0NTdiNDhiOTUyZDI=
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', 'd'], ['b', 'e'], ['c']]
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
 
@@ -5,6 +5,6 @@ require "in_columns/columnizer"
5
5
 
6
6
  module InColumns
7
7
  def self.columnize(elements, number_of_columns)
8
- Columnizer.new(elements).columns(number_of_columns)
8
+ Columnizer.new(elements).distribute(number_of_columns)
9
9
  end
10
10
  end
@@ -3,7 +3,7 @@ require_relative './columnizer'
3
3
  module InColumns
4
4
  module ArrayExt
5
5
  def in_columns(number_of_columns)
6
- InColumns::Columnizer.new(self).columns(number_of_columns)
6
+ InColumns::Columnizer.new(self).distribute(number_of_columns)
7
7
  end
8
8
  end
9
9
  end
@@ -4,25 +4,57 @@ module InColumns
4
4
  @elements = elements
5
5
  end
6
6
 
7
- def columns(number_of_columns)
8
- columns = create_columns(number_of_columns)
9
- distribute_elements(columns)
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 create_columns(number_of_columns)
17
- (1..number_of_columns).collect { [] }
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 distribute_elements(columns)
21
- elements.each_with_index { |element, index|
22
- column_number = index % columns.size
23
- columns[column_number] << element
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
- columns
56
+
57
+ counts
26
58
  end
27
59
  end
28
60
  end
@@ -1,3 +1,3 @@
1
1
  module InColumns
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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 [['a', 'c', 'e'], ['b', 'd']], ['a', 'b', 'c', 'd', 'e'].in_columns(2)
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 [[], [], []], [].in_columns(3)
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 test_returns_empty_array
6
- assert_equal [[],[],[]], InColumns::Columnizer.new([]).columns(3)
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).columns(2)
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).columns(2)
18
- assert_equal [['a'], ['b']], output
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).columns(2)
24
- assert_equal [['a', 'c', 'e'], ['b', 'd']], output
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
- output = InColumns::Columnizer.new(input).columns(3)
27
- assert_equal [['a', 'd'], ['b', 'e'], ['c']], output
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 test_distribute_too_few_elements
115
+ def test_can_be_run_multiple_times
31
116
  input = ['a', 'b']
32
- output = InColumns::Columnizer.new(input).columns(3)
33
- assert_equal [['a'], ['b'], []], output
117
+ columnizer = InColumns::Columnizer.new(input)
118
+ assert_equal columnizer.distribute(3), columnizer.distribute(3)
34
119
  end
35
-
36
120
  end
@@ -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 [['a', 'c', 'e'], ['b', 'd']], InColumns.columnize(['a', 'b', 'c', 'd', 'e'], 2)
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.1.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-15 00:00:00.000000000 Z
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.3
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: