Class: RandomPerson::Generator
- Inherits:
-
Object
- Object
- RandomPerson::Generator
- Defined in:
- lib/randomperson/generator.rb
Instance Attribute Summary collapse
-
#generators ⇒ Object
readonly
Returns the value of attribute generators.
Class Method Summary collapse
- .pick_age(lower = 0, upper = 100) ⇒ Object
- .pick_dob(y = 16) ⇒ Object
-
.pick_gender(ratio = [1,1]) ⇒ String
Takes a ratio and uses this to randomly select either a male or a female.
-
.ratiod(ratio = [1,1]) ⇒ Object
create a ratio that is made up of ranges to help hit with multiple valued ratios.
Instance Method Summary collapse
-
#initialize ⇒ Generator
constructor
Sets up a list of generators.
-
#make_generator(choice) ⇒ Object
Build a choice generator.
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Generator
Sets up a list of generators
13 14 15 |
# File 'lib/randomperson/generator.rb', line 13 def initialize @generators = [ ] end |
Instance Attribute Details
#generators ⇒ Object (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.
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
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 |
#reset ⇒ Object
102 103 104 |
# File 'lib/randomperson/generator.rb', line 102 def reset @generators = [ ] end |