random_data 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +13 -9
- data/README.txt +2 -2
- data/lib/random_data/dates.rb +31 -5
- data/lib/random_data/locations.rb +9 -3
- data/lib/random_data/names.rb +8 -53
- data/lib/random_data/version.rb +1 -1
- data/lib/random_data.rb +32 -1
- data/test/test_random_data.rb +102 -13
- metadata +47 -40
data/History.txt
CHANGED
@@ -1,17 +1,21 @@
|
|
1
|
-
== 1.
|
1
|
+
== 1.2.0 2007-09-17
|
2
2
|
|
3
|
-
|
4
|
-
* Separate methods for generating random male first names and random female first names
|
3
|
+
Thanks to Paul Barry and Hugh Sasse for some awesome patches!
|
5
4
|
|
6
|
-
*
|
7
|
-
*
|
8
|
-
*
|
5
|
+
* 1 major enhancement:
|
6
|
+
* Added method_missing to Random class, which looks for a method_name.dat and fetches a random line for you (see docs for details) (Hugh Sasse)
|
7
|
+
* added Random.date_between method to get a date between 2 given dates (Paul Barry)
|
8
|
+
* added Random.boolean method to get a random true or false (Paul Barry)
|
9
|
+
* added Random.number to get a random number less than a number or within a given range (Paul Barry)
|
9
10
|
|
10
|
-
|
11
|
+
* 1 minor enhancement:
|
12
|
+
* enhanced Random.date method to handle a Range as well as a Fixnum, which allows you to get a date guaranteed to be in the past (Paul Barry)
|
11
13
|
|
12
14
|
* 1 minor fix:
|
13
|
-
*
|
14
|
-
|
15
|
+
* Location sources organized into more understandable categories, for easier future expansion (Hugh Sasse)
|
16
|
+
* Fixed path of require statements in random_data.rb (Paul Barry)
|
17
|
+
* make initial never return nil, because if it returns nil then ContactInfo#email can thrown and error because it tries to call nil. (Paul Barry)
|
18
|
+
|
15
19
|
== 1.0.1 2007-09-17
|
16
20
|
|
17
21
|
* 1 minor fix:
|
data/README.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
=
|
1
|
+
= Random
|
2
2
|
|
3
3
|
This gem provides a Random singleton class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text. Instead of:
|
4
4
|
|
@@ -26,7 +26,7 @@ Random.date
|
|
26
26
|
Random.address_line_1, Random.address_line_2, Random.zipcode, Random.state, Random.country, Random.city,
|
27
27
|
|
28
28
|
=== Name Methods
|
29
|
-
Random.firstname, Random.
|
29
|
+
Random.firstname, Random.initial, Random.lastname,
|
30
30
|
|
31
31
|
=== Text Methods
|
32
32
|
Random.alphanumeric, Random.paragraphs
|
data/lib/random_data/dates.rb
CHANGED
@@ -6,15 +6,41 @@ module RandomData
|
|
6
6
|
|
7
7
|
module Dates
|
8
8
|
|
9
|
-
# Returns a date within a specified range of days
|
10
|
-
#
|
9
|
+
# Returns a date within a specified range of days. If dayrange is an Integer, then the date
|
10
|
+
# returned will be plus or minus half what you specify. The default is ten days, so by default
|
11
|
+
# you will get a date within plus or minus five days of today.
|
12
|
+
#
|
13
|
+
# If dayrange is a Range, then you will get a date the falls between that range
|
11
14
|
#
|
12
15
|
# Example:
|
13
16
|
#
|
14
|
-
#
|
15
|
-
|
17
|
+
# Random.date # => a Date +/- 5 days of today
|
18
|
+
# Random.date(20) # => a Date +/- 10 days of today
|
19
|
+
# Random.date(-60..-30) # => a Date between 60 days ago and 30 days ago
|
20
|
+
#
|
16
21
|
def date(dayrange=10)
|
17
|
-
|
22
|
+
if dayrange.is_a?(Range)
|
23
|
+
offset = rand(dayrange.max-dayrange.min) + dayrange.min
|
24
|
+
else
|
25
|
+
offset = rand(dayrange*2) - dayrange
|
26
|
+
end
|
27
|
+
Date.today + offset
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns a date within the specified Range. The Range can be Date or String objects.
|
31
|
+
#
|
32
|
+
#Example:
|
33
|
+
# min = Date.parse('1966-11-15')
|
34
|
+
# max = Date.parse('1990-01-01')
|
35
|
+
# Random.date(min..max) # => a Date between 11/15/1996 and 1/1/1990
|
36
|
+
# Random.date('1966-11-15'..'1990-01-01') # => a Date between 11/15/1996 and 1/1/1990
|
37
|
+
#
|
38
|
+
def date_between(range)
|
39
|
+
min_date = range.min.is_a?(Date) ? range.min : Date.parse(range.min)
|
40
|
+
max_date = range.max.is_a?(Date) ? range.max : Date.parse(range.max)
|
41
|
+
|
42
|
+
diff = (max_date - min_date).to_i
|
43
|
+
min_date + rand(diff)
|
18
44
|
end
|
19
45
|
|
20
46
|
end
|
@@ -4,8 +4,14 @@ module RandomData
|
|
4
4
|
|
5
5
|
module Locations
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
trees = %w( Acacia Beech Birch Cedar Cherry Chestnut Elm Larch Laurel
|
9
|
+
Linden Maple Oak Pine Rose Walnut Willow)
|
10
|
+
people = %w( Adams Franklin Jackson Jefferson Lincoln
|
11
|
+
Madison Washington Wilson)
|
12
|
+
places = %w( Highland Hill Park Woodland Sunset Virginia)
|
13
|
+
numbers = %w( 1st 2nd 4th 5th )
|
14
|
+
@@streetnames = trees + people + places + numbers
|
9
15
|
|
10
16
|
@@street_types = %w(St Ave Rd Blvd Trl Ter Rdg Pl Pkwy Ct Circle)
|
11
17
|
|
@@ -103,4 +109,4 @@ module RandomData
|
|
103
109
|
@@cities.rand
|
104
110
|
end
|
105
111
|
end
|
106
|
-
end
|
112
|
+
end
|
data/lib/random_data/names.rb
CHANGED
@@ -6,25 +6,12 @@ module RandomData
|
|
6
6
|
# Returns a random letter
|
7
7
|
|
8
8
|
def initial
|
9
|
-
|
10
|
-
choice = rand(26) + 66
|
11
|
-
|
12
|
-
if choice == 91
|
13
|
-
return nil
|
14
|
-
else
|
15
|
-
return choice.chr
|
16
|
-
end
|
17
|
-
|
9
|
+
('A'..'Z').to_a.rand
|
18
10
|
end
|
19
11
|
|
20
12
|
|
21
|
-
@@lastnames = %w(
|
22
|
-
|
23
|
-
GRANT HALL HARRIS HARRISON JACKSON JEFFRIES JEFFERSON JOHNSON JONES
|
24
|
-
KIRBY KIRK LAKE LEE LEWIS MARTIN MARTINEZ MAJOR MILLER MOORE OATES
|
25
|
-
PETERS PETERSON ROBERTSON ROBINSON RODRIGUEZ SMITH SMYTHE STEVENS
|
26
|
-
TAYLOR THATCHER THOMAS THOMPSON WALKER WASHINGTON WHITE WILLIAMS
|
27
|
-
WILSON YORKE)
|
13
|
+
@@lastnames = %w( SMITH JOHNSON WILLIAMS JONES BROWN DAVIS MILLER WILSON MOORE TAYLOR ANDERSON THOMAS JACKSON WHITE HARRIS MARTIN THOMPSON GARCIA MARTINEZ
|
14
|
+
ROBINSON CLARK RODRIGUEZ LEWIS LEE WALKER )
|
28
15
|
|
29
16
|
# Returns a random lastname
|
30
17
|
#
|
@@ -36,50 +23,18 @@ WILSON YORKE)
|
|
36
23
|
@@lastnames.rand.capitalize
|
37
24
|
end
|
38
25
|
|
39
|
-
@@
|
40
|
-
|
41
|
-
|
42
|
-
MATTHEW MICHAEL PAUL PHILIP RICHARD ROBERT ROGER RONALD SIMON
|
43
|
-
STEVEN TERRY THOMAS WILLIAM)
|
44
|
-
@@female_first_names = %w(ALISON ANN ANNA ANNE BARBARA BETTY BERYL
|
45
|
-
CAROL CHARLOTTE CHERYL DEBORAH DIANA DONNA DOROTHY ELIZABETH EVE FELICITY
|
46
|
-
FIONA HELEN HELENA JENNIFER JESSICA JUDITH KAREN KIMBERLY LAURA LINDA
|
47
|
-
LISA LUCY MARGARET MARIA MARY MICHELLE NANCY PATRICIA POLLY ROBYN RUTH
|
48
|
-
SANDRA SARAH SHARON SUSAN TABITHA URSULA VICTORIA WENDY)
|
49
|
-
|
50
|
-
@@first_names = @@male_first_names + @@female_first_names
|
26
|
+
@@firstnames = %w(JAMES JOHN ROBERT MICHAEL WILLIAM DAVID RICHARD CHARLES JOSEPH THOMAS CHRISTOPHER DANIEL PAUL MARK DONALD GEORGE KENNETH STEVEN EDWARD BRIAN
|
27
|
+
RONALD ANTHONY KEVIN JASON MARY PATRICIA LINDA BARBARA ELIZABETH JENNIFER MARIA SUSAN MARGARET DOROTHY LISA NANCY KAREN BETTY HELEN SANDRA
|
28
|
+
DONNA CAROL RUTH SHARON MICHELLE LAURA SARAH KIMBERLY DEBORAH)
|
51
29
|
|
52
|
-
|
53
|
-
# Returns a random firstname, male or female
|
30
|
+
# Returns a random firstname
|
54
31
|
#
|
55
32
|
# >> Random.firstname
|
56
33
|
#
|
57
34
|
# "Sandra"
|
58
35
|
|
59
36
|
def firstname
|
60
|
-
@@
|
61
|
-
end
|
62
|
-
|
63
|
-
|
64
|
-
# Returns a random male firstname
|
65
|
-
#
|
66
|
-
# >> Random.firstname_male
|
67
|
-
#
|
68
|
-
# "James"
|
69
|
-
|
70
|
-
def firstname_male
|
71
|
-
@@male_first_names.rand.capitalize
|
72
|
-
end
|
73
|
-
|
74
|
-
|
75
|
-
# Returns a random female firstname
|
76
|
-
#
|
77
|
-
# >> Random.firstname_female
|
78
|
-
#
|
79
|
-
# "Mary"
|
80
|
-
|
81
|
-
def firstname_female
|
82
|
-
@@female_first_names.rand.capitalize
|
37
|
+
@@firstnames.rand.capitalize
|
83
38
|
end
|
84
39
|
|
85
40
|
end
|
data/lib/random_data/version.rb
CHANGED
data/lib/random_data.rb
CHANGED
@@ -1,16 +1,47 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__)
|
2
2
|
|
3
3
|
require 'random_data/array_randomizer'
|
4
|
+
require 'random_data/booleans'
|
4
5
|
require 'random_data/contact_info'
|
5
6
|
require 'random_data/dates'
|
6
7
|
require 'random_data/locations'
|
7
8
|
require 'random_data/names'
|
9
|
+
require 'random_data/numbers'
|
8
10
|
require 'random_data/text'
|
9
11
|
|
10
12
|
class Random
|
13
|
+
extend RandomData::Booleans
|
11
14
|
extend RandomData::ContactInfo
|
12
15
|
extend RandomData::Dates
|
13
16
|
extend RandomData::Locations
|
14
17
|
extend RandomData::Names
|
18
|
+
extend RandomData::Numbers
|
15
19
|
extend RandomData::Text
|
16
|
-
|
20
|
+
|
21
|
+
# Looks for a file with the name methodname.dat, reads the lines from that file, then gives you a random line from that file
|
22
|
+
# Will also search your load path for the file. Raises an error if it can't find the file.
|
23
|
+
|
24
|
+
def self.method_missing(methodname)
|
25
|
+
thing = "#{methodname}.dat"
|
26
|
+
filename = find_path(thing)
|
27
|
+
|
28
|
+
if filename.nil?
|
29
|
+
super
|
30
|
+
else
|
31
|
+
array = []
|
32
|
+
File.open(filename, 'r') { |f| array = f.read.split(/[\r\n]+/) }
|
33
|
+
return array.rand
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def self.find_path(filename)
|
40
|
+
$:.each do |path|
|
41
|
+
new_path = File.join(path,filename)
|
42
|
+
return new_path if File.exist?(new_path)
|
43
|
+
end
|
44
|
+
return nil
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/test/test_random_data.rb
CHANGED
@@ -16,7 +16,7 @@ class TestRandomData < Test::Unit::TestCase
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_should_return_random_email
|
19
|
-
assert_equal "
|
19
|
+
assert_equal "iwalker@webmail.com", Random.email
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_should_return_random_date
|
@@ -30,7 +30,7 @@ class TestRandomData < Test::Unit::TestCase
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_should_return_random_address_line_1
|
33
|
-
assert_equal "38408
|
33
|
+
assert_equal "38408 Highland Blvd", Random.address_line_1
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_should_return_random_address_line_2
|
@@ -58,23 +58,15 @@ class TestRandomData < Test::Unit::TestCase
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_should_return_random_firstname
|
61
|
-
assert_equal "
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_should_return_random_firstname_male
|
65
|
-
assert_equal "Donald", Random.firstname_male
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_should_return_random_firstname_female
|
69
|
-
assert_equal "Charlotte", Random.firstname_female
|
61
|
+
assert_equal "Joseph", Random.firstname
|
70
62
|
end
|
71
63
|
|
72
64
|
def test_should_return_random_initial
|
73
|
-
assert_equal "
|
65
|
+
assert_equal "I", Random.initial
|
74
66
|
end
|
75
67
|
|
76
68
|
def test_should_return_random_lastname
|
77
|
-
assert_equal "
|
69
|
+
assert_equal "Moore", Random.lastname
|
78
70
|
end
|
79
71
|
|
80
72
|
def test_should_return_random_alphanumeric
|
@@ -94,4 +86,101 @@ class TestRandomData < Test::Unit::TestCase
|
|
94
86
|
assert_equal num_paragraphs, Random.paragraphs(num_paragraphs).scan(/\n\n/).size
|
95
87
|
end
|
96
88
|
|
89
|
+
def test_initial_should_not_return_nil
|
90
|
+
srand(11) #This would force the nil case in the old implementation
|
91
|
+
assert_not_nil Random.initial
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_should_return_random_date_using_range
|
95
|
+
Date.expects(:today).returns(Date.civil(2007,9,15))
|
96
|
+
assert_equal '1998-03-30', Random.date(-5000..-1000).to_s
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_should_return_random_date_between_using_range_of_dates
|
100
|
+
min = Date.parse('1966-11-15')
|
101
|
+
max = Date.parse('1990-01-01')
|
102
|
+
assert_equal '1982-04-25', Random.date_between(min..max).to_s
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_should_return_random_date_between_using_range_of_strings
|
106
|
+
assert_equal '1982-04-25', Random.date_between('1966-11-15'..'1990-01-01').to_s
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_should_return_random_boolean_true
|
110
|
+
srand(99)
|
111
|
+
assert_equal true, Random.boolean
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_should_return_random_boolean_false
|
115
|
+
assert_equal false, Random.boolean
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_should_return_random_number_less_than_10
|
119
|
+
assert_equal 8, Random.number(9)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_should_return_random_number_within_range
|
123
|
+
assert_equal 13, Random.number(5..15)
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
class TestRandomDataMethodMissing < Test::Unit::TestCase
|
129
|
+
|
130
|
+
def sports
|
131
|
+
@sports ||= %w(hockey basketball water\ polo five-a-side football judo pole\ vault steeple\ chase)
|
132
|
+
end
|
133
|
+
|
134
|
+
def lines
|
135
|
+
@lines ||= sports.join("\r\n")
|
136
|
+
end
|
137
|
+
|
138
|
+
def path
|
139
|
+
@path ||= File.join("lib","sport.dat")
|
140
|
+
end
|
141
|
+
|
142
|
+
def setup
|
143
|
+
@file = stub('file', :read => lines)
|
144
|
+
$:.stubs(:each).yields("lib")
|
145
|
+
File.stubs(:exist?).returns(true)
|
146
|
+
File.stubs(:open).yields(@file)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_should_search_load_path_for_file
|
150
|
+
$:.expects(:each).yields("lib")
|
151
|
+
Random.sport
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_should_join_path_with_methodname_dot_dat
|
155
|
+
File.expects(:join).with("lib","sport.dat").returns("lib/sport.dat")
|
156
|
+
Random.sport
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_should_test_for_existence_of_filename_matching_method_missing_call
|
160
|
+
File.expects(:exist?).with(path).returns(true)
|
161
|
+
Random.sport
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_should_call_super_if_unable_to_find_file
|
165
|
+
# can't test super call directly, so we test whether MethodMissing gets raised properly
|
166
|
+
File.stubs(:exist?).returns(false)
|
167
|
+
assert_raise(NoMethodError) { Random.horse }
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_should_open_file_matching_method_missing_call
|
171
|
+
File.expects(:open).with(path,"r").yields(@file)
|
172
|
+
Random.sport
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_should_read_lines_from_file
|
176
|
+
@file.expects(:read).returns(@lines)
|
177
|
+
Random.sport
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_should_return_random_line_from_a_file
|
181
|
+
srand(101)
|
182
|
+
assert_equal 'steeple chase', Random.sport
|
183
|
+
assert_equal 'five-a-side', Random.sport
|
184
|
+
end
|
185
|
+
|
97
186
|
end
|
metadata
CHANGED
@@ -1,33 +1,30 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.4
|
3
|
-
specification_version: 1
|
4
2
|
name: random_data
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.
|
7
|
-
|
8
|
-
summary: A Ruby gem that provides a Random singleton class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: mike@subelsky.com
|
12
|
-
homepage: http://random-data.rubyforge.org
|
13
|
-
rubyforge_project: random-data
|
14
|
-
description: A Ruby gem that provides a Random singleton class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
25
|
-
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
4
|
+
version: 1.2.0
|
5
|
+
platform: ""
|
29
6
|
authors:
|
30
7
|
- Mike Subelsky
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2007-11-29 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A Ruby gem that provides a Random singleton class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
|
17
|
+
email: mike@subelsky.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- History.txt
|
24
|
+
- License.txt
|
25
|
+
- Manifest.txt
|
26
|
+
- README.txt
|
27
|
+
- website/index.txt
|
31
28
|
files:
|
32
29
|
- History.txt
|
33
30
|
- License.txt
|
@@ -59,23 +56,33 @@ files:
|
|
59
56
|
- website/javascripts/rounded_corners_lite.inc.js
|
60
57
|
- website/stylesheets/screen.css
|
61
58
|
- website/template.rhtml
|
62
|
-
|
63
|
-
-
|
64
|
-
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://random-data.rubyforge.org
|
61
|
+
post_install_message:
|
65
62
|
rdoc_options:
|
66
63
|
- --main
|
67
64
|
- README.txt
|
68
|
-
|
69
|
-
-
|
70
|
-
|
71
|
-
|
72
|
-
-
|
73
|
-
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
78
79
|
requirements: []
|
79
80
|
|
80
|
-
|
81
|
-
|
81
|
+
rubyforge_project: random-data
|
82
|
+
rubygems_version: 0.9.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 2
|
85
|
+
summary: A Ruby gem that provides a Random singleton class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
|
86
|
+
test_files:
|
87
|
+
- test/test_helper.rb
|
88
|
+
- test/test_random_data.rb
|