Created
January 12, 2014 21:09
-
-
Save zuchmanski/8390651 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'digest/sha1' | |
require 'faker' | |
class Generator | |
class << self | |
attr_accessor :attributes | |
def number(n) | |
@number = n | |
end | |
def function_name(name) | |
@function_name = name | |
end | |
def attribute(name, type, &block) | |
self.attributes ||= {} | |
self.attributes[name] = { type: type, block: block } | |
end | |
def generate | |
out = [] | |
@number.times do | |
a = {} | |
@attributes.each do |name, values| | |
a[name] = escape(values[:block].call, values[:type]) | |
end | |
out << a | |
end | |
format(out) | |
end | |
def escape(value, type) | |
if value.nil? | |
return 'null' | |
else | |
case type | |
when :string | |
%{'#{value.gsub("'", "")}'} | |
when :integer | |
value.to_s | |
end | |
end | |
end | |
def format(array) | |
array.map do |attributes| | |
"select #{@function_name}(#{attributes.values.join(', ')});" | |
end.join("\n") | |
end | |
end | |
end | |
# companies | |
class Company < Generator | |
function_name :create_company | |
number 50 | |
attribute :name, :string do | |
Faker::Company.name | |
end | |
attribute :email, :string do | |
Faker::Internet.email | |
end | |
attribute :phone, :string do | |
Faker::PhoneNumber.cell_phone[0..14] | |
end | |
attribute :vat_id, :string do | |
Faker::Number.number(16) | |
end | |
end | |
class User < Generator | |
function_name :create_user | |
number 2000 | |
attribute :company_id, :integer do | |
rand(1..50) if rand > 0.75 | |
end | |
attribute :name, :string do | |
Faker::Name.name | |
end | |
attribute :email, :string do | |
Faker::Internet.email | |
end | |
attribute :sex, :string do | |
if rand > 0.5 then 'm' else 'f' end | |
end | |
attribute :phone, :string do | |
Faker::PhoneNumber.cell_phone[0..14] | |
end | |
attribute :student_id, :string do | |
Faker::Number.number(12) if rand > 0.9 | |
end | |
attribute :password, :string do | |
Digest::SHA1.hexdigest(Faker::Internet.password) | |
end | |
attribute :password_salt, :string do | |
Digest::SHA1.hexdigest(Faker::Internet.password) | |
end | |
end | |
puts Company.generate | |
puts User.generate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment