rujitsu 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,49 @@
1
+ = Version 0.2.5 (2009-04-08)
2
+
3
+ Adding a few debugging methods in rujitsu/inspect
4
+ Added rujitsu/all to require everything in rujitsu
5
+
6
+ = Version 0.2 (2009-04-06)
7
+
8
+ Adding :exclude to Fixnum#random_*
9
+
10
+ = Version 0.1.9 (2009-02-20)
11
+
12
+ Added specifying only even or odd random numbers
13
+
14
+ = Version 0.1.8 (2009-01-24)
15
+
16
+ Added String#truncate + matching specs
17
+
18
+ = Version 0.1.7 (2008-12-08)
19
+
20
+ Added the limiter to Range#random_numbers
21
+
22
+ = Version 0.1.6 (2008-12-08)
23
+
24
+ Added a limiter to Fixnum#random_numbers
25
+
26
+ = Version 0.1.5 (2008-12-03)
27
+
28
+ Added random_vowels and random_consonants
29
+
30
+ = Version 0.1.4 (2008-11-27)
31
+
32
+ Added the grammar section
33
+
34
+ = Version 0.1.3 (2008-11-24)
35
+
36
+ Cleaned up invalid characters
37
+ Refactored generate_random_string_using method (thanks to zachinglis)
38
+
39
+ = Version 0.1.2 (2008-11-24)
40
+
41
+ Added full test suite
42
+
43
+ = Version 0.1.1 (2008-11-22)
44
+
45
+ Added range based random character generation
46
+
47
+ = Version 0.1.0 (2008-11-22)
48
+
49
+ Initial Release; random character generation, URL-friendly strings.
data/Manifest ADDED
@@ -0,0 +1,20 @@
1
+ CHANGELOG
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ lib/rujitsu.rb
6
+ lib/rujitsu/all.rb
7
+ lib/rujitsu/fixnum.rb
8
+ lib/rujitsu/grammar.rb
9
+ lib/rujitsu/inspect.rb
10
+ lib/rujitsu/numeric.rb
11
+ lib/rujitsu/range.rb
12
+ lib/rujitsu/string.rb
13
+ rujitsu.gemspec
14
+ spec/fixnum_spec.rb
15
+ spec/numeric_spec.rb
16
+ spec/range_spec.rb
17
+ spec/spec.opts
18
+ spec/spec_helper.rb
19
+ spec/string_spec.rb
20
+ tasks/rspec.rake
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ = rujitsu
2
+
3
+ A ruby gem with various helper methods to smooth out your Ruby development.
4
+
5
+ == Install
6
+
7
+ gem install brightbox-rujitsu --source http://gems.github.com
8
+
9
+ == Including
10
+
11
+ require "rujitsu"
12
+ require "rujitsu/grammar"
13
+
14
+ === Rails
15
+
16
+ To require in rails 2.2, add the following to your <tt>environment.rb</tt> file.
17
+
18
+ config.gem "brightbox-rujitsu", :lib => "rujitsu", :source => "http://gems.github.com"
19
+ config.gem "brightbox-rujitsu", :lib => "rujitsu/grammar", :source => "http://gems.github.com"
20
+
21
+ == Documentation
22
+
23
+ Run <tt>rake docs</tt> and open doc/index.html.
24
+
25
+ == Usage
26
+
27
+ === Generating random strings
28
+
29
+ The Fixnum class has a couple of extensions allowing you to generate random strings.
30
+
31
+ 5.random_letters
32
+ 5.random_numbers
33
+ 5.random_characters
34
+
35
+ You can also generate a variable length string.
36
+
37
+ (3..5).random_letters
38
+
39
+ This generates a string of random letters whose length is 3, 4 or 5 characters.
40
+
41
+ === URL-friendly strings
42
+
43
+ The String class has an extension that strips out URL-unfriendly characters.
44
+
45
+ ""$%hello!@ 123 there'".to_url # => "hello-123-there"
46
+
47
+ === Truncating strings
48
+
49
+ The String class has an extension that truncates it to a customisable length with a customisable suffix.
50
+
51
+ "this is a string".truncate(:length => 15) # => "this is a st..."
52
+
53
+ === Grammar
54
+
55
+ So far the grammar library just adds the method <tt>should_recieve</tt> for rspec assertions. Use it to find out what it does!
56
+
57
+
58
+ == Released under the MIT Licence
59
+
60
+ Copyright (c) 2008 Brightbox Systems Ltd
61
+
62
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
63
+
64
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
67
+
68
+ See http://www.brightbox.co.uk/ for contact details.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('rujitsu', '0.2.5') do | config |
6
+ config.description = 'Various helper methods to smooth over Ruby development'
7
+ config.url = 'http://github.com/rahoub/rujitsu'
8
+ config.author = 'Brightbox Systems Ltd'
9
+ config.email = 'hello@brightbox.co.uk'
10
+ config.ignore_pattern = ['tmp/*', 'script/*']
11
+ config.development_dependencies = []
12
+ end
13
+
14
+ desc "Generates manifest & gemspec in one go"
15
+ task :build => [:manifest, :build_gemspec]
16
+
17
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each do | rake_file |
18
+ load rake_file
19
+ end
data/lib/rujitsu.rb ADDED
@@ -0,0 +1,5 @@
1
+ base_dir = File.expand_path(File.dirname(__FILE__))
2
+ require base_dir + '/rujitsu/fixnum'
3
+ require base_dir + '/rujitsu/numeric'
4
+ require base_dir + '/rujitsu/range'
5
+ require base_dir + '/rujitsu/string'
@@ -0,0 +1,8 @@
1
+ #
2
+ # Requires everything in rujitsu/
3
+ #
4
+ dir_path = File.dirname(__FILE__)
5
+ Dir["#{dir_path}/*.rb"].each do | file |
6
+ next if file =~ /all.rb$/
7
+ require file
8
+ end
@@ -0,0 +1,80 @@
1
+ class Fixnum
2
+ # produce a string of N random vowels
3
+ def random_vowels opts={}
4
+ generate_random_string_using VOWELS, opts
5
+ end
6
+ # produce a string of N random consonants
7
+ def random_consonants opts={}
8
+ generate_random_string_using CONSONANTS, opts
9
+ end
10
+ # produce a string of N random letters
11
+ # 5.random_letters
12
+ def random_letters opts={}
13
+ generate_random_string_using LETTERS, opts
14
+ end
15
+ # produce a string of N random numbers
16
+ # 5.random_numbers
17
+ # optionally specific limits on the numbers
18
+ # 5.random_numbers(:from => 1, :to => 5)
19
+ def random_numbers( opts = {} )
20
+ # Then set some defaults, just in case
21
+ upper = opts[:to] || 9
22
+ lower = opts[:from] || 0
23
+ only = opts[:only] || :both
24
+
25
+ # And finally calculate the number
26
+ n = []
27
+ (self - 1).times do
28
+ i = (lower..upper).to_a.sort_by { rand }.first
29
+ n << i.to_s
30
+ end
31
+ # add the last digit according to :only
32
+ n << end_number_choices(only).select {|x| (lower <= x) && (x <= upper) }.sort_by { rand }.first.to_s
33
+ n.join("")
34
+ end
35
+ # produce a string of N random characters
36
+ # 5.random_characters
37
+ def random_characters opts={}
38
+ generate_random_string_using CHARACTERS, opts
39
+ end
40
+
41
+ private
42
+
43
+ VOWELS = %w(a e i o u)
44
+ LETTERS = ('a'..'z').to_a
45
+ CONSONANTS = LETTERS - VOWELS
46
+ NUMBERS = ('0'..'9').to_a
47
+ CHARACTERS = LETTERS + NUMBERS
48
+ EVENS = %w(0 2 4 6 8).map {|x| x.to_i }
49
+ ODDS = %w(1 3 5 7 9).map {|x| x.to_i }
50
+
51
+ def generate_random_string_using(legal_characters, opts={})
52
+ # Check if we have anything to exclude from the legal_characters
53
+ if (chars = opts[:except])
54
+ # Make sure we can iterate over it
55
+ chars = [chars] unless chars.respond_to?(:each)
56
+ # Run through them all and remove them
57
+ chars.each do | char |
58
+ legal_characters.delete(char)
59
+ end
60
+ end
61
+
62
+ upper_limit = legal_characters.size - 1
63
+
64
+ srand # seed rand
65
+ (1..self).collect do |num|
66
+ legal_characters[rand(upper_limit)]
67
+ end.join
68
+ end
69
+
70
+ def end_number_choices opt
71
+ case opt
72
+ when :even
73
+ EVENS
74
+ when :odd
75
+ ODDS
76
+ else
77
+ EVENS | ODDS
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,12 @@
1
+ module Spec
2
+ module Mocks
3
+ module Methods
4
+ class GrammarException < Exception; end
5
+
6
+ def should_recieve(sym, opts={}, &block)
7
+ raise GrammarException, "Oi! i before except after c."
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,24 @@
1
+ module Kernel
2
+ # If output is true then echo's,
3
+ # else just returns the name
4
+ def _cmd output=false
5
+ caller[0] =~ /`([^']*)'/
6
+ output ? p($1) : $1
7
+ end
8
+ private :_cmd
9
+ end
10
+
11
+ module ExtendObject
12
+ # Outputs all methods not inherited from Object
13
+ def simple_methods
14
+ self.methods.sort - Object.methods
15
+ end
16
+
17
+ # Matches your search string against all
18
+ # methods and returns array
19
+ def grep_method match
20
+ match = /#{match}/ unless match.is_a? Regexp
21
+ self.methods.grep(match)
22
+ end
23
+ end
24
+ Object.send(:include, ExtendObject)
@@ -0,0 +1,8 @@
1
+ class Numeric
2
+ # convert float values to "cents"
3
+ # my_value = 2.5
4
+ # my_value.to_cents # => 250
5
+ def to_cents
6
+ (self * 100.0).to_i
7
+ end
8
+ end
@@ -0,0 +1,26 @@
1
+ class Range
2
+ # pull a random element out of this range
3
+ def to_random_i
4
+ self.to_a.sort_by { rand }.first
5
+ end
6
+
7
+ # create a string of random letters whose length is one of the values in your range
8
+ # (3..4).random_letters # => returns a string or 3 or 4 random letters
9
+ def random_letters
10
+ self.to_random_i.random_letters
11
+ end
12
+
13
+ # create a string of random numbers whose length is one of the values in your range
14
+ # (3..4).random_numbers # => returns a string or 3 or 4 random numbers
15
+ def random_numbers opts = {}
16
+ self.to_random_i.random_numbers opts
17
+ end
18
+
19
+ # create a string of random characters whose length is one of the values in your range
20
+ # (3..4).random_characters # => returns a string or 3 or 4 random characters
21
+ def random_characters
22
+ self.to_random_i.random_characters
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,26 @@
1
+ class String
2
+ # Return a string that can be used as part of a url
3
+ # replaces basic "bad" characters with "-"
4
+ def to_url
5
+ self.downcase.gsub(/[^\-0-9a-z ]/, '').split.join('-')
6
+ end
7
+
8
+ # Truncates a string to the specified length,
9
+ # and appends suffix if required
10
+ #
11
+ # Options:
12
+ # * +length+ length to truncate string to. Includes the suffix in the length. Default is 50.
13
+ # * +suffix+ suffix to append to truncated string. Pass "" or false for no suffix. Default is "...".
14
+ #
15
+ def truncate opts = {}
16
+ opts[:length] ||= 50
17
+ opts[:suffix] = opts.has_key?(:suffix) ? opts[:suffix] : "..."
18
+ opts[:suffix] ||= ""
19
+ opts[:length] -= (opts[:suffix].length+1)
20
+ if opts[:length] > 0
21
+ self.length > opts[:length] ? self[0..opts[:length]] + opts[:suffix] : self
22
+ else
23
+ opts[:suffix][0..(opts[:length] += opts[:suffix].length)]
24
+ end
25
+ end
26
+ end
data/rujitsu.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rujitsu}
5
+ s.version = "0.2.5"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Brightbox Systems Ltd"]
9
+ s.date = %q{2009-11-10}
10
+ s.description = %q{Various helper methods to smooth over Ruby development}
11
+ s.email = %q{hello@brightbox.co.uk}
12
+ s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/rujitsu.rb", "lib/rujitsu/all.rb", "lib/rujitsu/fixnum.rb", "lib/rujitsu/grammar.rb", "lib/rujitsu/inspect.rb", "lib/rujitsu/numeric.rb", "lib/rujitsu/range.rb", "lib/rujitsu/string.rb", "tasks/rspec.rake"]
13
+ s.files = ["CHANGELOG", "Manifest", "README.rdoc", "Rakefile", "lib/rujitsu.rb", "lib/rujitsu/all.rb", "lib/rujitsu/fixnum.rb", "lib/rujitsu/grammar.rb", "lib/rujitsu/inspect.rb", "lib/rujitsu/numeric.rb", "lib/rujitsu/range.rb", "lib/rujitsu/string.rb", "rujitsu.gemspec", "spec/fixnum_spec.rb", "spec/numeric_spec.rb", "spec/range_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/string_spec.rb", "tasks/rspec.rake"]
14
+ s.homepage = %q{http://github.com/rahoub/rujitsu}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rujitsu", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{rujitsu}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Various helper methods to smooth over Ruby development}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
@@ -0,0 +1,222 @@
1
+ require File.join(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe Fixnum do
4
+
5
+ describe "random_vowels" do
6
+ it "should be a method" do
7
+ Fixnum.instance_methods.should include("random_vowels")
8
+ 5.should respond_to(:random_vowels)
9
+ end
10
+
11
+ it "should produce a string of random vowels" do
12
+ [ 5, 10, 15, 25, 29 ].each do |i|
13
+ str = i.random_vowels
14
+ str.should be_a_kind_of( String )
15
+ str.length.should == i
16
+ str.should match( /^[aeiou]+$/ )
17
+ end
18
+ end
19
+
20
+ it "should exclude a character" do
21
+ # Given there are 5 vowels, 5^3 should
22
+ # contain at least one instance of every vowel
23
+ str = (5**3).random_vowels(:except => "a")
24
+ str.should be_a_kind_of( String )
25
+ str.length.should == 5**3
26
+ str.should_not match(/a/)
27
+ end
28
+
29
+ it "should exclude characters" do
30
+ str = (5**3).random_vowels(:except => %w(i o u))
31
+ str.should be_a_kind_of( String )
32
+ str.length.should == 5**3
33
+ str.should_not match(/iou/)
34
+ end
35
+ end
36
+
37
+ describe "random_consonants" do
38
+ it "should be a method" do
39
+ Fixnum.instance_methods.should include("random_consonants")
40
+ 5.should respond_to(:random_consonants)
41
+ end
42
+
43
+ it "should produce a string of random consonants" do
44
+ [ 5, 10, 15, 25, 29 ].each do |i|
45
+ str = i.random_consonants
46
+
47
+ str.should be_a_kind_of( String )
48
+ str.length.should == i
49
+ str.should match( /^[bcdfghjklmnpqrstvwxyz]+$/ )
50
+ end
51
+ end
52
+
53
+ it "should exclude a character" do
54
+ # 26^5 should be large enough to get at least one
55
+ # instance of every character.
56
+ str = (26**2).random_consonants(:except => "c")
57
+
58
+ str.should be_a_kind_of( String )
59
+ str.length.should == (26**2)
60
+ str.should_not match(/c/)
61
+ end
62
+
63
+ it "should exclude characters" do
64
+ # 26^5 should be large enough to get at least one
65
+ # instance of every character.
66
+ str = (26**2).random_consonants(:except => %w(c d f))
67
+
68
+ str.should be_a_kind_of( String )
69
+ str.length.should == (26**2)
70
+ str.should_not match(/cdf/)
71
+ end
72
+ end
73
+
74
+ describe "random_letters" do
75
+ it "should be a method" do
76
+ Fixnum.instance_methods.should include("random_letters")
77
+ 5.should respond_to(:random_letters)
78
+ end
79
+
80
+ it "should produce a string of random letters" do
81
+ [ 5, 10, 15, 25, 29 ].each do |i|
82
+ str = i.random_letters
83
+ str.should be_a_kind_of( String )
84
+ str.length.should == i
85
+ str.should match( /^[a-z]+$/ )
86
+ end
87
+ end
88
+
89
+ it "should exclude a character" do
90
+ # 26^5 should be large enough to get at least one
91
+ # instance of every character.
92
+ str = (26**2).random_letters(:except => "d")
93
+
94
+ str.should be_a_kind_of( String )
95
+ str.length.should == (26**2)
96
+ str.should_not match(/d/)
97
+ end
98
+
99
+ it "should exclude characters" do
100
+ # 26^5 should be large enough to get at least one
101
+ # instance of every character.
102
+ str = (26**2).random_letters(:except => %w(c d f))
103
+
104
+ str.should be_a_kind_of( String )
105
+ str.length.should == (26**2)
106
+ str.should_not match(/cdf/)
107
+ end
108
+ end
109
+
110
+ describe "random_numbers" do
111
+ it "should be a method" do
112
+ Fixnum.instance_methods.should include("random_numbers")
113
+ 5.should respond_to(:random_numbers)
114
+ end
115
+
116
+ it "should produce a string of random numbers" do
117
+ [ 5, 10, 15, 25, 29 ].each do |i|
118
+ num = i.random_numbers
119
+ num.should be_a_kind_of( String )
120
+ num.length.should == i
121
+ num.should match( /^[0-9]+$/ )
122
+ end
123
+ end
124
+
125
+ it "should contain only the number 5 upwards" do
126
+ num = 5.random_numbers(:from => 5)
127
+
128
+ num.should be_a_kind_of(String)
129
+
130
+ # Check each digit is greater than or equal to 5
131
+ string_to_integers(num).each do |i|
132
+ i.should be_a_kind_of(Integer)
133
+ i.should >= 5
134
+ end
135
+ end
136
+
137
+ it "should contain on the number 5 downwards" do
138
+ num = 5.random_numbers(:to => 5)
139
+
140
+ num.should be_a_kind_of(String)
141
+
142
+ # Check each digit is lower than or equal to 5
143
+ string_to_integers(num).each do |i|
144
+ i.should be_a_kind_of(Integer)
145
+ i.should <= 5
146
+ end
147
+ end
148
+
149
+ it "should contain numbers between 4 and 6" do
150
+ num = 5.random_numbers(:from => 4, :to => 6)
151
+
152
+ num.should be_a_kind_of(String)
153
+
154
+ # Check each digit is lower than or equal to 4..
155
+ # ..and greater than or equal to 6
156
+ string_to_integers(num).each do |i|
157
+ i.should be_a_kind_of(Integer)
158
+ i.should >= 4
159
+ i.should <= 6
160
+ end
161
+ end
162
+
163
+ it "should create an even number" do
164
+ num = 5.random_numbers(:only => :even)
165
+
166
+ num.should be_a_kind_of(String)
167
+ (num.to_i % 2).should == 0
168
+ end
169
+
170
+ it "should create an odd number" do
171
+ num = 5.random_numbers(:only => :odd)
172
+
173
+ num.should be_a_kind_of(String)
174
+ (num.to_i % 2).should_not == 0
175
+ end
176
+
177
+ private
178
+
179
+ def string_to_integers(str)
180
+ str.split("").map {|x| x.to_i }
181
+ end
182
+ end
183
+
184
+ describe "random_characters" do
185
+ it "should be a method" do
186
+ Fixnum.instance_methods.should include("random_characters")
187
+ 5.should respond_to(:random_numbers)
188
+ end
189
+
190
+ it "should produce a string of random letters and numbers" do
191
+ [ 5, 10, 15, 25, 29 ].each do |i|
192
+ str = i.random_characters
193
+ str.should be_a_kind_of( String )
194
+ str.length.should == i
195
+ str.should match( /^[a-z0-9]+$/ )
196
+ end
197
+ end
198
+
199
+
200
+ it "should exclude a character" do
201
+ # 26^5 should be large enough to get at least one
202
+ # instance of every character.
203
+ str = (26**2).random_characters(:except => "c")
204
+
205
+ str.should be_a_kind_of( String )
206
+ str.length.should == (26**2)
207
+ str.should_not match(/c/)
208
+ end
209
+
210
+
211
+ it "should exclude characters" do
212
+ # 26^5 should be large enough to get at least one
213
+ # instance of every character.
214
+ str = (26**2).random_characters(:except => %w(c d f))
215
+
216
+ str.should be_a_kind_of( String )
217
+ str.length.should == (26**2)
218
+ str.should_not match(/cdf/)
219
+ end
220
+ end
221
+
222
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe Numeric, "to_cents" do
4
+ it "should be a method" do
5
+ Numeric.instance_methods.should include("to_cents")
6
+ Numeric.new.should respond_to(:to_cents)
7
+ end
8
+
9
+ it "should convert float values to \"cents\"" do
10
+ [ 5, 10, 15, 25, 29 ].each do |i|
11
+ i.to_cents.should == (i*100).to_i
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,135 @@
1
+ require File.join(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe Range do
4
+
5
+ describe "to_random_i" do
6
+ it "should be a method" do
7
+ Range.instance_methods.should include("to_random_i")
8
+ end
9
+
10
+ it "should return a random number from the range" do
11
+ setup_range
12
+
13
+ # First check with an absolute
14
+ result = @range.to_random_i
15
+ result.should be_a_kind_of(Fixnum)
16
+ result.should == 4
17
+
18
+ # And then one that is random
19
+ res = (3..5).to_random_i
20
+ res.should be_a_kind_of(Fixnum)
21
+ [3,4,5].should include(res)
22
+ end
23
+ end
24
+
25
+ describe "random_letters" do
26
+ it "should be a method" do
27
+ Range.instance_methods.should include("random_letters")
28
+ (0..1).should respond_to(:random_letters)
29
+ end
30
+
31
+ it "should generate a string of random letters" do
32
+ setup_range
33
+
34
+ str = @range.random_letters
35
+ str.length.should == 4
36
+ str.should match( /^[a-z]+$/ )
37
+ end
38
+ end
39
+
40
+ describe "random_numbers" do
41
+ it "should be a method" do
42
+ Range.instance_methods.should include("random_numbers")
43
+ (0..1).should respond_to(:random_numbers)
44
+ end
45
+
46
+ it "should generate a string of random numbers" do
47
+ setup_range
48
+
49
+ str = @range.random_numbers
50
+ str.length.should == 4
51
+ str.should match( /^[0-9]+$/ )
52
+ end
53
+
54
+ it "should contain only the number 5 upwards" do
55
+ setup_range
56
+
57
+ str = @range.random_numbers :from => 5
58
+ str.should be_a_kind_of(String)
59
+
60
+ string_to_integers(str).each do |i|
61
+ i.should be_a_kind_of(Integer)
62
+ i.should >= 5
63
+ end
64
+ end
65
+
66
+ it "should contain only the number 5 downwards" do
67
+ setup_range
68
+
69
+ str = @range.random_numbers :to => 5
70
+ str.should be_a_kind_of(String)
71
+
72
+ string_to_integers(str).each do |i|
73
+ i.should be_a_kind_of(Integer)
74
+ i.should <= 5
75
+ end
76
+ end
77
+
78
+ it "should contain only numbers between 4 and 6" do
79
+ setup_range
80
+
81
+ str = @range.random_numbers :from => 4, :to => 6
82
+ str.should be_a_kind_of(String)
83
+
84
+ string_to_integers(str).each do |i|
85
+ i.should be_a_kind_of(Integer)
86
+ i.should >= 4
87
+ i.should <= 6
88
+ end
89
+ end
90
+
91
+ it "should create an even number" do
92
+ setup_range
93
+
94
+ str = @range.random_numbers :only => :even
95
+
96
+ str.should be_a_kind_of(String)
97
+ (str.to_i % 2).should == 0
98
+ end
99
+
100
+ it "should create an odd number" do
101
+ setup_range
102
+
103
+ str = @range.random_numbers :only => :odd
104
+
105
+ str.should be_a_kind_of(String)
106
+ (str.to_i % 2).should_not == 0
107
+ end
108
+
109
+ private
110
+
111
+ def string_to_integers(str)
112
+ str.split("").map {|x| x.to_i }
113
+ end
114
+ end
115
+
116
+ describe "random_letters" do
117
+ it "should be a method" do
118
+ Range.instance_methods.should include("random_characters")
119
+ (0..1).should respond_to(:random_characters)
120
+ end
121
+
122
+ it "should generate a string of random letters" do
123
+ setup_range
124
+
125
+ str = @range.random_characters
126
+ str.length.should == 4
127
+ str.should match( /^[a-z0-9]+$/ )
128
+ end
129
+ end
130
+
131
+ def setup_range
132
+ @range = (3..5)
133
+ @range.should_receive(:to_random_i).and_return(4)
134
+ end
135
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__) + "/../lib/rujitsu")
@@ -0,0 +1,99 @@
1
+ require File.join(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe String, "to_url" do
4
+
5
+ it "should remove non-valid characters" do
6
+ "a!@£$".to_url.should == "a"
7
+ end
8
+
9
+ it "should convert spaces to hyphens" do
10
+ "a string".to_url.should == "a-string"
11
+ end
12
+
13
+ it "should downcase entire string" do
14
+ "THISISASTRING".to_url.should == "thisisastring"
15
+ end
16
+
17
+ it "should not touch [\\-0-9a-z]" do
18
+ "post-number-12345".to_url.should == "post-number-12345"
19
+ end
20
+
21
+ end
22
+
23
+ describe String, "truncate" do
24
+
25
+ it "should return shorter string unmodified with no params" do
26
+ str = 30.random_characters
27
+ # default length is 50
28
+ str.truncate.should == str
29
+ end
30
+
31
+ it "should return shorter string unmodified with length param" do
32
+ str = "0123456789" # length 10
33
+ trunced = str.truncate(:length => 30)
34
+ trunced.length.should == 10
35
+ trunced.should == str
36
+ end
37
+
38
+ it "should return shorter string unmodified with suffix param" do
39
+ str = "0123456789" # length 10
40
+ trunced = str.truncate(:suffix => "abcd")
41
+ trunced.length.should == 10
42
+ trunced.should == str
43
+ end
44
+
45
+ it "should return shorter string unmodified with length and suffix params" do
46
+ str = "0123456789" # length 10
47
+ trunced = str.truncate(:length => 30, :suffix => "abcd")
48
+ trunced.length.should == 10
49
+ trunced.should == str
50
+ end
51
+
52
+ it "should truncate longer string with no params" do
53
+ str = "this is a string with a really long length just to get above 50 characters"
54
+ # default length is 50
55
+ # default suffix is "..."
56
+ trunced = str.truncate
57
+ trunced.length.should == 50
58
+ trunced.should == "this is a string with a really long length just..."
59
+ end
60
+
61
+ it "should truncate longer string with length param" do
62
+ str = "123456789123456789" # length 11
63
+ trunced = str.truncate(:length => 10)
64
+ trunced.length.should == 10
65
+ trunced.should == "1234567..."
66
+ end
67
+
68
+ it "should truncate longer string with suffix param" do
69
+ str = "this is a string with a really long length just to get above 50 characters"
70
+ # default length is 50
71
+ trunced = str.truncate(:suffix => "..abc")
72
+ trunced.length.should == 50
73
+ trunced.should == "this is a string with a really long length ju..abc"
74
+ end
75
+
76
+ it "should truncate longer string with length and suffix param" do
77
+ str = "123456789123456789" # length 11
78
+ trunced = str.truncate(:length => 10, :suffix => "abde")
79
+ trunced.length.should == 10
80
+ trunced.should == "123456abde"
81
+ end
82
+
83
+ it "should return truncated string with no suffix if suffix param is false" do
84
+ str = "this is a string with a really long length just to get above 50 characters"
85
+ # default length is 50
86
+ trunced = str.truncate(:suffix => false)
87
+ trunced.length.should == 50
88
+ trunced.should == "this is a string with a really long length just to"
89
+ end
90
+
91
+ it "should return a string of length specified even when the suffix is longer" do
92
+ str = "this is a string" # => 16
93
+ suff = "this is a longer string" # => 23
94
+ trunced = str.truncate(:suffix => suff, :length => 10)
95
+ trunced.should == "this is a "
96
+ trunced.length.should == 10
97
+ end
98
+
99
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,24 @@
1
+ # Borrowed from http://github.com/rsim/ruby-plsql/tree/master/tasks/rspec.rake
2
+ # Github++
3
+ begin
4
+ require "spec"
5
+ rescue LoadError
6
+ require "rubygems"
7
+ require "spec"
8
+ end
9
+
10
+ begin
11
+ require "spec/rake/spectask"
12
+ rescue LoadError
13
+ puts <<-EOS
14
+ To use rspec for testing you must install rspec gem:
15
+ [sudo] gem install rspec
16
+ EOS
17
+ exit(0)
18
+ end
19
+
20
+ desc "Run the specs under spec/*"
21
+ Spec::Rake::SpecTask.new do |t|
22
+ t.spec_opts = ["--options", "spec/spec.opts"]
23
+ t.spec_files = FileList["spec/*_spec.rb"]
24
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rujitsu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.5
5
+ platform: ruby
6
+ authors:
7
+ - Brightbox Systems Ltd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-10 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Various helper methods to smooth over Ruby development
17
+ email: hello@brightbox.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - CHANGELOG
24
+ - README.rdoc
25
+ - lib/rujitsu.rb
26
+ - lib/rujitsu/all.rb
27
+ - lib/rujitsu/fixnum.rb
28
+ - lib/rujitsu/grammar.rb
29
+ - lib/rujitsu/inspect.rb
30
+ - lib/rujitsu/numeric.rb
31
+ - lib/rujitsu/range.rb
32
+ - lib/rujitsu/string.rb
33
+ - tasks/rspec.rake
34
+ files:
35
+ - CHANGELOG
36
+ - Manifest
37
+ - README.rdoc
38
+ - Rakefile
39
+ - lib/rujitsu.rb
40
+ - lib/rujitsu/all.rb
41
+ - lib/rujitsu/fixnum.rb
42
+ - lib/rujitsu/grammar.rb
43
+ - lib/rujitsu/inspect.rb
44
+ - lib/rujitsu/numeric.rb
45
+ - lib/rujitsu/range.rb
46
+ - lib/rujitsu/string.rb
47
+ - rujitsu.gemspec
48
+ - spec/fixnum_spec.rb
49
+ - spec/numeric_spec.rb
50
+ - spec/range_spec.rb
51
+ - spec/spec.opts
52
+ - spec/spec_helper.rb
53
+ - spec/string_spec.rb
54
+ - tasks/rspec.rake
55
+ has_rdoc: true
56
+ homepage: http://github.com/rahoub/rujitsu
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --line-numbers
62
+ - --inline-source
63
+ - --title
64
+ - Rujitsu
65
+ - --main
66
+ - README.rdoc
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "1.2"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: rujitsu
84
+ rubygems_version: 1.3.5
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Various helper methods to smooth over Ruby development
88
+ test_files: []
89
+