Class: RandomPerson::Demographic
- Inherits:
-
Object
- Object
- RandomPerson::Demographic
- Includes:
- Loader
- Defined in:
- lib/randomperson/demographic.rb
Overview
Handles the demographics
Instance Attribute Summary collapse
-
#age_lower ⇒ Object
,:age_ratio.
-
#age_upper ⇒ Object
,:age_ratio.
-
#femalefirst ⇒ Object
(also: #female_first)
,:age_ratio.
-
#gender_ratio ⇒ Object
,:age_ratio.
-
#last ⇒ Object
(also: #lastname)
,:age_ratio.
-
#malefirst ⇒ Object
(also: #male_first)
,:age_ratio.
-
#name ⇒ Object
the name of this demographic.
-
#prefix ⇒ Object
,:age_ratio.
-
#suffix ⇒ Object
,:age_ratio.
Class Method Summary collapse
-
.available_name_files ⇒ Set
The available name files!.
-
.classify_true(pattern) ⇒ Set
The set for which the pattern matches.
-
.load ⇒ Object
puts the demo files into the class instance var.
Instance Method Summary collapse
-
#get_nots(words) ⇒ Array<String>
Get the negations prepended with “not”, then lop it off“.
- #get_words(name) ⇒ Array<String>
-
#get_yesses(words) ⇒ Array<String>
Get the positives.
-
#initialize(name = nil, opts = {}) ⇒ Demographic
constructor
Initialize the class with the parameters for the population you want.
-
#loaded_classes ⇒ Object
A hash of all the loaded classes.
-
#method_missing(name, *args) ⇒ Object
tribe, gender, position.
-
#require_and_add(yesses) ⇒ Object
private
Requires the name files and adds them to the loaded file hash.
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
private
Be nice.
Methods included from Loader
Constructor Details
#initialize(name = nil, opts = {}) ⇒ Demographic
Initialize the class with the parameters for the population you want.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/randomperson/demographic.rb', line 51 def initialize( name=nil, opts={} ) if name.kind_of? Hash opts = name name = nil end @name = name @gender_ratio = opts[:gender_ratio] || [1,1] #default @age_lower = opts[:age_lower] || 0 @age_upper = opts[:age_upper] || 115 @loaded_classes ||= {} self.class.load end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
tribe, gender, position
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/randomperson/demographic.rb', line 114 def method_missing( name, *args ) return super( name, *args ) unless name.to_s =~ /^add/ words = get_words( name ) nots = get_nots( words ).map{|word| self.class.classify_true word }.inject(:&) yesses = get_yesses( words ).map{|word| self.class.classify_true word }.inject(:&) require_and_add yesses self # just because end |
Instance Attribute Details
#age_lower ⇒ Object
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def age_lower @age_lower end |
#age_upper ⇒ Object
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def age_upper @age_upper end |
#femalefirst ⇒ Object Also known as: female_first
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def femalefirst @femalefirst end |
#gender_ratio ⇒ Object
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def gender_ratio @gender_ratio end |
#last ⇒ Object Also known as: lastname
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def last @last end |
#malefirst ⇒ Object Also known as: male_first
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def malefirst @malefirst end |
#name ⇒ Object
the name of this demographic
12 13 14 |
# File 'lib/randomperson/demographic.rb', line 12 def name @name end |
#prefix ⇒ Object
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def prefix @prefix end |
#suffix ⇒ Object
,:age_ratio
21 22 23 |
# File 'lib/randomperson/demographic.rb', line 21 def suffix @suffix end |
Class Method Details
.available_name_files ⇒ Set
The available name files!
33 34 35 |
# File 'lib/randomperson/demographic.rb', line 33 def available_name_files @available_name_files ||= Set.new end |
.classify_true(pattern) ⇒ Set
handle failure
Returns The set for which the pattern matches.
40 41 42 43 44 |
# File 'lib/randomperson/demographic.rb', line 40 def classify_true( pattern ) available_name_files.classify{|s| true if s =~ %r{^.*/[^/]*#{pattern}[^/]*$}i }[true] end |
.load ⇒ Object
puts the demo files into the class instance var
66 67 68 |
# File 'lib/randomperson/demographic.rb', line 66 def self.load self.available_name_files.merge self.load_names if self.available_name_files.empty? end |
Instance Method Details
#get_nots(words) ⇒ Array<String>
Get the negations prepended with “not”, then lop it off“
83 84 85 |
# File 'lib/randomperson/demographic.rb', line 83 def get_nots( words ) words.select{ |w| w =~ /^not/ }.map {|s| s[3..-1] } end |
#get_words(name) ⇒ Array<String>
73 74 75 76 77 |
# File 'lib/randomperson/demographic.rb', line 73 def get_words( name ) words = name.to_s.split( "_" ) words.shift #get rid of the "add" words end |
#get_yesses(words) ⇒ Array<String>
Get the positives.
91 92 93 94 95 96 97 98 99 |
# File 'lib/randomperson/demographic.rb', line 91 def get_yesses( words ) #get just the positives #check the beginning of each word has an uc letter words.reject{|w| w =~ /^not/ }.select{|w| w =~ /^\p{Upper}/ } end |
#loaded_classes ⇒ Object
A hash of all the loaded classes.
16 17 18 |
# File 'lib/randomperson/demographic.rb', line 16 def loaded_classes @loaded_classes ||= {} end |
#require_and_add(yesses) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Requires the name files and adds them to the loaded file hash.
104 105 106 107 108 109 110 |
# File 'lib/randomperson/demographic.rb', line 104 def require_and_add( yesses ) yesses.each {|file_name| require file_name klass = Kernel.const_get( Demographic.translate file_name ) addklass klass } end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Be nice.
135 136 137 138 139 |
# File 'lib/randomperson/demographic.rb', line 135 def respond_to_missing?(name, include_private = false) self.class.method_defined?(name) or name.to_s =~ /^add/ or super end |