Created
December 20, 2010 13:25
-
-
Save wtnabe/748375 to your computer and use it in GitHub Desktop.
fabrication with faker and forgery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Company | |
def initialize | |
@name = nil | |
@addr = nil | |
@url = nil | |
@tel = nil | |
@email = nil | |
@foundation = nil | |
end | |
attr_accessor :name, :addr, :url, :tel, :email, :foundation | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Company = Struct.new( 'Company', | |
:name, :addr, :url, :tel, :email, :foundation ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'pp' | |
require 'rubygems' | |
require 'fabrication' | |
require 'optparse' | |
@class_generator_method = 'poc' | |
@fabricator_with = 'faker' | |
@attributes_for = false | |
def main | |
args.parse( ARGV ) | |
generate_class | |
generate_fabricator | |
pp (1..10).map { | |
if ( @attributes_for ) | |
Fabricate.attributes_for( :company ) | |
else | |
Fabricate( :company ) | |
end | |
} | |
end | |
def generate_class | |
require File.dirname( __FILE__ ) + "/class_by_#{@class_generator_method}" | |
end | |
def generate_fabricator | |
require File.dirname( __FILE__ ) + "/fabricator_with_#{@fabricator_with}" | |
end | |
def class_generator_methods | |
Dir.glob( 'class_by_*.rb' ).map { |e| e.sub( /class_by_(.+)\.rb/, '\1' ) } | |
end | |
def fabricator_generator_methods | |
Dir.glob( 'fab_with_*.rb' ).map { |e| e.sub( /fab_with_(.+)\.rb/, '\1' ) } | |
end | |
def args | |
OptionParser.new { |opt| | |
opt.on( '-g', '--generator-method <poc|struct>' ) { |val| | |
if ( class_generator_methods.include?( val ) ) | |
@class_generator_method = val | |
else | |
raise ArgumentError | |
end | |
} | |
opt.on( '-a', '--attributes' ) { | |
@attributes_for = true | |
} | |
opt.on( '-f', '--fabricator-with <faker|forgery>' ) { |val| | |
@fabricator_with = val | |
} | |
} | |
end | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faker' | |
Fabricator( :company ) do | |
name { Faker::Company.name } | |
addr { [Faker::Address.street_address, Faker::Address.city, | |
Faker::Address.us_state].join( ', ' ) } | |
url { ['http://', Faker::Internet.domain_name].join } | |
tel { Faker::PhoneNumber.phone_number } | |
email { Faker::Internet.email } | |
foundation { rand(100) + 1900 } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'forgery' | |
Fabricator( :company ) do | |
name { Forgery::Name.company_name } | |
addr { [Forgery(:address).street_address, Forgery(:address).city, | |
Forgery(:address).state].join( ', ' ) } | |
url { ['http://', Forgery(:internet).domain_name].join } | |
tel { Forgery(:address).phone } | |
email { Forgery(:internet).email_address } | |
foundation { Forgery(:date).year( :future => true, | |
:min_delta => -110, | |
:max_delta => -10 ) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment