array_floe 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ronen Barzel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ = array_floe
2
+
3
+ This small extension to ruby's Array class simplifies the reasonably-common
4
+ 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
+ iterating through the elements of an array. It's particularly handy for
6
+ generating CSS classes.
7
+
8
+ The gem provides two additional iterators, Array#each_with_floe and
9
+ Array#each_with_index_floe, that provide a "floe" object for each element
10
+ in the array:
11
+
12
+ ary.each_with_floe do |element, floe|
13
+ if floe.first?
14
+ puts "#{element} is the first element"
15
+ end
16
+ if floe.last?
17
+ puts "#{element} is the last element"
18
+ end
19
+ if floe.odd?
20
+ puts "#{element} is an odd-numbered element"
21
+ end
22
+ if floe.even?
23
+ puts "#{element} is an even-numbered element"
24
+ end
25
+ end
26
+
27
+ ary.each_with_index_floe do |element, i, floe|
28
+ assert_equal(i == 0, floe.first?)
29
+ assert_equal(i == ary.last, floe.last?)
30
+ assert_equal(i % 2 == 1, float.odd?)
31
+ assert_equal(i % 2 == 1, float.even?)
32
+ end
33
+
34
+ If no block is given, an enumerator is returned instead.
35
+
36
+ The "floe" object's <code>to_s</code> method returns a space-separated list
37
+ of "first", "last", "odd", and "even" as appropriate:
38
+
39
+ [:a, :b, :c, :d].each_with_floe.collect{|element, floe| floe.to_s} #=> [ "first even", "odd", "even", "last odd" ]
40
+
41
+ <code>floe.to_s</code> is particularly useful when generating HTML. For
42
+ example, this haml[http://haml-lang.com/] snippet:
43
+
44
+ %table
45
+ - [:a, :b, :c, :d].each_with_floe do |row, floe|
46
+ %tr{:class => floe}
47
+ %td= row
48
+
49
+ would yield this HTML snippet:
50
+
51
+ <table>
52
+ <tr class="first even">
53
+ <td>a</td>
54
+ </tr>
55
+ <tr class="odd">
56
+ <td>b</td>
57
+ </tr>
58
+ <tr class="even">
59
+ <td>c</td>
60
+ </tr>
61
+ <tr class="last odd">
62
+ <td>d</td>
63
+ </tr>
64
+ </table>
65
+
66
+ == Installing
67
+
68
+ <code>% sudo gem install array_floe<code>
69
+
70
+ or, if you're using bundler[http://gembundler.com/]:
71
+
72
+ gem "array_floe"
73
+
74
+ == Copyright
75
+
76
+ Released under the MIT License. See LICENSE.txt for further details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "array_floe"
16
+ gem.homepage = "http://github.com/ronen/array_floe"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Provides iterators Array#each_with_floe and Array#each_with_index_floe}
19
+ gem.description = %Q{
20
+ This small extension to ruby's Array class provides two additional
21
+ iterators, Array#each_with_floe and Array#each_with_index_floe, that
22
+ simplify the reasonably-common need to specially handle "floe"--i.e.,
23
+ first, last, odd, even--when iterating through the elements of an array.
24
+ It's particularly handy for generating CSS classes.
25
+ }
26
+ gem.email = "ronen@barzel.org"
27
+ gem.authors = ["ronen barzel"]
28
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
29
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
30
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
31
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
32
+ end
33
+ Jeweler::RubygemsDotOrgTasks.new
34
+
35
+ require 'rspec/core'
36
+ require 'rspec/core/rake_task'
37
+ RSpec::Core::RakeTask.new(:spec) do |spec|
38
+ spec.pattern = FileList['spec/**/*_spec.rb']
39
+ end
40
+
41
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
42
+ spec.pattern = 'spec/**/*_spec.rb'
43
+ spec.rcov = true
44
+ end
45
+
46
+ task :default => :spec
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "array_floe #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{array_floe}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["ronen barzel"]
12
+ s.date = %q{2011-04-09}
13
+ s.description = %q{
14
+ This small extension to ruby's Array class provides two additional
15
+ iterators, Array#each_with_floe and Array#each_with_index_floe, that
16
+ simplify the reasonably-common need to specially handle "floe"--i.e.,
17
+ first, last, odd, even--when iterating through the elements of an array.
18
+ It's particularly handy for generating CSS classes.
19
+ }
20
+ s.email = %q{ronen@barzel.org}
21
+ s.extra_rdoc_files = [
22
+ "LICENSE.txt",
23
+ "README.rdoc"
24
+ ]
25
+ s.files = [
26
+ ".document",
27
+ ".rspec",
28
+ "Gemfile",
29
+ "Gemfile.lock",
30
+ "LICENSE.txt",
31
+ "README.rdoc",
32
+ "Rakefile",
33
+ "VERSION",
34
+ "array_floe.gemspec",
35
+ "lib/array_floe.rb",
36
+ "spec/array_floe_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/ronen/array_floe}
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.5.0}
43
+ s.summary = %q{Provides iterators Array#each_with_floe and Array#each_with_index_floe}
44
+ s.test_files = [
45
+ "spec/array_floe_spec.rb",
46
+ "spec/spec_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
54
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
56
+ s.add_development_dependency(%q<rcov>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
67
+ s.add_dependency(%q<rcov>, [">= 0"])
68
+ end
69
+ end
70
+
@@ -0,0 +1,88 @@
1
+ class Array
2
+
3
+ # The class for the "floe" object that constructed by
4
+ # Array#each_with_floe and Array#each_with_index_floe
5
+ class FirstLastOddEven
6
+ def initialize(i, size) # :nodoc:
7
+ @first = i == 0
8
+ @last = i == size -1
9
+ @odd = i.odd?
10
+ end
11
+
12
+ # Returns true for the first element
13
+ def first?
14
+ @first
15
+ end
16
+
17
+ # Returns true for the last element
18
+ def last?
19
+ @last
20
+ end
21
+
22
+ # Returns true for odd-numbered elements
23
+ def odd?
24
+ @odd
25
+ end
26
+
27
+ # Returns true for even-numbered elements
28
+ def even?
29
+ !@odd
30
+ end
31
+
32
+ # Returns a space-separated list of "first", "last", "odd", and "even"
33
+ # as appropriate for the current element.
34
+ def to_s
35
+ @str ||= [@first && "first", @last && "last", @odd ? "odd" : "even"].select{|x| x}.join(' ')
36
+ end
37
+ end
38
+
39
+ # For each element in the array, calls the block with two arguments: the
40
+ # element and a "floe" object.
41
+ #
42
+ # ary.each_with_floe do |element, floe|
43
+ # if floe.first?
44
+ # puts "#{element} is the first element"
45
+ # end
46
+ # if floe.last?
47
+ # puts "#{element} is the last element"
48
+ # end
49
+ # if floe.odd?
50
+ # puts "#{element} is an odd-numbered element"
51
+ # end
52
+ # if floe.even?
53
+ # puts "#{element} is an even-numbered element"
54
+ # end
55
+ # end
56
+ #
57
+ # If no block is given, an enumerator is returned instead.
58
+ def each_with_floe() # :yields: element, floe
59
+ if block_given?
60
+ each_with_index do |element, i|
61
+ yield(element, FirstLastOddEven.new(i, size))
62
+ end
63
+ else
64
+ Enumerable::Enumerator.new(self, :each_with_floe)
65
+ end
66
+ end
67
+
68
+ # For each element in the array, calls the block with three arguments: the
69
+ # element, its index, and a "floe" object.
70
+ #
71
+ # ary.each_with_index_floe do |element, i, floe|
72
+ # assert_equal(i == 0, floe.first?)
73
+ # assert_equal(i == ary.last, floe.last?)
74
+ # assert_equal(i % 2 == 1, float.odd?)
75
+ # assert_equal(i % 2 == 1, float.even?)
76
+ # end
77
+ #
78
+ # If no block is given, an enumerator is returned instead.
79
+ def each_with_index_floe() # :yields: element, i, floe
80
+ if block_given?
81
+ each_with_index do |element, i|
82
+ yield(element, i, FirstLastOddEven.new(i, size))
83
+ end
84
+ else
85
+ Enumerable::Enumerator.new(self, :each_with_index_floe)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ArrayFloe" do
4
+
5
+ let (:a1) { Array(0...1) }
6
+ let (:a5) { Array(0...5) }
7
+
8
+ ["each_with_floe", "each_with_index_floe"].each do |method|
9
+
10
+ context method do
11
+
12
+ it "has correct arity" do
13
+ a5.send(method) do |*args|
14
+ args.size == (method == "each_with_index_floe" ? 3 : 2)
15
+ end
16
+ end
17
+
18
+ it "correctly reports first" do
19
+ a5.send(method) do |*args|
20
+ item = args.first
21
+ floe = args.last
22
+ floe.first?.should == (item == 0)
23
+ end
24
+ end
25
+
26
+ it "correctly reports last" do
27
+ a5.send(method) do |*args|
28
+ item = args.first
29
+ floe = args.last
30
+ floe.last?.should == (item == 4)
31
+ end
32
+ end
33
+
34
+ it "correctly reports odd" do
35
+ a5.send(method) do |*args|
36
+ item = args.first
37
+ floe = args.last
38
+ floe.odd?.should == (item % 2 == 1)
39
+ end
40
+ end
41
+
42
+ it "correctly reports even" do
43
+ a5.send(method) do |*args|
44
+ item = args.first
45
+ floe = args.last
46
+ floe.even?.should == (item % 2 == 0)
47
+ end
48
+ end
49
+
50
+ it "returns an enumerator" do
51
+ a5.send(method).should be_an_instance_of Enumerable::Enumerator
52
+ a5.send(method).find{|args| args.last.last? }.first.should == 4
53
+ end
54
+
55
+ it "iterates on the array entries" do
56
+ a5.send(method).collect(&:first).should == a5
57
+ end
58
+
59
+ if method == "each_with_index_floe"
60
+ it "passes the index" do
61
+ a5.send(method).collect{|args| args[1]}.should == a5
62
+ end
63
+ end
64
+
65
+ it "constructs floe strings" do
66
+ a5.send(method).collect{ |args| args.last.to_s }.should == [
67
+ "first even",
68
+ "odd",
69
+ "even",
70
+ "odd",
71
+ "last even"
72
+ ]
73
+ end
74
+
75
+ it "constructs first and last floe string when size == 1" do
76
+ a1.send(method).collect{|args| args.last.to_s}.should == [ "first last even" ]
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'array_floe'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_floe
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - ronen barzel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-09 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :development
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 0
34
+ version: 2.3.0
35
+ name: rspec
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ type: :development
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 0
50
+ version: 1.0.0
51
+ name: bundler
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ prerelease: false
55
+ type: :development
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 7
62
+ segments:
63
+ - 1
64
+ - 5
65
+ - 2
66
+ version: 1.5.2
67
+ name: jeweler
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ prerelease: false
71
+ type: :development
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ name: rcov
82
+ version_requirements: *id004
83
+ description: "\n\
84
+ This small extension to ruby's Array class provides two additional\n\
85
+ iterators, Array#each_with_floe and Array#each_with_index_floe, that\n\
86
+ simplify the reasonably-common need to specially handle \"floe\"--i.e.,\n\
87
+ first, last, odd, even--when iterating through the elements of an array.\n\
88
+ It's particularly handy for generating CSS classes.\n "
89
+ email: ronen@barzel.org
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files:
95
+ - LICENSE.txt
96
+ - README.rdoc
97
+ files:
98
+ - .document
99
+ - .rspec
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ - Rakefile
105
+ - VERSION
106
+ - array_floe.gemspec
107
+ - lib/array_floe.rb
108
+ - spec/array_floe_spec.rb
109
+ - spec/spec_helper.rb
110
+ has_rdoc: true
111
+ homepage: http://github.com/ronen/array_floe
112
+ licenses:
113
+ - MIT
114
+ post_install_message:
115
+ rdoc_options: []
116
+
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ requirements: []
138
+
139
+ rubyforge_project:
140
+ rubygems_version: 1.5.0
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Provides iterators Array#each_with_floe and Array#each_with_index_floe
144
+ test_files:
145
+ - spec/array_floe_spec.rb
146
+ - spec/spec_helper.rb