Class: RandomPerson::Demographic

Inherits:
Object
  • Object
show all
Includes:
Loader
Defined in:
lib/randomperson/demographic.rb

Overview

Handles the demographics

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loader

included

Constructor Details

#initialize(name = nil, opts = {}) ⇒ Demographic

Initialize the class with the parameters for the population you want.

Examples:

demographic = RandomPerson::Demographic.new( {gender_ratio: [3,5] , age_lower:16, age_upper:35 } )


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_lowerObject

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def age_lower
  @age_lower
end

#age_upperObject

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def age_upper
  @age_upper
end

#femalefirstObject Also known as: female_first

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def femalefirst
  @femalefirst
end

#gender_ratioObject

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def gender_ratio
  @gender_ratio
end

#lastObject Also known as: lastname

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def last
  @last
end

#malefirstObject Also known as: male_first

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def malefirst
  @malefirst
end

#nameObject

the name of this demographic



12
13
14
# File 'lib/randomperson/demographic.rb', line 12

def name
  @name
end

#prefixObject

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def prefix
  @prefix
end

#suffixObject

,:age_ratio



21
22
23
# File 'lib/randomperson/demographic.rb', line 21

def suffix
  @suffix
end

Class Method Details

.available_name_filesSet

The available name files!

Returns:

  • (Set)


33
34
35
# File 'lib/randomperson/demographic.rb', line 33

def available_name_files
  @available_name_files ||= Set.new
end

.classify_true(pattern) ⇒ Set

TODO:

handle failure

Returns The set for which the pattern matches.

Returns:

  • (Set)

    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

.loadObject

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“

Parameters:

Returns:



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>

Parameters:

  • name (String)

Returns:



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.

Parameters:

Returns:



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_classesObject

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.

Returns:

  • (Boolean)


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