in_columns 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +7 -0
- data/in_columns.gemspec +23 -0
- data/lib/in_columns.rb +10 -0
- data/lib/in_columns/array_ext.rb +9 -0
- data/lib/in_columns/columnizer.rb +28 -0
- data/lib/in_columns/version.rb +3 -0
- data/test/in_columns/array_ext_test.rb +15 -0
- data/test/in_columns/columnizer_test.rb +42 -0
- data/test/in_columns_test.rb +7 -0
- data/test/test_helper.rb +6 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NGIzNmUwZWFjZTE5MDgyN2Y4ZGUzMWRmYTE3MGQ4ODEzMGVkZGRkZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NDljMDg5NTk0NzNkMWM1ODhiMTBiMjc4ZTU2M2E1YTRmN2Y4MjgxZA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NGMzZDBiYjg4MDYxODdhNGU4M2M5ODE1NzU1ZDY4NmYxOWRiZjYzZDA3NGM3
|
10
|
+
NmFlOTA5MzM3ZmRmNjQ2NjVlODE0OGQyMTYyNDM1ZGQwNjZlMzY0OGVkYmQz
|
11
|
+
MDA5YzM2YTM0ODhkMGExODczODc1ZmM1ODZlMWIwNjFmY2EyYjg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTlhNzkzMjY4NzVhODAxZDliNTAwNjJjMzE3ZWRiZGRmNmNkMWI0ZmJmY2Uw
|
14
|
+
YzVkMzMwYTZlZjlkMzZhNzQ0YjRhY2UwMWQ5OGM1YzM1MjVkMWI0ZmU5ZjFh
|
15
|
+
ZjZiMTU5MmVhYmVjODE0MzllZDlkYmU1NGE4YTZiMTY5YjYzMDY=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jakob Skjerning
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# InColumns
|
2
|
+
|
3
|
+
InColumns distributes the elements of an array into a number of equal-height columns.
|
4
|
+
|
5
|
+
list = ['a', 'b', 'c', 'd', 'e']
|
6
|
+
InColumns.columnize(list, 2)
|
7
|
+
#=> [['a', 'c', 'e'], ['b', 'd']]
|
8
|
+
|
9
|
+
InColumns.columnize(list, 3)
|
10
|
+
#=> [['a', 'd'], ['b', 'e'], ['c']]
|
11
|
+
|
12
|
+
This is useful when you want to format a straight array into a number of
|
13
|
+
columns - say for displaying it to an end user.
|
14
|
+
|
15
|
+
## Extension to Array
|
16
|
+
|
17
|
+
If you feel so inclined, InColumns comes with a module that can be included
|
18
|
+
wherever you need columnizability (yes, that's totally a word):
|
19
|
+
|
20
|
+
class Array
|
21
|
+
include InColumns::ArrayExt
|
22
|
+
end
|
23
|
+
|
24
|
+
['a', 'b', 'c'].in_columns(2)
|
25
|
+
#=> [['a', 'c'], ['b']]
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
gem 'in_columns'
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install in_columns
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/in_columns.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'in_columns/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "in_columns"
|
8
|
+
spec.version = InColumns::VERSION
|
9
|
+
spec.authors = ["Jakob Skjerning"]
|
10
|
+
spec.email = ["jakob@mentalized.net"]
|
11
|
+
spec.description = %q{InColumns distributes the elements of an array into a number of equal-height columns.}
|
12
|
+
spec.summary = %q{Distributes array elements into columns.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/in_columns.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module InColumns
|
2
|
+
class Columnizer
|
3
|
+
def initialize(elements)
|
4
|
+
@elements = elements
|
5
|
+
end
|
6
|
+
|
7
|
+
def columns(number_of_columns)
|
8
|
+
columns = create_columns(number_of_columns)
|
9
|
+
distribute_elements(columns)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
attr_reader :elements
|
15
|
+
|
16
|
+
def create_columns(number_of_columns)
|
17
|
+
(1..number_of_columns).collect { [] }
|
18
|
+
end
|
19
|
+
|
20
|
+
def distribute_elements(columns)
|
21
|
+
elements.each_with_index { |element, index|
|
22
|
+
column_number = index % columns.size
|
23
|
+
columns[column_number] << element
|
24
|
+
}
|
25
|
+
columns
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ArrayExtensionTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
Array.send(:include, InColumns::ArrayExt)
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_distributes_array_into_columns
|
9
|
+
assert_equal [['a', 'c', 'e'], ['b', 'd']], ['a', 'b', 'c', 'd', 'e'].in_columns(2)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_works_for_empty_array
|
13
|
+
assert_equal [[], [], []], [].in_columns(3)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ColumnizerTest < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_returns_empty_array
|
6
|
+
assert_equal [[],[],[]], InColumns::Columnizer.new([]).columns(3)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_does_not_modify_input
|
10
|
+
input = ['a', 'b', 'c']
|
11
|
+
InColumns::Columnizer.new(input).columns(2)
|
12
|
+
assert_equal ['a', 'b', 'c'], input
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_distribute_exact_elements
|
16
|
+
input = ['a', 'b']
|
17
|
+
output = InColumns::Columnizer.new(input).columns(2)
|
18
|
+
assert_equal [['a'], ['b']], output
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_distribute_too_many_elements
|
22
|
+
input = ['a', 'b', 'c', 'd', 'e']
|
23
|
+
output = InColumns::Columnizer.new(input).columns(2)
|
24
|
+
assert_equal [['a', 'c', 'e'], ['b', 'd']], output
|
25
|
+
|
26
|
+
output = InColumns::Columnizer.new(input).columns(3)
|
27
|
+
assert_equal [['a', 'd'], ['b', 'e'], ['c']], output
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_distribute_too_few_elements
|
31
|
+
input = ['a', 'b']
|
32
|
+
output = InColumns::Columnizer.new(input).columns(3)
|
33
|
+
assert_equal [['a'], ['b'], []], output
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
class CoreExtensionTest < MiniTest::Unit::TestCase
|
39
|
+
def test_does_not_extend_array_by_default
|
40
|
+
assert ![].respond_to?(:in_columns)
|
41
|
+
end
|
42
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: in_columns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakob Skjerning
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: InColumns distributes the elements of an array into a number of equal-height
|
42
|
+
columns.
|
43
|
+
email:
|
44
|
+
- jakob@mentalized.net
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- in_columns.gemspec
|
55
|
+
- lib/in_columns.rb
|
56
|
+
- lib/in_columns/array_ext.rb
|
57
|
+
- lib/in_columns/columnizer.rb
|
58
|
+
- lib/in_columns/version.rb
|
59
|
+
- test/in_columns/array_ext_test.rb
|
60
|
+
- test/in_columns/columnizer_test.rb
|
61
|
+
- test/in_columns_test.rb
|
62
|
+
- test/test_helper.rb
|
63
|
+
homepage: ''
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.0.3
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Distributes array elements into columns.
|
87
|
+
test_files:
|
88
|
+
- test/in_columns/array_ext_test.rb
|
89
|
+
- test/in_columns/columnizer_test.rb
|
90
|
+
- test/in_columns_test.rb
|
91
|
+
- test/test_helper.rb
|
92
|
+
has_rdoc:
|