Class: RandomPerson::Choice
- Inherits:
-
Object
- Object
- RandomPerson::Choice
- Defined in:
- lib/randomperson/choice.rb
Constant Summary collapse
- @@available_classes =
Set.new
Instance Attribute Summary collapse
-
#age_lower ⇒ Object
,:age_ratio.
-
#age_upper ⇒ Object
,:age_ratio.
-
#femalefirst ⇒ Object
,:age_ratio.
-
#gender_ratio ⇒ Object
,:age_ratio.
-
#last ⇒ Object
,:age_ratio.
-
#malefirst ⇒ Object
,:age_ratio.
-
#prefix ⇒ Object
,:age_ratio.
-
#suffix ⇒ Object
,:age_ratio.
Instance Method Summary collapse
- #addklass(fn, ps) ⇒ Object
- #available_classes ⇒ Object
-
#initialize(h = {}) ⇒ Choice
constructor
Initialize the class with the parameters for the population you want.
- #load_names(patterns = ['*.rb']) ⇒ Object
-
#method_missing(name, *args) ⇒ Object
tribe, gender, position.
-
#reset_all ⇒ Object
set all parameters and nameclasses for this instance to nil.
-
#reset_names ⇒ Object
set all the nameclasses for this instance to nil.
-
#reset_parameters ⇒ Object
set the age and gender parameters for this instance to nil.
Constructor Details
#initialize(h = {}) ⇒ Choice
Initialize the class with the parameters for the population you want.
18 19 20 21 22 23 24 25 |
# File 'lib/randomperson/choice.rb', line 18 def initialize( h={} ) @gender_ratio = h[:gender_ratio] || [1,1] #default @age_lower = h[:age_lower] || 0 @age_upper = h[:age_upper] || 100 load_names end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
tribe, gender, position
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/randomperson/choice.rb', line 38 def method_missing( name, *args ) return super( name, *args ) unless name.to_s =~ /^add/ words = name.to_s.split( "_" ) words.shift #get rid of the "add" #get the negations prepended with "not", then lop it off" nots = words.select{ |w| w =~ /^not/ }.map {|s| s[3..-1] } #get just the positives words.reject! {|w| w =~ /^not/ } #TODO: check the beginning of each word has an uc letter #get a set of nots n = nots.map{|word| @@available_classes.classify_true(word)}.fold(:&) #get a set of wanteds cs = words.map{|word| @@available_classes.classify_true(word)}.fold(:&) cs = cs - n unless n.nil? #remove nots from wanteds cs.each do |c| require c fn = File.basename( c, File.extname( c ) ) #remove the extension fn = 'RandomPerson::Names::' + fn addklass( fn, %w{ Male First } ) addklass( fn, %w{ Female First } ) addklass( fn, %w{ Last } ) addklass( fn, %w{ Prefix } ) addklass( fn, %w{ Suffix } ) end end |
Instance Attribute Details
#age_lower ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def age_lower @age_lower end |
#age_upper ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def age_upper @age_upper end |
#femalefirst ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def femalefirst @femalefirst end |
#gender_ratio ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def gender_ratio @gender_ratio end |
#last ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def last @last end |
#malefirst ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def malefirst @malefirst end |
#prefix ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def prefix @prefix end |
#suffix ⇒ Object
,:age_ratio
6 7 8 |
# File 'lib/randomperson/choice.rb', line 6 def suffix @suffix end |
Instance Method Details
#addklass(fn, ps) ⇒ Object
70 71 72 73 74 |
# File 'lib/randomperson/choice.rb', line 70 def addklass( fn, ps ) if ps.map { |p| fn =~ /#{p}/ }.all? instance_variable_set( "@#{ps.join.downcase}", qualified_const_get(fn).new ) end end |
#available_classes ⇒ Object
8 9 10 |
# File 'lib/randomperson/choice.rb', line 8 def available_classes @@available_classes end |
#load_names(patterns = ['*.rb']) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/randomperson/choice.rb', line 27 def load_names( patterns=['*.rb'] ) lib_dir = File.dirname(__FILE__) patterns.each do |pat| full_pattern = File.join( lib_dir, 'Names', pat ) Dir.glob( full_pattern ).each do |file| @@available_classes << file end end end |
#reset_all ⇒ Object
set all parameters and nameclasses for this instance to nil
86 87 88 89 |
# File 'lib/randomperson/choice.rb', line 86 def reset_all reset_names reset_parameters end |
#reset_names ⇒ Object
set all the nameclasses for this instance to nil
77 78 79 80 81 82 83 |
# File 'lib/randomperson/choice.rb', line 77 def reset_names @suffix = nil @prefix = nil @last = nil @malefirst = nil @femalefirst = nil end |
#reset_parameters ⇒ Object
set the age and gender parameters for this instance to nil
92 93 94 95 96 |
# File 'lib/randomperson/choice.rb', line 92 def reset_parameters @age_upper = nil @age_lower = nil @gender_ratio = nil end |