jjulian-random_data 1.5.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/History.txt ADDED
@@ -0,0 +1,63 @@
1
+ == 1.5.0 2008-11-06
2
+ * 2 major enhancement:
3
+ * Totally cleaned up and reconfigured hoe using newgem 1.0's conventions
4
+ * Added Markov model text generator (courtesy of Hugh Sasse)
5
+
6
+ * 1 minor enhancement:
7
+ * Added roulette method to Array (courtesy of Hugh Sasse)
8
+
9
+ * 1 minor fix:
10
+ * Fixed newline bug in the name generator (courtesy of Harold Gimenez)
11
+
12
+ == 1.3.1 2008-06-06
13
+
14
+ * 2 minor enhancements:
15
+ * Added added more last and firstnames (courtesy of Hugh Sasse)
16
+ * Added firstname_male and firstname_female (courtesy of Hugh Sasse)
17
+
18
+ == 1.3.0 2008-05-25
19
+
20
+ All these enhancements are from Hugh Sasse. Thanks Hugh!
21
+
22
+ * 4 major enhancements:
23
+ * Added RandomData::Grammar, which lets you create simple random sentences (see rdocs)
24
+ * Added Random.bit and Random.bits
25
+ * Added Random.grammatical_construct
26
+ * Fixed up rdocs which had gotten out of date
27
+
28
+ * 2 minor enhancements:
29
+ * Added Random.uk_post_code
30
+ * Zipcodes should strings, not integers
31
+
32
+ == 1.2.1 2007-11-29
33
+
34
+ * 1 minor fix:
35
+ * Updated gem manifest
36
+
37
+ == 1.2.0 2007-11-29
38
+
39
+ Thanks to Paul Barry and Hugh Sasse for some awesome patches!
40
+
41
+ * 1 major enhancement:
42
+ * 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)
43
+ * added Random.date_between method to get a date between 2 given dates (Paul Barry)
44
+ * added Random.boolean method to get a random true or false (Paul Barry)
45
+ * added Random.number to get a random number less than a number or within a given range (Paul Barry)
46
+
47
+ * 1 minor enhancement:
48
+ * 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)
49
+
50
+ * 1 minor fix:
51
+ * Location sources organized into more understandable categories, for easier future expansion (Hugh Sasse)
52
+ * Fixed path of require statements in random_data.rb (Paul Barry)
53
+ * 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)
54
+
55
+ == 1.0.1 2007-09-17
56
+
57
+ * 1 minor fix:
58
+ * Not creating the Random class properly by extending it (vs. including modules)
59
+
60
+ == 1.0.0 2007-09-17
61
+
62
+ * 1 major enhancement:
63
+ * Initial release
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Mike Subelsky, subelsky.com
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.
data/Manifest.txt ADDED
@@ -0,0 +1,24 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/random_data.rb
7
+ lib/random_data/array_randomizer.rb
8
+ lib/random_data/booleans.rb
9
+ lib/random_data/contact_info.rb
10
+ lib/random_data/dates.rb
11
+ lib/random_data/grammar.rb
12
+ lib/random_data/locations.rb
13
+ lib/random_data/markov.rb
14
+ lib/random_data/names.rb
15
+ lib/random_data/numbers.rb
16
+ lib/random_data/text.rb
17
+ lib/random_data/version.rb
18
+ script/console
19
+ script/destroy
20
+ script/generate
21
+ script/txt2html
22
+ test/henry_v_prolog.txt
23
+ test/test_helper.rb
24
+ test/test_random_data.rb
data/README.rdoc ADDED
@@ -0,0 +1,130 @@
1
+ = random_data
2
+
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
+
5
+ <tt>foo.name = "John Doe"</tt>
6
+
7
+ You get:
8
+
9
+ <tt>foo.name = "#{Random.firstname} #{Random.initial} #{Random.lastname}"</tt>
10
+
11
+ == Installation
12
+
13
+ <tt>sudo gem install random_data</tt>
14
+
15
+ or get the gem manually from http://rubyforge.org/frs/?group_id=4458
16
+
17
+ == Methods
18
+
19
+ === Contact Methods
20
+ Random.phone, Random.international_phone, Random.email
21
+
22
+ === Time/Date Methods
23
+ Random.date, Random.date_between
24
+
25
+ === Location Methods
26
+ Random.address_line_1, Random.address_line_2, Random.zipcode, Random.uk_post_code, Random.state, Random.state_full, Random.country, Random.city
27
+
28
+ === Name Methods
29
+ Random.firstname, Random.firstname_male, Random.firstname_female, Random.initial, Random.lastname
30
+
31
+ === Text Methods
32
+ Random.alphanumeric, Random.paragraphs
33
+
34
+ === Grammatical Methods
35
+ Random.grammatical_construct
36
+
37
+ === Number Methods
38
+ Random.number, Random.bit, Random.bits
39
+
40
+ === Boolean Methods
41
+ Random.boolean
42
+
43
+ === Markov Generator Methods
44
+ Random.MarkovGenerator.new
45
+ Random.MarkovGenerator.insert
46
+ Random.MarkovGenerator.generate
47
+
48
+ === Array Extension
49
+ Array.rand
50
+ Array.roulette
51
+
52
+ === Choose From File Methods
53
+ Random implements a method_missing helper that will choose a line at random from a file in your load path. See Random::method_missing for details.
54
+
55
+ Note that some methods take parameters to bound or limit the amount of data returned. See RDocs for details.
56
+
57
+ == Examples of Some Methods
58
+
59
+ >>Random.alphanumeric
60
+ => "cfbutm3vYfhZXil0"
61
+
62
+ >> Random.alphanumeric(5)
63
+ => "XYVyn"
64
+
65
+ >> Random.paragraphs
66
+ => "Ulysses, fighting evil and tyranny with all his power and with all of his might. Ulysses, fighting evil and tyranny with all his power and with all
67
+ of his might. \n\nHe's got style, a groovy style, and a car that just won't stop. \n\n"
68
+
69
+ >> Random.paragraphs(3)
70
+ => "Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum. Down the road that's where I'll always be.
71
+ He's got style, a groovy style, and a car that just won't stop. I've gotten burned over Cheryl Tiegs and blown up for Raquel Welch, but when I end up in the
72
+ hay it's only hay hey hey. Hey there where ya goin, not exactly knowin'. \n\nLorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor
73
+ incididunt ut labore et dolore magna aliqua. I might jump an open drawbridge or Tarzan from a vine, beause I'm the unknown stuntman that makes Eastwood look so
74
+ fine. He just keeps on movin' and ladies keep improvin'. Rolling down to Dallas - who is providin my palace?. Every stop I make I make a new friend; Can't
75
+ stay for long just turn around and I'm gone again. \n\nAlways fighting all the evil forces bringing peace and justice to all. \n\n"
76
+
77
+ >> Random.firstname
78
+ => "Thomas"
79
+
80
+ >> Random.initial
81
+ => "E"
82
+
83
+ >> Random.lastname
84
+ => "Robinson"
85
+
86
+ >> Random.date
87
+ => Sun, 09 Sep 2007
88
+
89
+ (Returns an actual date object, shown here in its to_s form)
90
+
91
+ >> Random.date(1000)
92
+ => Sun, 26 Nov 2006
93
+
94
+ >> Random.email
95
+ => "zrodriguez@example.com"
96
+
97
+ >> Random.phone
98
+ => "564-103-8353"
99
+
100
+ >> Random.international_phone
101
+ => "011-24-37-9704"
102
+
103
+ >> Random.address_line_1
104
+ => "38367 Adams Rd"
105
+
106
+ >> Random.address_line_2
107
+ => "Lot 247"
108
+
109
+ >> Random.zipcode
110
+ => 22904
111
+
112
+ (Note that the zipcodes are totally random and may not be real zipcodes)
113
+
114
+ >> Random.state
115
+ => "MD"
116
+
117
+ >> Random.state_full
118
+ => "New Mexico"
119
+
120
+ >> Random.country
121
+ => "Philippines"
122
+
123
+ == Contact
124
+ Let me know if you have patches, comments, or suggestions: mailto:mike@subelsky.com
125
+
126
+ == Special Thanks
127
+ Major thanks to Hugh Sasse and {Paul Barry}[http://paulbarry.com/] for their contributions.
128
+
129
+ == Copyright
130
+ Copyright (c) 2008 {Mike Subelsky}[http://subelsky.com/], Baltimore, Maryland, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/random_data'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('random_data', RandomData::VERSION) do |p|
7
+ p.developer('Mike Subelsky', 'mike@subelsky.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.rubyforge_name = "random-data"
10
+ p.description = "A Ruby gem that provides a Random class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text."
11
+ p.summary = "A Ruby gem that provides a Random class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text."
12
+ p.email = 'random_data@mikeshop.net'
13
+ # p.remote_rdoc_dir = 'random-data'
14
+
15
+ # p.extra_deps = [
16
+ # ['activesupport','>= 2.0.2'],
17
+ # ]
18
+ p.extra_dev_deps = [
19
+ ['newgem', ">= #{::Newgem::VERSION}"]
20
+ ]
21
+
22
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
23
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
24
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
25
+ p.rsync_args = '-av --delete --ignore-errors'
26
+ end
27
+
28
+ require 'newgem/tasks' # load /tasks/*.rake
29
+ Dir['tasks/**/*.rake'].each { |t| load t }
30
+
31
+ # TODO - want other tests/tasks run by default? Add them to the list
32
+ # task :default => [:spec, :features]
@@ -0,0 +1,63 @@
1
+ dir = "#{File.dirname(__FILE__)}/random_data"
2
+
3
+ require "#{dir}/array_randomizer"
4
+ require "#{dir}/booleans"
5
+ require "#{dir}/contact_info"
6
+ require "#{dir}/dates"
7
+ require "#{dir}/locations"
8
+ require "#{dir}/names"
9
+ require "#{dir}/numbers"
10
+ require "#{dir}/text"
11
+ require "#{dir}/markov"
12
+ require "#{dir}/grammar"
13
+ require "#{dir}/version"
14
+
15
+ class Random
16
+ extend RandomData::Booleans
17
+ extend RandomData::ContactInfo
18
+ extend RandomData::Dates
19
+ extend RandomData::Grammar
20
+ extend RandomData::Locations
21
+ extend RandomData::Names
22
+ extend RandomData::Numbers
23
+ extend RandomData::Text
24
+
25
+ # Looks for a file in the load path with the name methodname.dat, reads the lines from that file, then gives you a random line from that file.
26
+ # Raises an error if it can't find the file. For example, given a file named "horse.dat" in your load path:
27
+ # >> Random.horse
28
+ # => "Stallion"
29
+ # >> Random.horse
30
+ # => "Pony"
31
+ # >> Random.horse
32
+ # => "Mare"
33
+ # >> Random.horse
34
+ # => "Clydesdale"
35
+ # >> Random.horse
36
+ # => "Stallion"
37
+ # >> Random.horse
38
+ # => "Mare"
39
+
40
+ def self.method_missing(methodname)
41
+ thing = "#{methodname}.dat"
42
+ filename = find_path(thing)
43
+
44
+ if filename.nil?
45
+ super
46
+ else
47
+ array = []
48
+ File.open(filename, 'r') { |f| array = f.read.split(/[\r\n]+/) }
49
+ return array.rand
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def self.find_path(filename)
56
+ $:.each do |path|
57
+ new_path = File.join(path,filename)
58
+ return new_path if File.exist?(new_path)
59
+ end
60
+ return nil
61
+ end
62
+
63
+ end
@@ -0,0 +1,65 @@
1
+ # Extends the Array class with a function used to randomly choose elements. Note that if you are using Rails, ActiveSupport recently was updated to include a similar
2
+ # function. This code checks for the presence of that method and does not try to replace it. They work the same though so no worries.
3
+
4
+ module RandomData
5
+ module ArrayRandomizer
6
+
7
+ # Randomly chooses an element from an array
8
+ # >> [1,2,3].rand = 3
9
+ # [].rand = nil
10
+
11
+ def rand
12
+ return self[Kernel.rand(self.size)]
13
+ end
14
+
15
+ # Takes an array of non-negative weights
16
+ # and returns the index selected by a
17
+ # roulette wheel weighted according to those
18
+ # weights.
19
+ # If a block is given then k is used to determine
20
+ # how many times the block is called. In this
21
+ # case nil is returned.
22
+ def roulette(k=1)
23
+ wheel = []
24
+ weight = 0
25
+ # Create the cumulative array.
26
+ self.each do |x|
27
+ raise "Illegal negative weight #{x}" if x < 0
28
+ wheel.push(weight += x)
29
+ end
30
+ # print "wheel is #{wheel.inspect}\n";
31
+ # print "weight is #{weight.inspect}\n";
32
+ raise "Array had all zero weights" if weight.zero?
33
+ wheel.push(weight + 1) #Add extra element
34
+ if block_given?
35
+ k.times do
36
+ r = Kernel.rand() # so we don't pick up that from array.
37
+ # print "r is #{r.inspect}\n";
38
+ roll = weight.to_f * r
39
+ # print "roll is #{roll.inspect}\n";
40
+ 0.upto(self.size - 1) do |i|
41
+ if wheel[i+1] > roll
42
+ yield i
43
+ break
44
+ end # if
45
+ end # upto
46
+ end # if block_given?
47
+ return nil
48
+ else
49
+ r = Kernel.rand() # so we don't pick up that from array.
50
+ # print "r is #{r.inspect}\n";
51
+ roll = weight.to_f * r
52
+ # print "roll is #{roll.inspect}\n";
53
+ 0.upto(self.size - 1) do |i|
54
+ return i if wheel[i+1] > roll
55
+ end
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ unless Array.respond_to?(:rand)
64
+ Array.send :include, RandomData::ArrayRandomizer
65
+ end
@@ -0,0 +1,10 @@
1
+ module RandomData
2
+
3
+ module Booleans
4
+ #Returns true or false randomly
5
+ def boolean
6
+ rand(2) == 1 ? true : false
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,24 @@
1
+ module RandomData
2
+ # Methods for randomly generating contact data like phone numbers and e-mail addresses
3
+
4
+ module ContactInfo
5
+
6
+ # Returns a randomly-generated string of digits that roughly resembles a US telephone number. Not guaranteed to be a valid area code.
7
+ def phone
8
+ "#{rand(900) + 100}-#{rand(900)+100}-#{rand(10000)+1000}"
9
+ end
10
+
11
+ # Returns a randomly-generated string of digits that roughly resembles an international telephone number as dialed from the US.
12
+ # Not guaranteed to be a valid number but just good enough to get some sample data going.
13
+
14
+ def international_phone
15
+ "011-#{rand(100) + 1}-#{rand(100)+10}-#{rand(10000)+1000}"
16
+ end
17
+
18
+ # Returns an e-mail address of the form "{first_initial}{last_name}@{domain}"
19
+ def email
20
+ domains = %w(yahoo.com gmail.com privacy.net webmail.com msn.com hotmail.com example.com privacy.net)
21
+ "#{(initial + lastname).downcase}\@#{domains.rand}"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,47 @@
1
+ require 'date'
2
+
3
+ module RandomData
4
+
5
+ # Defines methods for random date generation
6
+
7
+ module Dates
8
+
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
14
+ #
15
+ # Example:
16
+ #
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
+ #
21
+ def date(dayrange=10)
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)
44
+ end
45
+
46
+ end
47
+ end