Skip to content

Commit

Permalink
Refactoring and writing specs.
Browse files Browse the repository at this point in the history
Removed ruby facets because of potentially conflicting licenses and 'YAGNI'.
Changing to RSpec for great profit.  Improving random range extension.
  • Loading branch information
Nathan Sutton committed Jun 1, 2008
1 parent 0c5211e commit d68c02a
Show file tree
Hide file tree
Showing 30 changed files with 211 additions and 253 deletions.
11 changes: 11 additions & 0 deletions Rakefile
@@ -0,0 +1,11 @@
require 'rake'
require 'spec/rake/spectask'

desc 'Default: run specs.'
task :default => :spec

desc 'Run the specs'
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
t.spec_files = FileList['spec/**/*_spec.rb']
end
Empty file added install.rb
Empty file.
5 changes: 5 additions & 0 deletions lib/extensions/array.rb
@@ -0,0 +1,5 @@
class Array
def random
self[Kernel.rand(size)]
end
end
147 changes: 0 additions & 147 deletions lib/extensions/random.rb

This file was deleted.

8 changes: 6 additions & 2 deletions lib/extensions/range.rb
@@ -1,5 +1,9 @@
class Range
def at_rand
Kernel.rand(self.last - self.first + 1) + self.first
def random
return nil unless self.max
Integer(min) && Integer(max)
Kernel.rand(self.max - self.min + 1) + self.min
rescue ArgumentError
self.to_a.random
end
end
4 changes: 2 additions & 2 deletions lib/extensions/string.rb
@@ -1,5 +1,5 @@
class String
def numerify
self.gsub(/#/) { Kernel.rand(10).to_s }
def to_numbers(replace='#')
self.gsub(/#{replace}/){ Kernel.rand(10) }
end
end
18 changes: 9 additions & 9 deletions lib/forgeries/address_forgery.rb
Expand Up @@ -3,42 +3,42 @@ class AddressForgery < Forgery
formats :zip, :phone, :street_number

def self.street_name
STREETS.at_rand
STREETS.random
end

def self.street_number
STREET_NUMBER_FORMATS.at_rand.numerify
STREET_NUMBER_FORMATS.random.to_numbers
end

def self.street_suffix
STREET_SUFFIXES.at_rand
STREET_SUFFIXES.random
end

def self.street_address
"#{street_number} #{street_name} #{street_suffix}"
end

def self.city
CITIES.at_rand
CITIES.random
end

def self.state
STATES.at_rand
STATES.random
end

def self.state_abbrev
STATE_ABBREVS.at_rand
STATE_ABBREVS.random
end

def self.zip
ZIP_FORMATS.at_rand.numerify
ZIP_FORMATS.random.to_numbers
end

def self.phone
PHONE_FORMATS.at_rand.numerify
PHONE_FORMATS.random.to_numbers
end

def self.country
COUNTRIES.at_rand
COUNTRIES.random
end
end
12 changes: 6 additions & 6 deletions lib/forgeries/basic_forgery.rb
Expand Up @@ -26,7 +26,7 @@ def self.salt
end

def self.boolean
[true, false].at_rand
[true, false].random
end

def self.random_text(options={})
Expand All @@ -43,7 +43,7 @@ def self.random_text(options={})
allowed_characters += NUMERIC if options[:allow_numeric]
allowed_characters += SPECIAL_CHARACTERS if options[:allow_special]

length = (options[:at_least]..options[:at_most]).at_rand
length = (options[:at_least]..options[:at_most]).random

allowed_characters.rand_subset(length, false).join
end
Expand All @@ -52,20 +52,20 @@ def self.number(options={})
options = {:at_least => 1,
:at_most => 10}.merge(options)

(options[:at_least]..options[:at_most]).at_rand
(options[:at_least]..options[:at_most]).random
end

def self.color
COLORS.at_rand
COLORS.random
end

def self.hex_color
hex_value = ""
6.times { hex_value << HEX_DIGITS.at_rand }
6.times { hex_value << HEX_DIGITS.random }
"##{hex_value}"
end

def self.short_hex_color
hex_color[0,4]
end
end
end
6 changes: 3 additions & 3 deletions lib/forgeries/internet_forgery.rb
Expand Up @@ -2,15 +2,15 @@ class InternetForgery < Forgery
dictionaries :male_first_names, :female_first_names, :last_names, :top_level_domains, :company_names

def self.user_name
(MALE_FIRST_NAMES.at_rand + LAST_NAMES.at_rand).downcase
(MALE_FIRST_NAMES.random + LAST_NAMES.random).downcase
end

def self.top_level_domain
TOP_LEVEL_DOMAINS.at_rand
TOP_LEVEL_DOMAINS.random
end

def self.domain_name
COMPANY_NAMES.at_rand.downcase + '.' + self.top_level_domain
COMPANY_NAMES.random.downcase + '.' + self.top_level_domain
end

def self.email_address
Expand Down
2 changes: 1 addition & 1 deletion lib/forgeries/lorem_ipsum_forgery.rb
Expand Up @@ -95,7 +95,7 @@ def self.range_from_quantity(quantity, options={})
return quantity if quantity.class == Range

if options[:random]
start = (0..options[:random_limit]).at_rand
start = (0..options[:random_limit]).random
start..(start+quantity-1)
else
0..(quantity-1)
Expand Down
2 changes: 1 addition & 1 deletion lib/forgeries/monetary_forgery.rb
Expand Up @@ -7,7 +7,7 @@ def self.money(options={})
options = {:max => 10,
:min => 0}.merge(options)

value = ((options[:min] * 100)..(options[:max] * 100)).at_rand
value = ((options[:min] * 100)..(options[:max] * 100)).random
"%1.2f" % (value.to_f / 100)
end
end
8 changes: 4 additions & 4 deletions lib/forgeries/name_forgery.rb
Expand Up @@ -2,18 +2,18 @@ class NameForgery < Forgery
dictionaries :last_names, :male_first_names, :female_first_names

def self.last_name
LAST_NAMES.at_rand
LAST_NAMES.random
end

def self.first_name
[MALE_FIRST_NAMES, FEMALE_FIRST_NAMES].at_rand.at_rand
[MALE_FIRST_NAMES, FEMALE_FIRST_NAMES].random.random
end

def self.male_first_name
MALE_FIRST_NAMES.at_rand
MALE_FIRST_NAMES.random
end

def self.female_first_name
FEMALE_FIRST_NAMES.at_rand
FEMALE_FIRST_NAMES.random
end
end
4 changes: 2 additions & 2 deletions lib/forgeries/personal_forgery.rb
@@ -1,13 +1,13 @@
class PersonalForgery < Forgery
def self.gender
%w{Male Female}.at_rand
%w{Male Female}.random
end

def self.abbreviated_gender
gender[0,1]
end

def self.shirt_size
%w{XS S M L XL 2XL 3XL}.at_rand
%w{XS S M L XL 2XL 3XL}.random
end
end
2 changes: 1 addition & 1 deletion lib/formats/phone
@@ -1 +1 @@
1-(###)###-####
#-(###)###-####
15 changes: 15 additions & 0 deletions spec/extensions/array_spec.rb
@@ -0,0 +1,15 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Array do
before do
@array = [0,1,2,3,4,5,6,7,8,9]
end

it "should get a random item out of the array" do
10.times { @array.should include(@array.random) }
end

it "should return nil if the array is empty" do
[].random.should be_nil
end
end

0 comments on commit d68c02a

Please sign in to comment.