faker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.1.0 2007-11-22
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Benjamin Curtis
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.
@@ -0,0 +1,29 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ config/hoe.rb
7
+ config/requirements.rb
8
+ lib/facets/random.rb
9
+ lib/faker.rb
10
+ lib/faker/address.rb
11
+ lib/faker/company.rb
12
+ lib/faker/internet.rb
13
+ lib/faker/name.rb
14
+ lib/faker/phone_number.rb
15
+ lib/faker/version.rb
16
+ script/destroy
17
+ script/generate
18
+ script/txt2html
19
+ setup.rb
20
+ tasks/deployment.rake
21
+ tasks/environment.rake
22
+ tasks/website.rake
23
+ test/test_faker.rb
24
+ test/test_helper.rb
25
+ website/index.html
26
+ website/index.txt
27
+ website/javascripts/rounded_corners_lite.inc.js
28
+ website/stylesheets/screen.css
29
+ website/template.rhtml
@@ -0,0 +1,8 @@
1
+ = Faker
2
+
3
+ A port of Perl's Data::Faker library that generates fake data.
4
+
5
+ == Usage
6
+ * Faker::Name.name => "Christophe Bartell"
7
+
8
+ * Faker::Internet.email => "kirsten.greenholt@corkeryfisher.info"
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,71 @@
1
+ require 'faker/version'
2
+
3
+ AUTHOR = 'Benjamin Curtis' # can also be an array of Authors
4
+ EMAIL = "benjamin.curtis@gmail.com"
5
+ DESCRIPTION = "A port of Perl's Data::Faker - Generates fake names, phone numbers, etc."
6
+ GEM_NAME = 'faker' # what ppl will type to install your gem
7
+ RUBYFORGE_PROJECT = 'faker' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+
31
+ REV = nil
32
+ # UNCOMMENT IF REQUIRED:
33
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
34
+ VERS = Faker::VERSION::STRING + (REV ? ".#{REV}" : "")
35
+ RDOC_OPTS = ['--quiet', '--title', 'faker documentation',
36
+ "--opname", "index.html",
37
+ "--line-numbers",
38
+ "--main", "README",
39
+ "--inline-source"]
40
+
41
+ class Hoe
42
+ def extra_deps
43
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
44
+ @extra_deps
45
+ end
46
+ end
47
+
48
+ # Generate all the Rake tasks
49
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
50
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
51
+ p.author = AUTHOR
52
+ p.description = DESCRIPTION
53
+ p.email = EMAIL
54
+ p.summary = DESCRIPTION
55
+ p.url = HOMEPATH
56
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
57
+ p.test_globs = ["test/**/test_*.rb"]
58
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
59
+
60
+ # == Optional
61
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
62
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
63
+
64
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
65
+
66
+ end
67
+
68
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
69
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
70
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
71
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'faker'
@@ -0,0 +1,224 @@
1
+ # TITLE:
2
+ #
3
+ # Random
4
+ #
5
+ # DESCRIPTION:
6
+ #
7
+ # Randomization methods.
8
+ #
9
+ # COPYRIGHT:
10
+ #
11
+ # Copyright (c) 2005 Ilmari Heikkinen, Christian Neukirchen, Thomas Sawyer
12
+ #
13
+ # LICENSE:
14
+ #
15
+ # Ruby License
16
+ #
17
+ # This module is free software. You may use, modify, and/or redistribute this
18
+ # software under the same terms as Ruby.
19
+ #
20
+ # This program is distributed in the hope that it will be useful, but WITHOUT
21
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22
+ # FOR A PARTICULAR PURPOSE.
23
+ #
24
+ # AUTHORS:
25
+ #
26
+ # - Ilmari Heikkinen <mailto:kig@misfiring.net>
27
+ # - Christian Neukirchen <mailto:chneukirchen@gmail.com>
28
+ # - Thomas Sawyer
29
+
30
+ # Modifications to Kernal, Hash, and String have been removed for Faker
31
+
32
+
33
+ class Array
34
+
35
+ # Return a random element of the array.
36
+ #
37
+ # [1, 2, 3, 4].at_rand #=> 2
38
+ # [1, 2, 3, 4].at_rand #=> 4
39
+ #
40
+ def at_rand
41
+ self.at( rand( size ) )
42
+ end
43
+
44
+ # Same as #at_rand, but acts in place removing a
45
+ # random element from the array.
46
+ #
47
+ # a = [1,2,3,4]
48
+ # a.at_rand! #=> 2
49
+ # a #=> [1,3,4]
50
+ #
51
+ def at_rand!
52
+ return delete_at( Kernel.rand( size ) )
53
+ end
54
+
55
+ # Similar to #at_rand, but will return an array of randomly
56
+ # picked exclusive elements if given a number.
57
+ def pick(n=nil)
58
+ if n
59
+ a = self.dup
60
+ a.pick!(n)
61
+ else
62
+ at( Kernel.rand( size ) )
63
+ end
64
+ end
65
+
66
+ # Similar to #at_rand!, but given a number will return
67
+ # an array of exclusive elements.
68
+ def pick!(n=nil)
69
+ if n
70
+ if n > self.size
71
+ r = self.dup
72
+ self.replace([])
73
+ r
74
+ else
75
+ r = []
76
+ n.times { r << delete_at( Kernel.rand( size ) ) }
77
+ r
78
+ end
79
+ else
80
+ delete_at( Kernel.rand( size ) )
81
+ end
82
+ end
83
+
84
+ # Random index.
85
+ #
86
+ def rand_index
87
+ rand( size )
88
+ end
89
+
90
+ # Returns a random subset of an Array. If a _number_
91
+ # of elements is specified then returns that number of
92
+ # elements, otherwise returns a random number of elements
93
+ # upto the size of the Array.
94
+ #
95
+ # By defualt the returned values are exclusive of
96
+ # each other, but if _exclusive_ is set to <tt>false</tt>,
97
+ # the same values can be choosen more than once.
98
+ #
99
+ # When _exclusive_ is <tt>true</tt> (the default) and the
100
+ # _number_ given is greater than the size of the array,
101
+ # then all values are returned.
102
+ #
103
+ # [1, 2, 3, 4].rand_subset(1) #=> [2]
104
+ # [1, 2, 3, 4].rand_subset(4) #=> [2, 1, 3, 4]
105
+ # [1, 2, 3, 4].rand_subset #=> [1, 3, 4]
106
+ # [1, 2, 3, 4].rand_subset #=> [2, 3]
107
+ #
108
+ def rand_subset( number=nil, exclusive=true )
109
+ number = rand( size ) unless number
110
+ number = number.to_int
111
+ #return self.dup if (number >= size and exlusive)
112
+ return sort_by{rand}.slice(0,number) if exclusive
113
+ ri =[]; number.times { |n| ri << rand( size ) }
114
+ return values_at(*ri)
115
+ end
116
+
117
+ # Randomize the order of an array.
118
+ #
119
+ # [1,2,3,4].shuffle #=> [2,4,1,3]
120
+ #
121
+ def shuffle
122
+ dup.shuffle!
123
+ #sort_by{Kernel.rand}
124
+ end
125
+
126
+ # CREDIT Niel Spring
127
+
128
+ # As with #shuffle but modifies the array in place.
129
+ # The algorithm used here is known as a Fisher-Yates shuffle.
130
+ #
131
+ # a = [1,2,3,4]
132
+ # a.shuffle!
133
+ # a #=> [2,4,1,3]
134
+ #
135
+ def shuffle!
136
+ s = size
137
+ each_index do |j|
138
+ i = Kernel.rand(s-j)
139
+ #self[j], self[j+i] = self[j+i], self[j]
140
+ tmp = self[j]
141
+ self[j] = self[j+i]
142
+ self[j+i] = tmp
143
+ end
144
+ self
145
+ end
146
+
147
+ end
148
+
149
+
150
+
151
+
152
+
153
+ # _____ _
154
+ # |_ _|__ ___| |_
155
+ # | |/ _ \/ __| __|
156
+ # | | __/\__ \ |_
157
+ # |_|\___||___/\__|
158
+ #
159
+ =begin test
160
+
161
+ require 'test/unit'
162
+
163
+ class TestArrayRandom < Test::Unit::TestCase
164
+
165
+ def test_at_rand
166
+ a = [1,2,3,4,5]
167
+ 20.times{ assert_nothing_raised{ a.at_rand } }
168
+ 20.times{ assert( a.include?( a.at_rand ) ) }
169
+ end
170
+
171
+ def test_at_rand!
172
+ a = ['a','b','c']
173
+ assert_equal( 1, a.at_rand!.length )
174
+ assert_equal( 2, a.length )
175
+ end
176
+
177
+ def test_pick
178
+ a = ['a','b','c']
179
+ assert_equal( 3, a.pick(3).length )
180
+ assert_equal( 3, a.length )
181
+ a = ['a','b','c']
182
+ assert_equal( 3, a.pick(4).length )
183
+ assert_equal( 3, a.length )
184
+ end
185
+
186
+ def test_pick!
187
+ a = ['a','b','c']
188
+ assert_equal( 3, a.pick!(3).length )
189
+ assert_equal( 0, a.length )
190
+ a = ['a','b','c']
191
+ assert_equal( 3, a.pick!(4).length )
192
+ assert_equal( 0, a.length )
193
+ end
194
+
195
+ def test_rand_index
196
+ 10.times {
197
+ i = [1,2,3].rand_index
198
+ assert( (0..2).include?(i) )
199
+ }
200
+ end
201
+
202
+ def test_rand_subset
203
+ 10.times {
204
+ a = [1,2,3,4].rand_subset
205
+ assert( a.size <= 4 )
206
+ }
207
+ end
208
+
209
+ def test_shuffle
210
+ a = [1,2,3,4,5]
211
+ b = a.shuffle
212
+ assert_equal( a, b.sort )
213
+ end
214
+
215
+ def test_shuffle!
216
+ a = [1,2,3,4,5]
217
+ b = a.dup
218
+ b.shuffle!
219
+ assert_equal( a, b.sort )
220
+ end
221
+
222
+ end
223
+
224
+ =end
@@ -0,0 +1,15 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'faker/address'
4
+ require 'faker/company'
5
+ require 'faker/internet'
6
+ require 'faker/name'
7
+ require 'faker/phone_number'
8
+
9
+ require 'facets/random'
10
+
11
+ module Faker
12
+ def self.numerify(number_string)
13
+ number_string.gsub(/#/) { rand(10).to_s }
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ module Faker
2
+ class Address
3
+ class << self
4
+ def zip_code
5
+ Faker.numerify(['#####', '#####-####'].at_rand)
6
+ end
7
+
8
+ def us_state
9
+ ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'].at_rand
10
+ end
11
+
12
+ def us_state_abbr
13
+ %w(AL AK AS AZ AR CA CO CT DE DC FM FL GA GU HI ID IL IN IA KS KY LA ME MH MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND MP OH OK OR PW PA PR RI SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP).at_rand
14
+ end
15
+
16
+ def street_suffix
17
+ %w(Alley Avenue Branch Bridge Brook Brooks Burg Burgs Bypass Camp Canyon Cape Causeway Center Centers Circle Circles Cliff Cliffs Club Common Corner Corners Course Court Courts Cove Coves Creek Crescent Crest Crossing Crossroad Curve Dale Dam Divide Drive Drive Drives Estate Estates Expressway Extension Extensions Fall Falls Ferry Field Fields Flat Flats Ford Fords Forest Forge Forges Fork Forks Fort Freeway Garden Gardens Gateway Glen Glens Green Greens Grove Groves Harbor Harbors Haven Heights Highway Hill Hills Hollow Inlet Inlet Island Island Islands Islands Isle Isle Junction Junctions Key Keys Knoll Knolls Lake Lakes Land Landing Lane Light Lights Loaf Lock Locks Locks Lodge Lodge Loop Mall Manor Manors Meadow Meadows Mews Mill Mills Mission Mission Motorway Mount Mountain Mountain Mountains Mountains Neck Orchard Oval Overpass Park Parks Parkway Parkways Pass Passage Path Pike Pine Pines Place Plain Plains Plains Plaza Plaza Point Points Port Port Ports Ports Prairie Prairie Radial Ramp Ranch Rapid Rapids Rest Ridge Ridges River Road Road Roads Roads Route Row Rue Run Shoal Shoals Shore Shores Skyway Spring Springs Springs Spur Spurs Square Square Squares Squares Station Station Stravenue Stravenue Stream Stream Street Street Streets Summit Summit Terrace Throughway Trace Track Trafficway Trail Trail Tunnel Tunnel Turnpike Turnpike Underpass Union Unions Valley Valleys Via Viaduct View Views Village Village Villages Ville Vista Vista Walk Walks Wall Way Ways Well Wells).at_rand
18
+ end
19
+
20
+ def street_name
21
+ [
22
+ Proc.new { [Name.last_name, street_suffix].join(' ') },
23
+ Proc.new { [Name.first_name, street_suffix].join(' ') }
24
+ ].at_rand.call
25
+ end
26
+
27
+ def street_address
28
+ Faker.numerify([
29
+ Proc.new { '##### %s' % street_name },
30
+ Proc.new { '##### %s' % street_name },
31
+ Proc.new { '##### %s' % street_name },
32
+ Proc.new { '##### %s' % street_name },
33
+ Proc.new { '##### %s Apt. ###' % street_name },
34
+ Proc.new { '##### %s Suite ###' % street_name }
35
+ ].at_rand.call)
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ module Faker
2
+ class Company
3
+ class << self
4
+ def name
5
+ Formats.at_rand.call
6
+ end
7
+
8
+ def suffix
9
+ %w(Inc and\ Sons LLC Group).at_rand
10
+ end
11
+ end
12
+
13
+ Formats = [
14
+ Proc.new { [ Name.last_name, suffix ].join(' ') },
15
+ Proc.new { [ Name.last_name, Name.last_name ].join('-') },
16
+ Proc.new { "%s, %s and %s" % [ Name.last_name, Name.last_name, Name.last_name ] }
17
+ ]
18
+ end
19
+ end