Class: RandomPerson::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/randomperson/generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Sets up a list of generators

Examples:

g = RandomPerson::Generator.new


13
14
15
# File 'lib/randomperson/generator.rb', line 13

def initialize
  @generators = [ ] 
end

Instance Attribute Details

#generatorsObject (readonly)

Returns the value of attribute generators.



8
9
10
# File 'lib/randomperson/generator.rb', line 8

def generators
  @generators
end

Class Method Details

.pick_age(lower = 0, upper = 100) ⇒ Object



84
85
86
# File 'lib/randomperson/generator.rb', line 84

def pick_age( lower=0, upper=100 )
  age =  rand(upper - lower).to_i + lower 
end

.pick_dob(y = 16) ⇒ Object



88
89
90
91
92
93
# File 'lib/randomperson/generator.rb', line 88

def pick_dob( y=16 )
  year  = Time.now.year - y
  month = rand(12) + 1;
  day   = rand( Date.days_in_month( year, month ) ) + 1 
  Time.local( year, month, day )
end

.pick_gender(ratio = [1,1]) ⇒ String

Takes a ratio and uses this to randomly select either a male or a female. To get only males or only females, pass in a 0 for either gender to make sure of getting the other gender for definite.

Examples:

To only get males:

pick_gender( [1,0] )

To only get females:

pick_gender( [0,1] )

To be likely to get 2 females to every male:

pick_gender( [1,2] )

Parameters:

  • ratio (optional, Array<Integer,Integer>) (defaults to: [1,1])

    The ratio of males to females expressed in an array, e.g. [1,1] is (a chance of) one male for every female, [1,2] is one male to two females. The default is [1,1].

Returns:

  • (String)

    Returns a ‘f’ for female or an ‘m’ for male.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/randomperson/generator.rb', line 71

def pick_gender( ratio=[1,1] ) #male first
  if ratio.length == 2
    return 'f' if ratio.first == 0
    return 'm' if ratio.last == 0 
  end
  rat = ratiod( ratio )
  r = rand(rat.last.last)
  case r
    when rat[0] then 'm'
    when rat[1] then 'f'
  end
end

.ratiod(ratio = [1,1]) ⇒ Object

create a ratio that is made up of ranges to help hit with multiple valued ratios



53
54
55
56
57
58
59
60
# File 'lib/randomperson/generator.rb', line 53

def ratiod( ratio=[1,1] )
  sum = ratio.reduce(:+) #sum
  mult = 100.divmod( sum ).first #get a bigger spread of numbers
  ratio.map! { |n| n * mult }
  new_ratio = ratio.inject([0..0]) {|acc,n| acc + [acc.last.last ... (n + acc.last.last)] }
  new_ratio.shift #get rid of 0..0
  return new_ratio
end

Instance Method Details

#make_generator(choice) ⇒ Object

Build a choice generator

Examples:

g.make_generator choice

Parameters:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/randomperson/generator.rb', line 22

def make_generator( choice )
f = -> {
    g = Generator.pick_gender( choice.gender_ratio )
    age = Generator.pick_age( choice.age_lower, choice.age_upper)
    
    person = Person.new(
      :gender =>  g, 
      :age => age,
      :dob => Generator.pick_dob( age )
    )
    
    if person.gender == 'm'   
      person.first = choice.malefirst.execute( person ) unless choice.malefirst.nil?
    else
      person.first = choice.femalefirst.execute( person ) unless choice.femalefirst.nil?
    end
      
    person.last = choice.last.execute( person ) unless choice.last.nil?#lastname, 
    person.prefix = choice.prefix.execute( person ) unless choice.prefix.nil? #title 
    person.suffix = choice.suffix.execute( person ) unless choice.suffix.nil?#suffix
    
    person
  }
  
  @generators << f
end

#resetObject



102
103
104
# File 'lib/randomperson/generator.rb', line 102

def reset
  @generators = [ ] 
end