Permalink
Browse files

Dropped facets because of conflict with ActiveSupport. Also tweaked o…

…utput of Faker::Internet#user_name.

git-svn-id: svn://rubyforge.org/var/svn/faker/trunk@14 d8fb7423-2fd9-4a2a-9c92-119baff0aaab
  • Loading branch information...
1 parent 2e5315c commit 4d310807dc19d4f9418c264fb4f9484faf6145da bcurtis committed Dec 5, 2007
View
@@ -5,7 +5,7 @@ README.txt
Rakefile
config/hoe.rb
config/requirements.rb
-lib/facets/random.rb
+lib/extensions/array.rb
lib/faker.rb
lib/faker/address.rb
lib/faker/company.rb
View
@@ -0,0 +1,9 @@
+class Array
+ def rand
+ self[Kernel.rand(length)]
+ end
+
+ def shuffle
+ self.sort_by{Kernel.rand}
+ end
+end
View
@@ -1,224 +0,0 @@
-# TITLE:
-#
-# Random
-#
-# DESCRIPTION:
-#
-# Randomization methods.
-#
-# COPYRIGHT:
-#
-# Copyright (c) 2005 Ilmari Heikkinen, Christian Neukirchen, Thomas Sawyer
-#
-# LICENSE:
-#
-# Ruby License
-#
-# This module is free software. You may use, modify, and/or redistribute this
-# software under the same terms as Ruby.
-#
-# This program is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-# AUTHORS:
-#
-# - Ilmari Heikkinen <mailto:kig@misfiring.net>
-# - Christian Neukirchen <mailto:chneukirchen@gmail.com>
-# - Thomas Sawyer
-
-# Modifications to Kernal, Hash, and String have been removed for Faker
-
-
-class Array
-
- # Return a random element of the array.
- #
- # [1, 2, 3, 4].at_rand #=> 2
- # [1, 2, 3, 4].at_rand #=> 4
- #
- def at_rand
- self.at( rand( size ) )
- end
-
- # Same as #at_rand, but acts in place removing a
- # random element from the array.
- #
- # a = [1,2,3,4]
- # a.at_rand! #=> 2
- # a #=> [1,3,4]
- #
- def at_rand!
- return delete_at( Kernel.rand( size ) )
- end
-
- # Similar to #at_rand, but will return an array of randomly
- # picked exclusive elements if given a number.
- def pick(n=nil)
- if n
- a = self.dup
- a.pick!(n)
- else
- at( Kernel.rand( size ) )
- end
- end
-
- # Similar to #at_rand!, but given a number will return
- # an array of exclusive elements.
- def pick!(n=nil)
- if n
- if n > self.size
- r = self.dup
- self.replace([])
- r
- else
- r = []
- n.times { r << delete_at( Kernel.rand( size ) ) }
- r
- end
- else
- delete_at( Kernel.rand( size ) )
- end
- end
-
- # Random index.
- #
- def rand_index
- rand( size )
- end
-
- # Returns a random subset of an Array. If a _number_
- # of elements is specified then returns that number of
- # elements, otherwise returns a random number of elements
- # upto the size of the Array.
- #
- # By defualt the returned values are exclusive of
- # each other, but if _exclusive_ is set to <tt>false</tt>,
- # the same values can be choosen more than once.
- #
- # When _exclusive_ is <tt>true</tt> (the default) and the
- # _number_ given is greater than the size of the array,
- # then all values are returned.
- #
- # [1, 2, 3, 4].rand_subset(1) #=> [2]
- # [1, 2, 3, 4].rand_subset(4) #=> [2, 1, 3, 4]
- # [1, 2, 3, 4].rand_subset #=> [1, 3, 4]
- # [1, 2, 3, 4].rand_subset #=> [2, 3]
- #
- def rand_subset( number=nil, exclusive=true )
- number = rand( size ) unless number
- number = number.to_int
- #return self.dup if (number >= size and exlusive)
- return sort_by{rand}.slice(0,number) if exclusive
- ri =[]; number.times { |n| ri << rand( size ) }
- return values_at(*ri)
- end
-
- # Randomize the order of an array.
- #
- # [1,2,3,4].shuffle #=> [2,4,1,3]
- #
- def shuffle
- dup.shuffle!
- #sort_by{Kernel.rand}
- end
-
- # CREDIT Niel Spring
-
- # As with #shuffle but modifies the array in place.
- # The algorithm used here is known as a Fisher-Yates shuffle.
- #
- # a = [1,2,3,4]
- # a.shuffle!
- # a #=> [2,4,1,3]
- #
- def shuffle!
- s = size
- each_index do |j|
- i = Kernel.rand(s-j)
- #self[j], self[j+i] = self[j+i], self[j]
- tmp = self[j]
- self[j] = self[j+i]
- self[j+i] = tmp
- end
- self
- end
-
-end
-
-
-
-
-
-# _____ _
-# |_ _|__ ___| |_
-# | |/ _ \/ __| __|
-# | | __/\__ \ |_
-# |_|\___||___/\__|
-#
-=begin test
-
- require 'test/unit'
-
- class TestArrayRandom < Test::Unit::TestCase
-
- def test_at_rand
- a = [1,2,3,4,5]
- 20.times{ assert_nothing_raised{ a.at_rand } }
- 20.times{ assert( a.include?( a.at_rand ) ) }
- end
-
- def test_at_rand!
- a = ['a','b','c']
- assert_equal( 1, a.at_rand!.length )
- assert_equal( 2, a.length )
- end
-
- def test_pick
- a = ['a','b','c']
- assert_equal( 3, a.pick(3).length )
- assert_equal( 3, a.length )
- a = ['a','b','c']
- assert_equal( 3, a.pick(4).length )
- assert_equal( 3, a.length )
- end
-
- def test_pick!
- a = ['a','b','c']
- assert_equal( 3, a.pick!(3).length )
- assert_equal( 0, a.length )
- a = ['a','b','c']
- assert_equal( 3, a.pick!(4).length )
- assert_equal( 0, a.length )
- end
-
- def test_rand_index
- 10.times {
- i = [1,2,3].rand_index
- assert( (0..2).include?(i) )
- }
- end
-
- def test_rand_subset
- 10.times {
- a = [1,2,3,4].rand_subset
- assert( a.size <= 4 )
- }
- end
-
- def test_shuffle
- a = [1,2,3,4,5]
- b = a.shuffle
- assert_equal( a, b.sort )
- end
-
- def test_shuffle!
- a = [1,2,3,4,5]
- b = a.dup
- b.shuffle!
- assert_equal( a, b.sort )
- end
-
- end
-
-=end
View
@@ -5,8 +5,9 @@
require 'faker/internet'
require 'faker/name'
require 'faker/phone_number'
+require 'faker/version'
-require 'facets/random'
+require 'extensions/array'
module Faker
def self.numerify(number_string)
View
@@ -2,26 +2,26 @@ module Faker
class Address
class << self
def zip_code
- Faker.numerify(['#####', '#####-####'].at_rand)
+ Faker.numerify(['#####', '#####-####'].rand)
end
def us_state
- ['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
+ ['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'].rand
end
def us_state_abbr
- %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
+ %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).rand
end
def street_suffix
- %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
+ %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).rand
end
def street_name
[
Proc.new { [Name.last_name, street_suffix].join(' ') },
Proc.new { [Name.first_name, street_suffix].join(' ') }
- ].at_rand.call
+ ].rand.call
end
def street_address
@@ -32,7 +32,7 @@ def street_address
Proc.new { '##### %s' % street_name },
Proc.new { '##### %s Apt. ###' % street_name },
Proc.new { '##### %s Suite ###' % street_name }
- ].at_rand.call)
+ ].rand.call)
end
end
Oops, something went wrong.

0 comments on commit 4d31080

Please sign in to comment.