bicycle 0.0.1 → 0.1.0
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/Gemfile +0 -2
- data/Gemfile.lock +0 -10
- data/README.md +45 -7
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/bicycle.gemspec +2 -8
- data/lib/bicycle.rb +91 -6
- data/test/helper.rb +1 -4
- data/test/test_bicycle.rb +99 -9
- metadata +4 -34
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,21 +1,13 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
ansi (1.4.2)
|
5
4
|
git (1.2.5)
|
6
5
|
jeweler (1.6.4)
|
7
6
|
bundler (~> 1.0)
|
8
7
|
git (>= 1.2.5)
|
9
8
|
rake
|
10
9
|
minitest (3.0.1)
|
11
|
-
multi_json (1.3.6)
|
12
10
|
rake (0.9.2.2)
|
13
|
-
simplecov (0.6.4)
|
14
|
-
multi_json (~> 1.0)
|
15
|
-
simplecov-html (~> 0.5.3)
|
16
|
-
simplecov-html (0.5.3)
|
17
|
-
turn (0.9.5)
|
18
|
-
ansi
|
19
11
|
yard (0.6.8)
|
20
12
|
|
21
13
|
PLATFORMS
|
@@ -25,6 +17,4 @@ DEPENDENCIES
|
|
25
17
|
bundler
|
26
18
|
jeweler (~> 1.6.4)
|
27
19
|
minitest
|
28
|
-
simplecov
|
29
|
-
turn (~> 0.9.5)
|
30
20
|
yard (~> 0.6.0)
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
bicycle
|
2
2
|
=======
|
3
3
|
|
4
|
-
A
|
4
|
+
A convenience gem for cycling through a set of values, ported from the ['cycle'](http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-cycle) functionality found in Rails. It is independent of which framework you use so will run in Sinatra, Padrino or straight up Ruby itself.
|
5
5
|
|
6
6
|
Installation:
|
7
7
|
-------------
|
@@ -19,18 +19,56 @@ gem 'bicycle'
|
|
19
19
|
Usage
|
20
20
|
-----
|
21
21
|
|
22
|
-
|
22
|
+
Add the following to your app.rb file:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
include Bicycle
|
26
|
+
```
|
27
|
+
|
28
|
+
To use, call the cycle function with a set of values you wish to cycle through.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
<%= cycle("odd", "even") %>
|
32
|
+
```
|
33
|
+
|
34
|
+
For example:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
<% @items.each do |item| %>
|
38
|
+
<tr class="<%= cycle("odd", "even") %>">
|
39
|
+
<td>item</td>
|
40
|
+
</tr>
|
41
|
+
<% end %>
|
42
|
+
```
|
43
|
+
|
44
|
+
If you want to call multiple instances of the cycle function, you can by specifying a name attribute, like so: '
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
<%= cycle("red", "green", "blue", :name => "colours") %>
|
48
|
+
```
|
49
|
+
|
50
|
+
You can also reset a given cycle by calling:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
<% reset_cycle("colours") %>
|
54
|
+
```
|
23
55
|
|
24
56
|
For example:
|
25
57
|
|
26
58
|
```ruby
|
27
|
-
<table>
|
28
59
|
<% @items.each do |item| %>
|
29
|
-
|
30
|
-
|
31
|
-
|
60
|
+
<tr class="<%= cycle("odd", "even", :name => "row_class") -%>">
|
61
|
+
<td>
|
62
|
+
<% item.values.each do |value| %>
|
63
|
+
<%# Create a named cycle "colors" %>
|
64
|
+
<span style="color:<%= cycle("red", "green", "blue", :name => "colors") -%>">
|
65
|
+
<%= value %>
|
66
|
+
</span>
|
67
|
+
<% end %>
|
68
|
+
<% reset_cycle("colors") %>
|
69
|
+
</td>
|
70
|
+
</tr>
|
32
71
|
<% end %>
|
33
|
-
</table>
|
34
72
|
```
|
35
73
|
|
36
74
|
Contributing to bicycle
|
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
|
|
18
18
|
gem.homepage = "http://github.com/sleepingstu/bicycle"
|
19
19
|
gem.license = "MIT"
|
20
20
|
gem.summary = %Q{Why walk when you can cycle}
|
21
|
-
gem.description = %Q{A
|
21
|
+
gem.description = %Q{A convenience gem for cycling through a set of values, ported from the 'cycle' functionality found in Rails. It is independent of which framework you use so will run in Sinatra, Padrino or straight up Ruby itself.}
|
22
22
|
gem.email = "sleepingstu@gmail.com"
|
23
23
|
gem.authors = ["Stuart Chinery"]
|
24
24
|
# dependencies defined in Gemfile
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/bicycle.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "bicycle"
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Stuart Chinery"]
|
12
12
|
s.date = "2012-06-06"
|
13
|
-
s.description = "A
|
13
|
+
s.description = "A convenience gem for cycling through a set of values, ported from the 'cycle' functionality found in Rails. It is independent of which framework you use so will run in Sinatra, Padrino or straight up Ruby itself."
|
14
14
|
s.email = "sleepingstu@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
@@ -43,23 +43,17 @@ Gem::Specification.new do |s|
|
|
43
43
|
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
44
44
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
45
45
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
46
|
-
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
47
|
-
s.add_development_dependency(%q<turn>, ["~> 0.9.5"])
|
48
46
|
else
|
49
47
|
s.add_dependency(%q<minitest>, [">= 0"])
|
50
48
|
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
51
49
|
s.add_dependency(%q<bundler>, [">= 0"])
|
52
50
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
53
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
54
|
-
s.add_dependency(%q<turn>, ["~> 0.9.5"])
|
55
51
|
end
|
56
52
|
else
|
57
53
|
s.add_dependency(%q<minitest>, [">= 0"])
|
58
54
|
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
59
55
|
s.add_dependency(%q<bundler>, [">= 0"])
|
60
56
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
61
|
-
s.add_dependency(%q<simplecov>, [">= 0"])
|
62
|
-
s.add_dependency(%q<turn>, ["~> 0.9.5"])
|
63
57
|
end
|
64
58
|
end
|
65
59
|
|
data/lib/bicycle.rb
CHANGED
@@ -1,11 +1,96 @@
|
|
1
1
|
module Bicycle
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
3
|
+
# Creates a Cycle object whose _to_s_ method cycles through elements of an
|
4
|
+
# array every time it is called. This can be used for example, to alternate
|
5
|
+
# classes for table rows. You can use named cycles to allow nesting in loops.
|
6
|
+
# Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
|
7
|
+
# named cycle. The default name for a cycle without a +:name+ key is
|
8
|
+
# <tt>"default"</tt>. You can manually reset a cycle by calling reset_cycle
|
9
|
+
# and passing the name of the cycle. The current cycle string can be obtained
|
10
|
+
# anytime using the current_cycle method.
|
11
|
+
#
|
12
|
+
def cycle(*values)
|
13
|
+
if (values.last.instance_of? Hash)
|
14
|
+
params = values.pop
|
15
|
+
name = params[:name]
|
16
|
+
else
|
17
|
+
name = "default"
|
18
|
+
end
|
19
|
+
|
20
|
+
cycle = get_cycle(name)
|
21
|
+
unless cycle && cycle.values == values
|
22
|
+
cycle = set_cycle(name, Cycle.new(values))
|
23
|
+
end
|
24
|
+
cycle.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the current cycle string after a cycle has been started. Useful
|
28
|
+
# for complex table highlighting or any other design need which requires
|
29
|
+
# the current cycle string in more than one place.
|
30
|
+
#
|
31
|
+
def current_cycle(name = "default")
|
32
|
+
cycle = get_cycle(name)
|
33
|
+
cycle.current_value if cycle
|
34
|
+
end
|
35
|
+
|
36
|
+
# Resets a cycle so that it starts from the first element the next time
|
37
|
+
# it is called. Pass in +name+ to reset a named cycle.
|
38
|
+
#
|
39
|
+
def reset_cycle(name = "default")
|
40
|
+
cycle = get_cycle(name)
|
41
|
+
cycle.reset if cycle
|
42
|
+
end
|
43
|
+
|
44
|
+
class Cycle #:nodoc:
|
45
|
+
attr_reader :values
|
46
|
+
|
47
|
+
def initialize(values)
|
48
|
+
@values = values
|
49
|
+
reset
|
50
|
+
end
|
51
|
+
|
52
|
+
def reset
|
53
|
+
@index = 0
|
54
|
+
end
|
55
|
+
|
56
|
+
def current_value
|
57
|
+
@values[previous_index].to_s
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_s
|
61
|
+
value = @values[@index].to_s
|
62
|
+
@index = next_index
|
63
|
+
return value
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def next_index
|
69
|
+
step_index(1)
|
70
|
+
end
|
71
|
+
|
72
|
+
def previous_index
|
73
|
+
step_index(-1)
|
74
|
+
end
|
75
|
+
|
76
|
+
def step_index(n)
|
77
|
+
(@index + n) % @values.size
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
# The cycle helpers need to store the cycles in a place that is
|
84
|
+
# guaranteed to be reset every time a page is rendered, so it
|
85
|
+
# uses an instance variable of ActionView::Base.
|
86
|
+
def get_cycle(name)
|
87
|
+
@_cycles = Hash.new unless defined?(@_cycles)
|
88
|
+
return @_cycles[name]
|
89
|
+
end
|
90
|
+
|
91
|
+
def set_cycle(name, cycle_object)
|
92
|
+
@_cycles = Hash.new unless defined?(@_cycles)
|
93
|
+
@_cycles[name] = cycle_object
|
9
94
|
end
|
10
95
|
|
11
96
|
end
|
data/test/helper.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'simplecov'
|
2
|
-
SimpleCov.start
|
3
|
-
|
4
1
|
require 'rubygems'
|
5
2
|
require 'bundler'
|
6
3
|
begin
|
@@ -12,13 +9,13 @@ rescue Bundler::BundlerError => e
|
|
12
9
|
end
|
13
10
|
|
14
11
|
require 'minitest/spec'
|
15
|
-
begin; require 'turn/autorun'; rescue LoadError; end
|
16
12
|
|
17
13
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
18
14
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
19
15
|
require 'bicycle'
|
20
16
|
|
21
17
|
class MiniTest::Unit::TestCase
|
18
|
+
|
22
19
|
end
|
23
20
|
|
24
21
|
MiniTest::Unit.autorun
|
data/test/test_bicycle.rb
CHANGED
@@ -1,17 +1,107 @@
|
|
1
1
|
require 'helper'
|
2
|
+
include Bicycle
|
2
3
|
|
3
4
|
describe "When cycling" do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
describe "with values" do
|
6
|
+
before do
|
7
|
+
@values = []
|
8
|
+
for i in (0..3)
|
9
|
+
@values[i] = cycle("Foo", "Bar", "Huh?")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return a value in the cycle order" do
|
14
|
+
assert_equal "Foo", @values[0]
|
15
|
+
assert_equal "Bar", @values[1]
|
16
|
+
assert_equal "Huh?", @values[2]
|
17
|
+
assert_equal "Foo", @values[3]
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "and with new values" do
|
21
|
+
before do
|
22
|
+
@values = []
|
23
|
+
for i in (0..5)
|
24
|
+
@values[i] = cycle("I", "just", "want", "to", "ride", :name => "bikes")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return a new value in the cycle order" do
|
29
|
+
assert_equal "I", @values[0]
|
30
|
+
assert_equal "just", @values[1]
|
31
|
+
assert_equal "want", @values[2]
|
32
|
+
assert_equal "to", @values[3]
|
33
|
+
assert_equal "ride", @values[4]
|
34
|
+
assert_equal "I", @values[0]
|
35
|
+
end
|
8
36
|
end
|
9
37
|
end
|
10
38
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
39
|
+
describe "with multiple instances in one loop" do
|
40
|
+
before do
|
41
|
+
@values = []
|
42
|
+
@sub_values = []
|
43
|
+
for i in (0..3)
|
44
|
+
@values[i] = cycle("Foo", "Bar", "Huh?")
|
45
|
+
|
46
|
+
value = []
|
47
|
+
for j in (0..5)
|
48
|
+
value[j] = cycle("I", "just", "want", "to", "ride", :name => "bikes")
|
49
|
+
end
|
50
|
+
@sub_values[i] = value
|
51
|
+
|
52
|
+
reset_cycle("bikes")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return a value from the first cycle order" do
|
57
|
+
assert_equal "Foo", @values[0]
|
58
|
+
assert_equal "Bar", @values[1]
|
59
|
+
assert_equal "Huh?", @values[2]
|
60
|
+
assert_equal "Foo", @values[3]
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return a value from the first bike cycle order" do
|
64
|
+
assert_equal "I", @sub_values[0][0]
|
65
|
+
assert_equal "just", @sub_values[0][1]
|
66
|
+
assert_equal "want", @sub_values[0][2]
|
67
|
+
assert_equal "to", @sub_values[0][3]
|
68
|
+
assert_equal "ride", @sub_values[0][4]
|
69
|
+
assert_equal "I", @sub_values[0][0]
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return a value from the second bike cycle order" do
|
73
|
+
assert_equal "I", @sub_values[1][0]
|
74
|
+
assert_equal "just", @sub_values[1][1]
|
75
|
+
assert_equal "want", @sub_values[1][2]
|
76
|
+
assert_equal "to", @sub_values[1][3]
|
77
|
+
assert_equal "ride", @sub_values[1][4]
|
78
|
+
assert_equal "I", @sub_values[1][0]
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should return a value from the third bike cycle order" do
|
82
|
+
assert_equal "I", @sub_values[2][0]
|
83
|
+
assert_equal "just", @sub_values[2][1]
|
84
|
+
assert_equal "want", @sub_values[2][2]
|
85
|
+
assert_equal "to", @sub_values[2][3]
|
86
|
+
assert_equal "ride", @sub_values[2][4]
|
87
|
+
assert_equal "I", @sub_values[2][0]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "finding its current cycle" do
|
92
|
+
before do
|
93
|
+
@values = []
|
94
|
+
@current = []
|
95
|
+
for i in (0..5)
|
96
|
+
@values[i] = cycle("I", "just", "want", "to", "ride")
|
97
|
+
@current[i] = current_cycle
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should match whats cycled" do
|
102
|
+
for i in (0..5)
|
103
|
+
assert_equal @values[i], @current[i]
|
104
|
+
end
|
105
|
+
end
|
16
106
|
end
|
17
107
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bicycle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stuart Chinery
|
@@ -77,37 +77,7 @@ dependencies:
|
|
77
77
|
version_requirements: *id004
|
78
78
|
name: jeweler
|
79
79
|
prerelease: false
|
80
|
-
|
81
|
-
type: :development
|
82
|
-
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
84
|
-
requirements:
|
85
|
-
- - ">="
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
hash: 3
|
88
|
-
segments:
|
89
|
-
- 0
|
90
|
-
version: "0"
|
91
|
-
version_requirements: *id005
|
92
|
-
name: simplecov
|
93
|
-
prerelease: false
|
94
|
-
- !ruby/object:Gem::Dependency
|
95
|
-
type: :development
|
96
|
-
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
|
-
requirements:
|
99
|
-
- - ~>
|
100
|
-
- !ruby/object:Gem::Version
|
101
|
-
hash: 49
|
102
|
-
segments:
|
103
|
-
- 0
|
104
|
-
- 9
|
105
|
-
- 5
|
106
|
-
version: 0.9.5
|
107
|
-
version_requirements: *id006
|
108
|
-
name: turn
|
109
|
-
prerelease: false
|
110
|
-
description: A conveniance gem for cycling through a set of values, much like the 'cycle' functionality found in Rails, but is independant of which framework you use so will run in Sinatra, Padrino or straight up Ruby itself.
|
80
|
+
description: A convenience gem for cycling through a set of values, ported from the 'cycle' functionality found in Rails. It is independent of which framework you use so will run in Sinatra, Padrino or straight up Ruby itself.
|
111
81
|
email: sleepingstu@gmail.com
|
112
82
|
executables: []
|
113
83
|
|