liquidity 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.
- data/README.rdoc +104 -0
- data/lib/liquidity.rb +2 -0
- data/lib/liquidity/liquidity.rb +61 -0
- metadata +64 -0
data/README.rdoc
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
= Motivation
|
2
|
+
|
3
|
+
Liquid layouts: the practice (among other things) of rendering a single ul/li group (like this):
|
4
|
+
|
5
|
+
<ul>
|
6
|
+
<li>1</li>
|
7
|
+
<li>2</li>
|
8
|
+
<li>3</li>
|
9
|
+
<li>4</li>
|
10
|
+
<li>5</li>
|
11
|
+
<li>6</li>
|
12
|
+
<li>7</li>
|
13
|
+
<li>8</li>
|
14
|
+
<li>9</li>
|
15
|
+
<li>10</li>
|
16
|
+
<li>11</li>
|
17
|
+
</ul>
|
18
|
+
|
19
|
+
into a multi-column layout like this:
|
20
|
+
|
21
|
+
1 2 3
|
22
|
+
4 5 6
|
23
|
+
7 8 9
|
24
|
+
10 11
|
25
|
+
|
26
|
+
using CSS like this:
|
27
|
+
|
28
|
+
<style type="text/css">
|
29
|
+
ul {width: 30%}
|
30
|
+
li {width: 33%; text-align: left; float: left}
|
31
|
+
</style>
|
32
|
+
|
33
|
+
|
34
|
+
But what if you wanted the data sorted by column, not by row? For this, CSS is inadequate: it's capable of flowing from left to right,
|
35
|
+
top to bottom. So in order to acheive a column sorting, we need to resort the data, like this:
|
36
|
+
|
37
|
+
<ul>
|
38
|
+
<li>1</li>
|
39
|
+
<li>5</li>
|
40
|
+
<li>9</li>
|
41
|
+
<li>2</li>
|
42
|
+
<li>6</li>
|
43
|
+
<li>10</li>
|
44
|
+
<li>3</li>
|
45
|
+
<li>7</li>
|
46
|
+
<li>11</li>
|
47
|
+
<li>4</li>
|
48
|
+
<li>8</li>
|
49
|
+
<li></li>
|
50
|
+
</ul>
|
51
|
+
|
52
|
+
Using the same CSS, this would render:
|
53
|
+
|
54
|
+
1 5 9
|
55
|
+
2 6 10
|
56
|
+
3 7 11
|
57
|
+
4 8
|
58
|
+
|
59
|
+
The liquidity gem adds a "column_sort" method to the Array class, making it simple to resort your collection for a column-sorted liquid
|
60
|
+
layout.
|
61
|
+
|
62
|
+
= Installation
|
63
|
+
|
64
|
+
Install the gem from rubygems.org:
|
65
|
+
|
66
|
+
# gem install liquidity
|
67
|
+
|
68
|
+
= Usage
|
69
|
+
|
70
|
+
Though I would imagine you would typically be using this for doing liquid layouts on the web, for the purposes of instruction, we'll
|
71
|
+
imagine that we have the following method for printing out an array:
|
72
|
+
|
73
|
+
require 'liquidity'
|
74
|
+
|
75
|
+
def print_matrix(a)
|
76
|
+
a.each_slice(3) do |slice|
|
77
|
+
puts slice.join("\t")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
If we passed off the array of (1..11) to it, we would see the following printed:
|
82
|
+
|
83
|
+
a = (1..11).to_a
|
84
|
+
print_matrix a
|
85
|
+
# ==>
|
86
|
+
1 2 3
|
87
|
+
4 5 6
|
88
|
+
7 8 9
|
89
|
+
10 11
|
90
|
+
|
91
|
+
Now, imagine that we wanted see the matrix where the data was sorted into columns, not rows. We can use the "column\_sort" method
|
92
|
+
provided by the "liquidity" gem:
|
93
|
+
|
94
|
+
a = (1..11).to_a
|
95
|
+
print_matrix a.column_sort(3)
|
96
|
+
# ==>
|
97
|
+
1 5 9
|
98
|
+
2 6 10
|
99
|
+
3 7 11
|
100
|
+
4 8
|
101
|
+
|
102
|
+
== Passing a block to column_sort
|
103
|
+
|
104
|
+
Just like the {Array::sort}[http://ruby-doc.org/core/classes/Array.html#M002185] method, you can pass a block to column_sort.
|
data/lib/liquidity.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
#Though I would imagine you would typically be using this for doing liquid layouts on the web, for the purposes of instruction, we'll
|
4
|
+
#imagine that we have the following method for printing out an array:
|
5
|
+
#
|
6
|
+
# require 'liquidity'
|
7
|
+
#
|
8
|
+
# def print_matrix(a)
|
9
|
+
# a.each_slice(3) do |slice|
|
10
|
+
# puts slice.join("\t")
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
#If we passed off the array of (1..11) to it, we would see the following printed:
|
15
|
+
#
|
16
|
+
# a = (1..11).to_a
|
17
|
+
# print_matrix a
|
18
|
+
# # ==>
|
19
|
+
# 1 2 3
|
20
|
+
# 4 5 6
|
21
|
+
# 7 8 9
|
22
|
+
# 10 11
|
23
|
+
#
|
24
|
+
#Now, imagine that we wanted see the matrix where the data was sorted into columns, not rows. We can use the "column\_sort" method
|
25
|
+
#provided by the "liquidity" gem:
|
26
|
+
#
|
27
|
+
# a = (1..11).to_a
|
28
|
+
# print_matrix a.column_sort(3)
|
29
|
+
# # ==>
|
30
|
+
# 1 5 9
|
31
|
+
# 2 6 10
|
32
|
+
# 3 7 11
|
33
|
+
# 4 8
|
34
|
+
#
|
35
|
+
#== Passing a block to column_sort
|
36
|
+
#
|
37
|
+
#Just like the {Array::sort}[http://ruby-doc.org/core/classes/Array.html#M002185] method, you can pass a block to column_sort.
|
38
|
+
def column_sort(num_columns, &block)
|
39
|
+
new_array = sort(&block)
|
40
|
+
new_length = new_array.length % num_columns == 0 ? new_array.length : new_array.length + num_columns - new_array.length % num_columns
|
41
|
+
new_array.pad(new_length).slices(num_columns).transpose.flatten
|
42
|
+
end
|
43
|
+
|
44
|
+
# pad an array to a new length, adding nil elements onto the end of the collection
|
45
|
+
def pad(new_length)
|
46
|
+
if new_length > length
|
47
|
+
self + ([nil] * (new_length - length))
|
48
|
+
else
|
49
|
+
self
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# return a new array, sliced up into a given number of smaller arrays.
|
54
|
+
# e.g.:
|
55
|
+
# [1,2,3,4].slices(2) #==> [[1,2],[3,4]]
|
56
|
+
def slices(num_slices)
|
57
|
+
array_of_slices = []
|
58
|
+
each_slice(length / num_slices) { |s| array_of_slices << s }
|
59
|
+
array_of_slices
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: liquidity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Matt Parker
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-04 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: This gem adds a "column_sort" method to the Array class.
|
22
|
+
email: moonmaster9000@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- README.rdoc
|
31
|
+
- lib/liquidity.rb
|
32
|
+
- lib/liquidity/liquidity.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/moonmaster9000/liquidity
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.6
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: A gem for making column-sorted liquid layouts simple.
|
63
|
+
test_files: []
|
64
|
+
|