Skip to content

Instantly share code, notes, and snippets.

@aitor
Created September 8, 2008 09:54
Show Gist options
  • Save aitor/9404 to your computer and use it in GitHub Desktop.
Save aitor/9404 to your computer and use it in GitHub Desktop.
Fake community
require 'faker'
require 'colored'
class Object
def tap
yield self
self
end
end
class Percentage
attr_reader :amount
def initialize(amount)
@amount = amount
end
def chance
Chance.new(self)
end
def of(number)
(number * (amount / 100.0)).to_i
end
class Chance
attr_reader :odds, :happens
alias :happens? :happens
def initialize(percent)
@odds = percent.amount
@happens = @odds > Kernel.rand(100)
end
def of(&block)
yield if happens?
end
end
end
module Kernel
def rand_within(range)
rand(range.max - range.min) + range.min
end
def maybe(percent = 50.percent, &block)
chances_of_this_happens 50.percent, &block
end
def frequently(&block)
chances_of_this_happens 80.percent, &block
end
def probably(&block)
#70.percent.chance.of &block
chances_of_this_happens 70.percent, &block
end
def rarely(&block)
chances_of_this_happens 20.percent, &block
end
def almost_never(&block)
chances_of_this_happens 5.percent, &block
end
def chances_of_this_happens(percent = 50.percent, &block)
if block_given?
percent.chance.of &block
else
percent.chance.happens?
end
end
end
class Numeric
def percent
Percentage.new(self)
end
end
class Range
def percent
Percentage.new(rand_within(self))
end
end
def log(text, color=:white)
puts text.send(color)
end
def the_community
User.count
end
def create_user
User.new.tap do |u|
u.login = Faker::Internet.user_name
u.email = Faker::Internet.free_email
u.password = u.password_confirmation = "perron"
u.register!
u.activate!
log "User ##{u.id} generated. Welcome '#{u.login}'"
end
end
def update_her_profile(user)
user.profile.first_name = Faker::Name.first_name
user.profile.last_name = Faker::Name.last_name
user.profile.website = user.profile.blog = "www." + Faker::Internet.domain_name
with_random_avatar do |avatar|
user.profile.icon = avatar
user.profile.save!
end
end
def with_random_avatar(&block)
begin
uri = URI.parse(Faker::Avatar.avatar)
tmp_file = uri.path[/.*\/(.*)\z/,1]
open(tmp_file, "wb") { |file|
file.write(Net::HTTP.get_response(uri).read_body)
}
yield File.new(tmp_file)
File.delete(tmp_file)
rescue
log "Unable to get avatar", :red
end
end
def create_a_group(user)
Group.new.tap do |g|
g.name = Faker::Company.catch_phrase
g.description = Faker::Lorem.paragraphs(rand_within(1..4)).join
g.author = user
g.private = maybe
g.moderated = maybe
g.tag_list.add Faker::Lorem.words(rand_within(0..6))
g.save!
with_random_avatar do |avatar|
g.image = avatar
g.save!
end
probably{
g.activate!
}
log "Group ##{g.id} generated [#{g.state.upcase}]. Topic: '#{g.name}'", :yellow
end
end
def random_user
User.find :first, :offset => rand_within(0..User.count - 1)
end
def this_group_would_be_joined_by(group, number)
number.times do
u = random_user
group.join(u, almost_never)
group.activate_membership(u) if maybe
end
log " └ #{number} users joined it!", :yellow
end
def will_have_a_number_of_friends(user, &block)
friends = yield
if friends
friends.times do
probably {
user.profile.add_follower(random_user.profile)
}
rarely {
user.profile.add_friend(random_user.profile)
}
end
log " └ #{user.login} has #{friends} friends."
end
end
100.times do
a_user = create_user
frequently do
update_her_profile(a_user)
end
will_have_a_number_of_friends(a_user) do
probably { rand_within(10..50) } || rarely { 20.percent.of(the_community) }
end
rarely do
a_group = create_a_group(a_user)
a_range_of_users = nil
frequently do
a_range_of_users = (1..5).percent.of the_community
end
almost_never do
a_range_of_users = (20..30).percent.of the_community
end
this_group_would_be_joined_by(a_group, a_range_of_users) if a_range_of_users
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment