Create a gist now

Instantly share code, notes, and snippets.

fabrication with faker and forgery
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
Company = Struct.new( 'Company',
:name, :addr, :url, :tel, :email, :foundation )
#! /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()
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
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