randomperson 0.0.19 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG +2 -0
- data/Manifest +22 -2
- data/README.markdown +475 -0
- data/lib/randomperson/Loader.rb +62 -0
- data/lib/randomperson/Names/AmericanFemaleFirst.rb +10 -8
- data/lib/randomperson/Names/AmericanLast.rb +13 -10
- data/lib/randomperson/Names/AmericanMaleFirst.rb +11 -8
- data/lib/randomperson/Names/AmericanPrefix.rb +16 -18
- data/lib/randomperson/Names/AmericanSuffix.rb +26 -16
- data/lib/randomperson/Names/AncientGreekFemaleFirst.rb +12 -8
- data/lib/randomperson/Names/AncientGreekLast.rb +11 -8
- data/lib/randomperson/Names/AncientGreekMaleFirst.rb +11 -8
- data/lib/randomperson/Names/AnyLast.rb +11 -9
- data/lib/randomperson/Names/BasqueFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/BasqueLast.rb +9 -8
- data/lib/randomperson/Names/BasqueMaleFirst.rb +10 -9
- data/lib/randomperson/Names/BritishPrefix.rb +10 -16
- data/lib/randomperson/Names/BritishSuffix.rb +21 -16
- data/lib/randomperson/Names/EnglishFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/EnglishLast.rb +12 -11
- data/lib/randomperson/Names/EnglishMaleFirst.rb +10 -9
- data/lib/randomperson/Names/EnglishPrefix.rb +11 -15
- data/lib/randomperson/Names/FinnishFemaleFirst.rb +10 -8
- data/lib/randomperson/Names/FinnishLast.rb +9 -8
- data/lib/randomperson/Names/FinnishMaleFirst.rb +10 -9
- data/lib/randomperson/Names/FinnishPrefix.rb +16 -18
- data/lib/randomperson/Names/FrenchFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/FrenchLast.rb +12 -11
- data/lib/randomperson/Names/FrenchMaleFirst.rb +11 -10
- data/lib/randomperson/Names/GermanFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/GermanLast.rb +9 -8
- data/lib/randomperson/Names/GermanMaleFirst.rb +10 -9
- data/lib/randomperson/Names/ScottishFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/ScottishLast.rb +11 -10
- data/lib/randomperson/Names/ScottishMaleFirst.rb +13 -12
- data/lib/randomperson/Names/ScottishPrefix.rb +10 -16
- data/lib/randomperson/Names/SpanishFemaleFirst.rb +11 -10
- data/lib/randomperson/Names/SpanishLast.rb +15 -12
- data/lib/randomperson/Names/SpanishMaleFirst.rb +12 -11
- data/lib/randomperson/Names/SpanishPrefix.rb +17 -19
- data/lib/randomperson/Names/SwedishFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/SwedishLast.rb +10 -9
- data/lib/randomperson/Names/SwedishMaleFirst.rb +15 -14
- data/lib/randomperson/Names/ThaiFemaleFirst.rb +9 -8
- data/lib/randomperson/Names/ThaiFirst.rb +9 -8
- data/lib/randomperson/Names/ThaiLast.rb +9 -8
- data/lib/randomperson/Names/ThaiMaleFirst.rb +9 -8
- data/lib/randomperson/Names/ThaiRomanisedFemaleFirst.rb +10 -9
- data/lib/randomperson/Names/ThaiRomanisedLast.rb +11 -10
- data/lib/randomperson/Names/ThaiRomanisedMaleFirst.rb +10 -9
- data/lib/randomperson/Names/WelshFemaleFirst.rb +9 -8
- data/lib/randomperson/Names/WelshLast.rb +11 -10
- data/lib/randomperson/Names/WelshMaleFirst.rb +9 -8
- data/lib/randomperson/Names/WelshPrefix.rb +10 -16
- data/lib/randomperson/Outputter.rb +53 -0
- data/lib/randomperson/demographic.rb +75 -0
- data/lib/randomperson/ext/Array.rb +6 -2
- data/lib/randomperson/generator.rb +54 -77
- data/lib/randomperson/name.rb +18 -13
- data/lib/randomperson/person.rb +2 -0
- data/lib/randomperson/version.rb +1 -1
- data/lib/randomperson.rb +126 -1
- data/spec/AmericanSuffix_spec.rb +37 -0
- data/spec/AnyLast_spec.rb +25 -0
- data/spec/Last_and_First_Names_spec.rb +113 -0
- data/spec/array_spec.rb +49 -0
- data/spec/demographic_spec.rb +15 -0
- data/spec/generator_spec.rb +1 -4
- data/spec/name_spec.rb +3 -2
- data/spec/randomperson_spec.rb +321 -0
- data/spec/rspec_helper.rb +22 -0
- data/spec/support/helpers.rb +13 -0
- data/spec/support/matchers/all.rb +14 -0
- data/spec/support/matchers/be_in.rb +9 -0
- data/spec/support/shared_examples/names.rb +59 -0
- data/test/benchmarks/concat_or_add.rb +30 -0
- data/test/benchmarks/my_assoc_or_select.rb +60 -0
- data.tar.gz.sig +0 -0
- metadata +35 -7
- metadata.gz.sig +2 -3
- data/README +0 -324
- data/lib/randomperson/choice.rb +0 -113
- data/spec/choice_spec.rb +0 -13
@@ -1,28 +1,17 @@
|
|
1
1
|
module RandomPerson
|
2
2
|
|
3
|
-
|
3
|
+
module Generator
|
4
4
|
|
5
|
-
require_relative './choice.rb'
|
6
5
|
require_relative './person.rb'
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
# Sets up a list of generators
|
7
|
+
# Build a demographic generator
|
8
|
+
# @param [RandomPerson::Demographic] demographic The demographic you would like.
|
11
9
|
# @example
|
12
|
-
#
|
13
|
-
def
|
14
|
-
@generators = [ ]
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
# Build a choice generator
|
19
|
-
# @param [RandomPerson::Choice] choice A Choice object.
|
20
|
-
# @example
|
21
|
-
# g.make_generator choice
|
22
|
-
def make_generator( choice )
|
10
|
+
# Generator.make_generator demographic
|
11
|
+
def self.make_generator( demographic )
|
23
12
|
f = -> {
|
24
|
-
g = Generator.pick_gender(
|
25
|
-
age = Generator.pick_age(
|
13
|
+
g = Generator.pick_gender( demographic.gender_ratio )
|
14
|
+
age = Generator.pick_age( demographic.age_lower, demographic.age_upper)
|
26
15
|
|
27
16
|
person = Person.new(
|
28
17
|
:gender => g,
|
@@ -31,80 +20,68 @@ module RandomPerson
|
|
31
20
|
)
|
32
21
|
|
33
22
|
if person.gender == 'm'
|
34
|
-
person.first =
|
23
|
+
person.first = demographic.malefirst.execute( person ) unless demographic.malefirst.nil?
|
35
24
|
else
|
36
|
-
person.first =
|
25
|
+
person.first = demographic.femalefirst.execute( person ) unless demographic.femalefirst.nil?
|
37
26
|
end
|
38
27
|
|
39
|
-
person.last =
|
40
|
-
person.prefix =
|
41
|
-
person.suffix =
|
28
|
+
person.last = demographic.last.execute( person ) unless demographic.last.nil? #lastname,
|
29
|
+
person.prefix = demographic.prefix.execute( person ) unless demographic.prefix.nil? #title
|
30
|
+
person.suffix = demographic.suffix.execute( person ) unless demographic.suffix.nil?#suffix
|
42
31
|
|
43
32
|
person
|
44
33
|
}
|
45
|
-
|
46
|
-
|
34
|
+
|
35
|
+
f
|
47
36
|
end
|
48
37
|
|
49
|
-
class << self
|
50
|
-
|
51
|
-
#create a ratio that is made up of ranges
|
52
|
-
#to help hit with multiple valued ratios
|
53
|
-
def ratiod( ratio=[1,1] )
|
54
|
-
sum = ratio.reduce(:+) #sum
|
55
|
-
mult = 100.divmod( sum ).first #get a bigger spread of numbers
|
56
|
-
ratio.map! { |n| n * mult }
|
57
|
-
new_ratio = ratio.inject([0..0]) {|acc,n| acc + [acc.last.last ... (n + acc.last.last)] }
|
58
|
-
new_ratio.shift #get rid of 0..0
|
59
|
-
return new_ratio
|
60
|
-
end
|
61
38
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
39
|
+
#create a ratio that is made up of ranges
|
40
|
+
#to help hit with multiple valued ratios
|
41
|
+
def self.ratiod( ratio=[1,1] )
|
42
|
+
sum = ratio.reduce(:+) #sum
|
43
|
+
mult = 100.divmod( sum ).first #get a bigger spread of numbers
|
44
|
+
ratio.map! { |n| n * mult }
|
45
|
+
new_ratio = ratio.inject([0..0]) {|acc,n| acc + [acc.last.last ... (n + acc.last.last)] }
|
46
|
+
new_ratio.shift #get rid of 0..0
|
47
|
+
return new_ratio
|
48
|
+
end
|
49
|
+
|
50
|
+
# 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.
|
51
|
+
# @example To only get males:
|
52
|
+
# pick_gender( [1,0] )
|
53
|
+
# @example To only get females:
|
54
|
+
# pick_gender( [0,1] )
|
55
|
+
# @example To be likely to get 2 females to every male:
|
56
|
+
# pick_gender( [1,2] )
|
57
|
+
# @param [optional,Array<Integer,Integer>] ratio 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].
|
58
|
+
# @return [String] Returns a 'f' for female or an 'm' for male.
|
59
|
+
def self.pick_gender( ratio=[1,1] ) #male first
|
60
|
+
if ratio.length == 2
|
61
|
+
return 'f' if ratio.first == 0
|
62
|
+
return 'm' if ratio.last == 0
|
86
63
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
Time.local( year, month, day )
|
64
|
+
rat = ratiod( ratio )
|
65
|
+
r = rand(rat.last.last)
|
66
|
+
case r
|
67
|
+
when rat[0] then 'm'
|
68
|
+
when rat[1] then 'f'
|
93
69
|
end
|
94
|
-
|
95
|
-
|
96
|
-
end #class << self
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
70
|
+
end
|
101
71
|
|
102
|
-
def
|
103
|
-
|
72
|
+
def self.pick_age( lower=0, upper=100 )
|
73
|
+
age = rand(upper - lower).to_i + lower
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.pick_dob( y=16 )
|
77
|
+
year = Time.now.year - y
|
78
|
+
month = rand(12) + 1;
|
79
|
+
day = rand( Date.days_in_month( year, month ) ) + 1
|
80
|
+
Time.local( year, month, day )
|
104
81
|
end
|
105
82
|
|
106
83
|
|
107
84
|
|
108
85
|
end
|
109
86
|
|
110
|
-
end
|
87
|
+
end
|
data/lib/randomperson/name.rb
CHANGED
@@ -1,20 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative "./Outputter.rb"
|
4
|
+
|
5
|
+
|
1
6
|
module RandomPerson
|
2
7
|
|
3
8
|
class Name
|
9
|
+
include Outputter
|
10
|
+
|
11
|
+
attr_accessor :formats, :formats_ratiod, :names, :possibles
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@possibles = Hash[ @formats_ratiod.zip @formats ] if @possibles.nil? unless @formats_ratiod.nil?
|
15
|
+
|
16
|
+
@on_execute ||= for_standard
|
17
|
+
|
18
|
+
@possibles
|
19
|
+
end
|
4
20
|
|
5
|
-
attr_accessor :formats, :formats_ratiod, :names
|
6
21
|
|
7
|
-
def execute( person=nil )
|
8
|
-
if @formats.length > 1
|
9
|
-
r = rand(@formats_ratiod.last.end)
|
10
|
-
i = @formats_ratiod.index_in_range( r )
|
11
|
-
f = @formats.by_index(i).last
|
12
|
-
else
|
13
|
-
k, f = @formats.first #throw away the key name
|
14
|
-
end
|
15
|
-
f.( @names )
|
16
|
-
end
|
17
22
|
|
18
|
-
end
|
23
|
+
end # class
|
19
24
|
|
20
|
-
end
|
25
|
+
end # RandomPerson
|
data/lib/randomperson/person.rb
CHANGED
@@ -5,6 +5,8 @@ module RandomPerson
|
|
5
5
|
|
6
6
|
attr_accessor :gender, :age, :dob, :first, :last, :prefix, :suffix
|
7
7
|
|
8
|
+
alias :lastname :last
|
9
|
+
|
8
10
|
## @param [optional,Hash] h A hash of the person's attributes
|
9
11
|
# @option h [String] :gender nil Gender, 'm' for male or 'f' for female
|
10
12
|
# @option h [Integer] :age nil Age
|
data/lib/randomperson/version.rb
CHANGED
data/lib/randomperson.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
+
|
3
|
+
|
2
4
|
module RandomPerson
|
3
5
|
|
4
6
|
#require all the scaffolding
|
@@ -14,10 +16,133 @@ module RandomPerson
|
|
14
16
|
require_relative './randomperson/ext/Kernel.rb'
|
15
17
|
require_relative './randomperson/ext/Set.rb'
|
16
18
|
|
17
|
-
require_relative './randomperson/
|
19
|
+
require_relative './randomperson/demographic.rb'
|
18
20
|
require_relative './randomperson/generator.rb'
|
19
21
|
require_relative './randomperson/name.rb'
|
20
22
|
require_relative './randomperson/person.rb'
|
21
23
|
#require_relative './randomperson/ratio.rb'
|
22
24
|
|
25
|
+
|
26
|
+
class Facade
|
27
|
+
|
28
|
+
def demographics
|
29
|
+
@demos ||= {}
|
30
|
+
end
|
31
|
+
|
32
|
+
#class instance variable to keep track of generators
|
33
|
+
def generators
|
34
|
+
@generators ||= {}
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
# little factory
|
39
|
+
def demographic( name=nil, opts={} )
|
40
|
+
if name.kind_of? Hash
|
41
|
+
opts = name
|
42
|
+
name = nil
|
43
|
+
end
|
44
|
+
name = demographics.length.to_s if name.nil?
|
45
|
+
demo = Demographic.new name, opts
|
46
|
+
demographics[name] = demo
|
47
|
+
generators.delete name # just in case it already exists
|
48
|
+
demo
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
# start afresh
|
53
|
+
def clear
|
54
|
+
@demos = nil
|
55
|
+
@generators = nil
|
56
|
+
@person = nil
|
57
|
+
@last_demo_name = nil
|
58
|
+
end
|
59
|
+
|
60
|
+
alias :reset :clear
|
61
|
+
attr_accessor :last_demo_name
|
62
|
+
|
63
|
+
|
64
|
+
# The last person generated.
|
65
|
+
# If a demographic name is given that is different to the last then a new person is generated. If no name is given then the last is used.
|
66
|
+
def person( demo_name=nil )
|
67
|
+
person, last_demo_name =
|
68
|
+
if demo_name.nil?
|
69
|
+
|
70
|
+
if @person.nil?
|
71
|
+
|
72
|
+
# either generate a new one
|
73
|
+
gen_new( demo_name ) # gen a new person and get back demo name
|
74
|
+
else
|
75
|
+
|
76
|
+
# get last one
|
77
|
+
[@person, @last_demo_name]
|
78
|
+
end
|
79
|
+
else # demo name given
|
80
|
+
|
81
|
+
if demographics.has_key? demo_name
|
82
|
+
|
83
|
+
if demo_name == @last_demo_name
|
84
|
+
|
85
|
+
[@person, @last_demo_name]
|
86
|
+
else
|
87
|
+
|
88
|
+
gen_new( demo_name )
|
89
|
+
end
|
90
|
+
else
|
91
|
+
|
92
|
+
if demographics.nil? || demographics.empty?
|
93
|
+
|
94
|
+
raise "No demographics have been selected yet! Try something like r.demographic.add_Spanish..."
|
95
|
+
else # that demo name doesn't exist...
|
96
|
+
|
97
|
+
[nil, @last_demo_name] # so preserve the last good demo name
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
return nil if person.nil?
|
102
|
+
@person, @last_demo_name = [person, last_demo_name]
|
103
|
+
|
104
|
+
@person
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
# generate a new person
|
109
|
+
# either with the last demographic loaded, or a specific one by passing the name.
|
110
|
+
def generate( demo_name=nil )
|
111
|
+
ds = gen_new( demo_name )
|
112
|
+
ds.nil? ? nil : ds.first
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
# If not given a demographic's name then the *last demographic defined* will be used.
|
117
|
+
def gen_new( demo_name=nil )
|
118
|
+
if demographics.nil? || demographics.empty?
|
119
|
+
raise "No demographics have been selected yet! Try something like r.demographic.add_Spanish..."
|
120
|
+
end
|
121
|
+
demo_name, demo = if demo_name.nil?
|
122
|
+
demographics.to_a.last # this produces a 2 dimensional array
|
123
|
+
else
|
124
|
+
demographics.assoc(demo_name)
|
125
|
+
end
|
126
|
+
|
127
|
+
if demo_name
|
128
|
+
@last_demo_name = demo_name
|
129
|
+
unless generators.has_key? demo.name
|
130
|
+
generators[demo.name] = Generator.make_generator( demo )
|
131
|
+
end
|
132
|
+
|
133
|
+
@person = generators[@last_demo_name].call
|
134
|
+
|
135
|
+
[@person, @last_demo_name]
|
136
|
+
else
|
137
|
+
nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end # class
|
142
|
+
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
def RandomPerson
|
147
|
+
RandomPerson::Facade.new
|
23
148
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative "./rspec_helper.rb"
|
4
|
+
require_relative "./support/shared_examples/names.rb"
|
5
|
+
require_relative "../lib/randomperson/Names/AmericanSuffix.rb"
|
6
|
+
require_relative "../lib/randomperson/person.rb"
|
7
|
+
|
8
|
+
module RandomPerson
|
9
|
+
module Names
|
10
|
+
|
11
|
+
describe AmericanSuffix do
|
12
|
+
let(:instance) { AmericanSuffix.new }
|
13
|
+
subject{ instance }
|
14
|
+
it_should_behave_like "a Name class"
|
15
|
+
describe :execute do
|
16
|
+
context "Who is young" do
|
17
|
+
let(:p) { instance.execute Person.new( age: 16) }
|
18
|
+
subject { p }
|
19
|
+
it { should_not be_nil }
|
20
|
+
1000.times do |_|
|
21
|
+
it { should be_in instance.names[0..-2] }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
context "Who is older" do
|
25
|
+
let(:p) { instance.execute Person.new( age: 18) }
|
26
|
+
subject { p }
|
27
|
+
it { should_not be_nil }
|
28
|
+
1000.times do |_|
|
29
|
+
it { should be_in instance.names[1..-1] }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end # execute
|
33
|
+
|
34
|
+
end # AmericanSuffix
|
35
|
+
|
36
|
+
end # Names
|
37
|
+
end # RandomPerson
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative "./rspec_helper.rb"
|
4
|
+
require_relative "./support/shared_examples/names.rb"
|
5
|
+
require_relative "../lib/randomperson/Names/AnyLast.rb"
|
6
|
+
require_relative "../lib/randomperson/person.rb"
|
7
|
+
|
8
|
+
module RandomPerson
|
9
|
+
module Names
|
10
|
+
|
11
|
+
describe AnyLast do
|
12
|
+
let(:instance) { AnyLast.new }
|
13
|
+
subject{ instance }
|
14
|
+
it_should_behave_like "a Name class"
|
15
|
+
describe :execute do
|
16
|
+
subject { instance.execute Person.new( gender: "m", age: rand(100)) }
|
17
|
+
it { should_not be_nil }
|
18
|
+
1000.times do |_|
|
19
|
+
it { should match /^ \b\p{Upper}\p{Alpha}+?\b (?:(?:\s|-)\b\p{Upper}\p{Alpha}+?\b)?$/x }
|
20
|
+
end
|
21
|
+
end # execute
|
22
|
+
end # AnyLast
|
23
|
+
|
24
|
+
end # Names
|
25
|
+
end # RandomPerson
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative "./rspec_helper.rb"
|
4
|
+
require_relative "./support/shared_examples/names.rb"
|
5
|
+
require_relative "../lib/randomperson/person.rb"
|
6
|
+
require_relative "../lib/randomperson/demographic.rb"
|
7
|
+
|
8
|
+
module RandomPerson
|
9
|
+
module Names
|
10
|
+
|
11
|
+
available_classes = Demographic.load_names
|
12
|
+
available_classes.each do |klass|
|
13
|
+
klass = Demographic.requiring klass
|
14
|
+
|
15
|
+
k = eval "RandomPerson::Names::#{klass}"
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
word = /\b\p{Upper}\p{Alpha}+?\b (?:(?:\s|-)\b\p{Upper}\p{Alpha}+?\b)?/x
|
20
|
+
hypenated_rgx = /^#{word}$/x
|
21
|
+
french_rgx = /^(?:(?: (?:\bLe\b\s) | (?:\bd[eu]\b)\s) | (?:#{word}-))? #{word}$/x
|
22
|
+
hypenated_many_rgx = /^#{word} (?:(?:\s|-)#{word})*$/x
|
23
|
+
unhypenated_rgx = /^#{word}\s#{word}?$/x
|
24
|
+
poss_unhyph = /^#{word}(?:\s#{word})?$/
|
25
|
+
thai_style = /^\b\p{Graph}+?\b$/x
|
26
|
+
default_fix = {times: 50 }
|
27
|
+
fixtures = {}
|
28
|
+
fixtures.default = default_fix
|
29
|
+
fixtures["SpanishLast"] = default_fix.merge({ rgx: /^ \b\p{Upper}(?:\p{Alpha}+-)?\p{Alpha}+?\b\s (?: (?:\bde\b\s) | (?: \b\p{Upper}(?:\p{Alpha}+-)?\p{Alpha}+?\b\s (?:\by\b\s)? ) )? \b\p{Upper}(?:\p{Alpha}+-)?\p{Alpha}+?\b$/x })
|
30
|
+
fixtures["AmericanLast"] = default_fix.merge({ rgx: hypenated_rgx } )
|
31
|
+
fixtures["AnyLast"] = default_fix.merge({ rgx: hypenated_rgx } )
|
32
|
+
fixtures["BritishLast"] = default_fix.merge({ rgx: hypenated_rgx } )
|
33
|
+
fixtures["EnglishLast"] = default_fix.merge({ rgx: hypenated_rgx } )
|
34
|
+
fixtures["ScottishLast"] = default_fix.merge({ rgx: hypenated_rgx } )
|
35
|
+
fixtures["WelshLast"] = default_fix.merge({ rgx: poss_unhyph } )
|
36
|
+
fixtures["FrenchLast"] = default_fix.merge({ rgx: french_rgx} )
|
37
|
+
fixtures["FrenchFemaleFirst"] = default_fix.merge({ rgx: hypenated_rgx } )
|
38
|
+
fixtures["FrenchMaleFirst"] = default_fix.merge({ rgx: hypenated_rgx } )
|
39
|
+
fixtures["BasqueMaleFirst"] = default_fix.merge({ rgx: hypenated_rgx } )
|
40
|
+
fixtures["BasqueLast"] = default_fix.merge({ rgx: unhypenated_rgx } )
|
41
|
+
fixtures["ThaiRomanisedLast"] = default_fix.merge({ rgx: hypenated_rgx } )
|
42
|
+
fixtures["ThaiFemaleFirst"] = default_fix.merge({ rgx: thai_style } )
|
43
|
+
fixtures["ThaiMaleFirst"] = default_fix.merge({ rgx: thai_style} )
|
44
|
+
fixtures["ThaiLast"] = default_fix.merge({ rgx: thai_style} )
|
45
|
+
fixtures["ThaiFirst"] = default_fix.merge({ rgx: thai_style} )
|
46
|
+
fixtures["ThaiRomanisedMaleFirst"] = default_fix.merge({ rgx: hypenated_many_rgx} )
|
47
|
+
fixtures["ThaiRomanisedFemaleFirst"] = default_fix.merge({ rgx: hypenated_many_rgx} )
|
48
|
+
fixtures["AncientGreekLast"] = default_fix.merge(
|
49
|
+
{ rgx: /^\bof\b\s(?:\b\p{Alpha}+?\b(?:\s|-)){0,2}\b\p{Alpha}+?\b$/x } )
|
50
|
+
|
51
|
+
#PREFIXES
|
52
|
+
fix_fixtures = {}
|
53
|
+
fix_fixtures["FinnishPrefix"] = {
|
54
|
+
young_male: {gender: "m", age: rand(17), in: ["herra"]}, #rgx: /^\bherra\b$/x },
|
55
|
+
older_male: {gender: "m", in: ["herra", "Dr"]}, #rgx: /^\b(?:herra|Dr)\b$/x },
|
56
|
+
young_female: {gender: "f", age: rand(17), in: ["neiti"]}, #rgx: /^\bneiti\b$/x },
|
57
|
+
older_female: {gender: "f", age: rand(17), in: ["rouva", "neiti", "Dr"]} #rgx: /^\b(?:rouva)|(?:neiti)|(?:Dr)\b$/x },
|
58
|
+
}
|
59
|
+
fix_fixtures["SpanishPrefix"] = {
|
60
|
+
young_male: {gender: "m", age: rand(17), in: ["Sr."]}, #rgx: /^\bSr\b\.$/x },
|
61
|
+
older_male: {gender: "m", in: ["Sr.", "Dr."]}, #rgx: /^\b(?:[SD]r\b\.)$/x },
|
62
|
+
young_female: {gender: "f", age: rand(17), in: ["Srta."]}, #rgx: /^\bSrta\b.$/x },
|
63
|
+
older_female: {gender: "f", in: ["Dra.", "Srta.", "Sra."]}#rgx: /^[DS]rt?a\b\.$/x }
|
64
|
+
}
|
65
|
+
fix_fixtures["AmericanPrefix"] = {
|
66
|
+
young_female: {gender: "f", age: rand(17), rgx: /^\bMiss\b$/},
|
67
|
+
older_female: {gender: "f", in: ["Miss", "Mrs.", "Dr.", "Ms."]}, #rgx: /^\bM(?:(?:iss\b)|(?:(?:r?s)\b\.))$|(?:Dr\b\.$)/x },
|
68
|
+
young_male: {gender: "m", age: rand(17), in: ["Mr."]}, #rgx: /^\bMr\b\.$/},
|
69
|
+
older_male: {gender: "m", in: ["Mr.", "Dr."]},
|
70
|
+
} #rgx: /^\b[MD]r\b\.$/}, }
|
71
|
+
fix_fixtures["AmericanSuffix"] = {
|
72
|
+
young_female: {},
|
73
|
+
older_female: {},
|
74
|
+
young_male: {},
|
75
|
+
older_male: {} }
|
76
|
+
fix_fixtures["BritishPrefix"] = {
|
77
|
+
young_female: {gender: "f", age: rand(17), rgx: /^\bMiss\b$/},
|
78
|
+
older_female: {gender: "f", rgx: /^\b(?:M(?:is|r)?s)|(?:Dr)\b$/},
|
79
|
+
young_male: {gender: "m", age: rand(17), rgx: /^\bMr\b$/},
|
80
|
+
older_male: {gender: "m", rgx: /^\b[MD]r\b$/}, }
|
81
|
+
fix_fixtures["EnglishPrefix"] = fix_fixtures["ScottishPrefix"] = fix_fixtures["WelshPrefix"] = fix_fixtures["BritishPrefix"]
|
82
|
+
|
83
|
+
#SUFFIXES
|
84
|
+
fix_fixtures["BritishSuffix"] = {
|
85
|
+
young_female: {age: rand(17), gender: "f", in: [""] },
|
86
|
+
older_female: { gender: "f", in: %w( OBE MBE GBE KBE DBE CBE JP GM PhD BSc BA )},
|
87
|
+
young_male: {age: rand(17), gender: "m", in: [""] },
|
88
|
+
older_male: {gender: "m", in: %w( OBE MBE GBE KBE DBE CBE JP GM PhD BSc BA ) },
|
89
|
+
}
|
90
|
+
fix_fixtures["AmericanSuffix"] = { # strictly speaking, gender doesn't matter here, but I'll leave the test in.
|
91
|
+
young_female: {age: rand(17), gender: "f", in: [ 'Jr.', ''] + %w( I II III IV V) },
|
92
|
+
young_male: { age: rand(17), gender: "m", in: [ 'Jr.', ''] + %w( I II III IV V ) },
|
93
|
+
older_male: { gender: "m", in: [ ''] + %w( I II III IV V Sr. ) },
|
94
|
+
older_female: { gender: "f", in: [ ''] + %w( I II III IV V Sr. ) },
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
puts "Describe #{klass}" #because rspec doesn't output this before it blow up! Sheesh!!
|
99
|
+
describe klass do
|
100
|
+
let(:instance) { k.new }
|
101
|
+
subject{ instance }
|
102
|
+
it_should_behave_like "a Name class"
|
103
|
+
if ["Prefix", "Suffix"].any?{|x| klass.include? x }
|
104
|
+
|
105
|
+
it_should_behave_like "it cares about gender and/or age", fix_fixtures[klass]
|
106
|
+
else
|
107
|
+
it_should_behave_like "a name generator", fixtures[klass]
|
108
|
+
end
|
109
|
+
end # describe
|
110
|
+
end # loop
|
111
|
+
|
112
|
+
end # Names
|
113
|
+
end # RandomPerson
|
data/spec/array_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative "./rspec_helper.rb"
|
4
|
+
require_relative "../lib/randomperson/ext/array.rb"
|
5
|
+
|
6
|
+
describe Array do
|
7
|
+
|
8
|
+
describe :rand do
|
9
|
+
let(:m1) { 1_000_000 }
|
10
|
+
let(:r) { m1 * 10 }
|
11
|
+
let(:range) {
|
12
|
+
u = rand(r) + m1
|
13
|
+
(u..u+10)
|
14
|
+
}
|
15
|
+
subject { range.to_a.rand }
|
16
|
+
it { range.cover?( subject ).should be_true }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe :index_in_range do
|
20
|
+
context "Given an number" do
|
21
|
+
let(:max) { 99 }
|
22
|
+
let(:array) {
|
23
|
+
array = []
|
24
|
+
i = 0
|
25
|
+
until i >= max
|
26
|
+
last = array.empty? ? 0 : array.last.max + 1
|
27
|
+
i = (i == 0 ? rand(99) : rand(max - i)) + last
|
28
|
+
array << (last..i)
|
29
|
+
end
|
30
|
+
array
|
31
|
+
}
|
32
|
+
context "That is not in the ranges in the array" do
|
33
|
+
let(:n) { max + 1}
|
34
|
+
subject{ array.index_in_range( n ) }
|
35
|
+
it { should be_nil }
|
36
|
+
end
|
37
|
+
context "That is in the ranges in the array" do
|
38
|
+
0.upto(99) do |n|
|
39
|
+
subject{ array.index_in_range( n ) }
|
40
|
+
it { subject.should_not be_nil }
|
41
|
+
it { should be_a_kind_of Integer}
|
42
|
+
it { should be_between( 0, array.length - 1 ) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end # :index_in_range
|
47
|
+
|
48
|
+
|
49
|
+
end # Array
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'rspec'
|
4
|
+
require_relative "../lib/randomperson/demographic.rb"
|
5
|
+
|
6
|
+
|
7
|
+
module RandomPerson
|
8
|
+
|
9
|
+
describe Demographic do
|
10
|
+
|
11
|
+
it "should not blow up on instatiation" do
|
12
|
+
Demographic.new.should_not == nil
|
13
|
+
end
|
14
|
+
end # Demographic
|
15
|
+
end # RandomPerson
|
data/spec/generator_spec.rb
CHANGED
data/spec/name_spec.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
require 'rspec'
|
2
4
|
require_relative "../lib/randomperson/name.rb"
|
3
5
|
|
4
|
-
|
5
6
|
module RandomPerson
|
6
7
|
|
7
8
|
describe Name do
|
@@ -10,4 +11,4 @@ module RandomPerson
|
|
10
11
|
Name.new.should_not == nil
|
11
12
|
end
|
12
13
|
end # Name
|
13
|
-
end # RandomPerson
|
14
|
+
end # RandomPerson
|