rfortune 0.2.0 → 0.2.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/CHANGELOG.rdoc +5 -0
- data/README.rdoc +8 -3
- data/lib/rfortune.rb +57 -1
- data/test/test_rfortune.rb +36 -0
- metadata +2 -4
- data/TODO.rdoc +0 -2
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -14,9 +14,10 @@ gem install rfortune
|
|
14
14
|
|
15
15
|
== SYNOPSIS:
|
16
16
|
|
17
|
-
To instantiate
|
18
|
-
|
19
|
-
|
17
|
+
To instantiate RFortune you have to give the relative path to a cookie
|
18
|
+
jar file as an argument. If there is no argument given or no such file
|
19
|
+
a new one will be created. File paths can be specified relative or
|
20
|
+
absolute or as the name of a file from beneath /usr/share/games/forunes.
|
20
21
|
|
21
22
|
cookies = RFortune.new 'cookie_jar_file'
|
22
23
|
|
@@ -24,6 +25,10 @@ To get a random cookie from the jar simply do:
|
|
24
25
|
|
25
26
|
cookies.random
|
26
27
|
|
28
|
+
== Links
|
29
|
+
|
30
|
+
WP: Fortune[http://en.wikipedia.org/wiki/Fortune_(Unix)]
|
31
|
+
|
27
32
|
== Copyright
|
28
33
|
|
29
34
|
Copyright (c) 2009 Helge Rausch. See LICENSE for details.
|
data/lib/rfortune.rb
CHANGED
@@ -109,7 +109,7 @@ class RFortune
|
|
109
109
|
|
110
110
|
# Depricated, alias for to_a
|
111
111
|
def all
|
112
|
-
|
112
|
+
|
113
113
|
to_a
|
114
114
|
|
115
115
|
end
|
@@ -146,5 +146,61 @@ class RFortune
|
|
146
146
|
|
147
147
|
end
|
148
148
|
|
149
|
+
# Calls block once for each cookie, passing that cookie as a parameter.
|
150
|
+
def each
|
151
|
+
|
152
|
+
@cookies.each { |c| yield c }
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
# Returns a random fortune from fortune's fortunes. If a block is
|
157
|
+
# given it passes one fortune per iteration to the block. Infinite.
|
158
|
+
# That means FOREVER! So be careful!
|
159
|
+
# Takes 'false' as an argument if you'd like to include offensive
|
160
|
+
# fortunes.
|
161
|
+
def self.random_fortune( no_offense = true )
|
162
|
+
|
163
|
+
cookie_jars = []
|
164
|
+
|
165
|
+
Dir.foreach( FORTUNES_PATH ) { |filename|
|
166
|
+
|
167
|
+
cookie_jars += [ filename ] if filename.match( /\A.*\.u8\Z/ )
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
unless no_offense
|
172
|
+
|
173
|
+
Dir.foreach( FORTUNES_PATH + 'off/' ) { |filename|
|
174
|
+
|
175
|
+
cookie_jars += [ 'off/' + filename ] if filename.match( /\.u8\Z/ )
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
if block_given?
|
182
|
+
|
183
|
+
fortunes = RFortune.new
|
184
|
+
|
185
|
+
cookie_jars.each { |jar|
|
186
|
+
|
187
|
+
fortunes += RFortune.new( jar )
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
begin
|
192
|
+
|
193
|
+
yield fortunes.random
|
194
|
+
|
195
|
+
end while true
|
196
|
+
|
197
|
+
else
|
198
|
+
|
199
|
+
RFortune.new( FORTUNES_PATH + cookie_jars[ rand( cookie_jars.size ) ] ).random
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
204
|
+
|
149
205
|
end
|
150
206
|
|
data/test/test_rfortune.rb
CHANGED
@@ -72,5 +72,41 @@ class RFortuneTest < Test::Unit::TestCase
|
|
72
72
|
|
73
73
|
end
|
74
74
|
|
75
|
+
def test_each
|
76
|
+
|
77
|
+
cookies = RFortune.new + [ 'foo', 'bar' ]
|
78
|
+
|
79
|
+
cookies.each { |c|
|
80
|
+
|
81
|
+
assert_match /\A(foo|bar)\Z/, c, 'Block for \'each\' method'
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_random_fortune
|
88
|
+
|
89
|
+
# Get regular fortune
|
90
|
+
assert_equal String, RFortune.random_fortune.class, 'Try to get a random fortune from fortune\'s fortunes'
|
91
|
+
|
92
|
+
# Include offensive fortunes
|
93
|
+
assert_equal String, RFortune.random_fortune( false ).class, 'Try to get a random fortune from fortune\'s fortunes'
|
94
|
+
|
95
|
+
# From block
|
96
|
+
i = 0
|
97
|
+
|
98
|
+
RFortune.random_fortune { |fortune|
|
99
|
+
|
100
|
+
assert_equal String, fortune.class, 'Try to get fortunes using a block.'
|
101
|
+
|
102
|
+
break if i == 3
|
103
|
+
|
104
|
+
i += 1
|
105
|
+
|
106
|
+
}
|
107
|
+
|
108
|
+
|
109
|
+
end
|
110
|
+
|
75
111
|
end
|
76
112
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rfortune
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Helge Rausch
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-12 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -21,7 +21,6 @@ extensions: []
|
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- README.rdoc
|
24
|
-
- TODO.rdoc
|
25
24
|
- CHANGELOG.rdoc
|
26
25
|
files:
|
27
26
|
- lib/rfortune.rb
|
@@ -29,7 +28,6 @@ files:
|
|
29
28
|
- test/test_rfortune.rb
|
30
29
|
- LICENSE
|
31
30
|
- README.rdoc
|
32
|
-
- TODO.rdoc
|
33
31
|
- VERSION
|
34
32
|
has_rdoc: true
|
35
33
|
homepage: http://github.com/tsujigiri/rfortune
|
data/TODO.rdoc
DELETED