Created
December 12, 2012 22:28
-
-
Save partkyle/4272244 to your computer and use it in GitHub Desktop.
custom_fields.rb
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
module CustomFieldsGenerator | |
# generate a number of custom fields based on the `@@field_types` | |
# this is to simulate a list upload | |
def self.generate(count) | |
pre_hash = @@field_enumerator.take(count) | |
Hash[pre_hash] | |
end | |
@@field_types = { | |
# basic custom fields | |
:name_prefix => lambda { Faker::Name.prefix }, | |
:first_name => lambda { Faker::Name.first_name}, | |
:last_name => lambda { Faker::Name.last_name }, | |
:suffix => lambda { Faker::Name.suffix }, | |
# home fields | |
:home_address_line_1 => lambda { Faker::Address.street_address }, | |
:home_address_line_2 => lambda { Faker::Address.secondary_address }, | |
:home_state => lambda { Faker::Address.state }, | |
:home_city => lambda { Faker::Address.city }, | |
:home_zip => lambda { Faker::Address.zip }, | |
:home_phone => lambda { Faker::PhoneNumber.phone_number }, | |
# work fields | |
:company => lambda { Faker::Company.name }, | |
:title => lambda { Faker::Name.title }, | |
:work_address_line_1 => lambda { Faker::Address.street_address }, | |
:work_address_line_2 => lambda { Faker::Address.secondary_address }, | |
:work_state => lambda { Faker::Address.state }, | |
:work_zip => lambda { Faker::Address.zip }, | |
:work_phone => lambda { Faker::PhoneNumber.phone_number }, | |
# client fields | |
:user_name => lambda { Faker::Internet.user_name }, | |
:domain => lambda { Faker::Internet.domain_name } | |
} | |
# this will loop through the default custom fields infinitely, | |
# it works best with the `take` function on enumerable | |
@@field_enumerator = Enumerator.new do |yielder| | |
counter = 0 | |
loop do | |
@@field_types.each do |field, exec| | |
out_field = counter == 0 ? field : "#{field}_#{counter.to_s}" | |
yielder << [out_field, exec.call] | |
end | |
counter += 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment