array_floe 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -3
- data/VERSION +1 -1
- data/array_floe.gemspec +1 -1
- data/lib/array_floe.rb +4 -2
- data/spec/array_floe_spec.rb +84 -52
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
= array_floe
|
2
2
|
|
3
|
+
== Overview
|
4
|
+
|
3
5
|
This small extension to ruby's Array class simplifies the reasonably-common
|
4
6
|
need to specially handle "floe"--i.e., <b>f</b>irst, <b>l</b>ast, <b>o</b>dd, <b>e</b>ven--when
|
5
7
|
iterating through the elements of an array. It's particularly handy for
|
6
8
|
generating CSS classes.
|
7
9
|
|
10
|
+
Full Rdoc is available at http://rubydoc.info/gems/array_floe
|
11
|
+
|
12
|
+
== Usage
|
13
|
+
|
8
14
|
The gem provides two additional iterators, Array#each_with_floe and
|
9
15
|
Array#each_with_index_floe, that provide a "floe" object for each element
|
10
16
|
in the array:
|
@@ -38,7 +44,7 @@ of "first", "last", "odd", and "even" as appropriate:
|
|
38
44
|
|
39
45
|
[:a, :b, :c, :d].each_with_floe.collect{|element, floe| floe.to_s} #=> [ "first even", "odd", "even", "last odd" ]
|
40
46
|
|
41
|
-
<code>floe.to_s</code> is particularly useful when generating HTML. For
|
47
|
+
<code>floe.to_s</code> is particularly useful to construct CSS classes when generating HTML. For
|
42
48
|
example, this haml[http://haml-lang.com/] snippet:
|
43
49
|
|
44
50
|
%table
|
@@ -65,12 +71,18 @@ would yield this HTML snippet:
|
|
65
71
|
|
66
72
|
== Installing
|
67
73
|
|
68
|
-
|
74
|
+
Standard installation from http://rubygems.org/gems/array_floe
|
69
75
|
|
70
|
-
|
76
|
+
% sudo gem install array_floe
|
77
|
+
|
78
|
+
or, if you're using bundler[http://gembundler.com/], include this line in your Gemfile:
|
71
79
|
|
72
80
|
gem "array_floe"
|
73
81
|
|
82
|
+
== Ruby Versions
|
83
|
+
|
84
|
+
Tested on ruby 1.8.7 and 1.9.2
|
85
|
+
|
74
86
|
== Copyright
|
75
87
|
|
76
88
|
Released under the MIT License. See LICENSE.txt for further details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
data/array_floe.gemspec
CHANGED
data/lib/array_floe.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'enumerator' unless defined?(Enumerator)
|
2
|
+
|
1
3
|
class Array
|
2
4
|
|
3
5
|
# The class for the "floe" object that constructed by
|
@@ -61,7 +63,7 @@ class Array
|
|
61
63
|
yield(element, FirstLastOddEven.new(i, size))
|
62
64
|
end
|
63
65
|
else
|
64
|
-
|
66
|
+
to_enum(:each_with_floe)
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
@@ -82,7 +84,7 @@ class Array
|
|
82
84
|
yield(element, i, FirstLastOddEven.new(i, size))
|
83
85
|
end
|
84
86
|
else
|
85
|
-
|
87
|
+
to_enum(:each_with_index_floe)
|
86
88
|
end
|
87
89
|
end
|
88
90
|
end
|
data/spec/array_floe_spec.rb
CHANGED
@@ -2,80 +2,112 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "ArrayFloe" do
|
4
4
|
|
5
|
-
let (:
|
6
|
-
let (:a5) { Array(0...5) }
|
5
|
+
let (:enumerator) { defined?(Enumerator) ? Enumerator : Enumerable::Enumerator } # ruby 1.9 vs. 1.8
|
7
6
|
|
8
|
-
[
|
7
|
+
let (:a1) { [:a] }
|
8
|
+
let (:a5) { [:a, :b, :c, :d, :e] }
|
9
9
|
|
10
|
-
|
10
|
+
context "each_with_floe" do
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
12
|
+
it "correctly reports first" do
|
13
|
+
a5.each_with_floe do |item, floe|
|
14
|
+
floe.first?.should == (item == :a)
|
16
15
|
end
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
floe = args.last
|
22
|
-
floe.first?.should == (item == 0)
|
23
|
-
end
|
18
|
+
it "correctly reports last" do
|
19
|
+
a5.each_with_floe do |item, floe|
|
20
|
+
floe.last?.should == (item == :e)
|
24
21
|
end
|
22
|
+
end
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
floe = args.last
|
30
|
-
floe.last?.should == (item == 4)
|
31
|
-
end
|
24
|
+
it "correctly reports odd" do
|
25
|
+
a5.each_with_floe do |item, floe|
|
26
|
+
floe.odd?.should == [:b, :d].include?(item)
|
32
27
|
end
|
28
|
+
end
|
33
29
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
floe = args.last
|
38
|
-
floe.odd?.should == (item % 2 == 1)
|
39
|
-
end
|
30
|
+
it "correctly reports even" do
|
31
|
+
a5.each_with_floe do |item, floe|
|
32
|
+
floe.even?.should == [:a, :c, :e].include?(item)
|
40
33
|
end
|
34
|
+
end
|
41
35
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
36
|
+
it "returns an enumerator" do
|
37
|
+
a5.each_with_floe.should be_an_instance_of enumerator
|
38
|
+
end
|
39
|
+
|
40
|
+
it "iterates on the array entries" do
|
41
|
+
a5.each_with_floe.collect{|item, floe| item}.should == a5
|
42
|
+
end
|
43
|
+
|
44
|
+
it "constructs floe strings" do
|
45
|
+
a5.each_with_floe.collect{ |item, floe| floe.to_s }.should == [
|
46
|
+
"first even",
|
47
|
+
"odd",
|
48
|
+
"even",
|
49
|
+
"odd",
|
50
|
+
"last even"
|
51
|
+
]
|
52
|
+
end
|
53
|
+
|
54
|
+
it "constructs floe string with first last and even when size == 1" do
|
55
|
+
a1.each_with_floe.collect{|item, floe| floe.to_s}.should == [ "first last even" ]
|
56
|
+
end
|
57
|
+
end
|
49
58
|
|
50
|
-
|
51
|
-
|
52
|
-
|
59
|
+
context "each_with_index_floe" do
|
60
|
+
|
61
|
+
it "passes the index" do
|
62
|
+
a5.each_with_index_floe do |item, i, floe|
|
63
|
+
a5[i].should == item
|
53
64
|
end
|
65
|
+
end
|
54
66
|
|
55
|
-
|
56
|
-
|
67
|
+
it "correctly reports first" do
|
68
|
+
a5.each_with_index_floe do |item, i, floe|
|
69
|
+
floe.first?.should == (item == :a)
|
57
70
|
end
|
71
|
+
end
|
58
72
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
73
|
+
it "correctly reports last" do
|
74
|
+
a5.each_with_index_floe do |item, i, floe|
|
75
|
+
floe.last?.should == (item == :e)
|
63
76
|
end
|
77
|
+
end
|
64
78
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
"odd",
|
69
|
-
"even",
|
70
|
-
"odd",
|
71
|
-
"last even"
|
72
|
-
]
|
79
|
+
it "correctly reports odd" do
|
80
|
+
a5.each_with_index_floe do |item, i, floe|
|
81
|
+
floe.odd?.should == [:b, :d].include?(item)
|
73
82
|
end
|
83
|
+
end
|
74
84
|
|
75
|
-
|
76
|
-
|
85
|
+
it "correctly reports even" do
|
86
|
+
a5.each_with_index_floe do |item, i, floe|
|
87
|
+
floe.even?.should == [:a, :c, :e].include?(item)
|
77
88
|
end
|
89
|
+
end
|
90
|
+
|
91
|
+
it "returns an enumerator" do
|
92
|
+
a5.each_with_index_floe.should be_an_instance_of enumerator
|
93
|
+
end
|
94
|
+
|
95
|
+
it "iterates on the array entries" do
|
96
|
+
a5.each_with_index_floe.collect{|item, i, floe| item}.should == a5
|
97
|
+
end
|
98
|
+
|
99
|
+
it "constructs floe strings" do
|
100
|
+
a5.each_with_index_floe.collect{ |item, i, floe| floe.to_s }.should == [
|
101
|
+
"first even",
|
102
|
+
"odd",
|
103
|
+
"even",
|
104
|
+
"odd",
|
105
|
+
"last even"
|
106
|
+
]
|
107
|
+
end
|
78
108
|
|
109
|
+
it "constructs floe string with first last and even when size == 1" do
|
110
|
+
a1.each_with_index_floe.collect{|item, i, floe| floe.to_s}.should == [ "first last even" ]
|
79
111
|
end
|
80
112
|
|
81
113
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_floe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- ronen barzel
|