Class: Matlock::Data
- Inherits:
-
Object
- Object
- Matlock::Data
- Defined in:
- lib/matlock/data.rb
Class Method Summary collapse
-
.common_words ⇒ Object
Retrieves a hash of common words.
-
.female_name?(str) ⇒ Boolean
Determines if a string is a female first name.
-
.female_names ⇒ Object
Retrieves a hash of female names.
-
.first_name?(str) ⇒ Boolean
Determines if a string is a first name.
-
.first_names ⇒ Object
Retrieves a hash of first names.
-
.load ⇒ Object
Loads all data from the data files.
-
.load_file(filename) ⇒ Object
Loads a single data file as a hash lookup.
-
.male_name?(str) ⇒ Boolean
Determines if a string is a male first name.
-
.male_names ⇒ Object
Retrieves a hash of male names.
-
.surname?(str) ⇒ Boolean
Determines if a string is a surname.
-
.surnames ⇒ Object
Retrieves a hash of surnames.
Class Method Details
.common_words ⇒ Object
Retrieves a hash of common words.
22 |
# File 'lib/matlock/data.rb', line 22 def self.common_words; @common_words; end |
.female_name?(str) ⇒ Boolean
Determines if a string is a female first name.
111 112 113 |
# File 'lib/matlock/data.rb', line 111 def self.female_name?(str) return !str.nil? && !female_names[str.upcase].nil? end |
.female_names ⇒ Object
Retrieves a hash of female names.
19 |
# File 'lib/matlock/data.rb', line 19 def self.female_names; @female_names; end |
.first_name?(str) ⇒ Boolean
Determines if a string is a first name.
93 94 95 |
# File 'lib/matlock/data.rb', line 93 def self.first_name?(str) return !str.nil? && !first_names[str.upcase].nil? end |
.first_names ⇒ Object
Retrieves a hash of first names.
13 |
# File 'lib/matlock/data.rb', line 13 def self.first_names; @first_names; end |
.load ⇒ Object
Loads all data from the data files.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/matlock/data.rb', line 36 def self.load() # Determine path to the data files embedded in the gem. path = File.join(File.(File.dirname(__FILE__)), "data") # Load files into global hashes on the Data class. @surnames = load_file("#{path}/names.surname.txt") @male_names = load_file("#{path}/names.male.txt") @female_names = load_file("#{path}/names.female.txt") @common_words = load_file("#{path}/common.txt") # Join the male and female names into a single lookup. @first_names = @male_names.merge(@female_names) end |
.load_file(filename) ⇒ Object
Loads a single data file as a hash lookup.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/matlock/data.rb', line 55 def self.load_file(filename) lookup = {} index = 1; File.open(filename, "r").each_line do |line| next if line.chomp.length == 0 lookup[line.chomp.upcase] = index index = index + 1 end return lookup end |
.male_name?(str) ⇒ Boolean
Determines if a string is a male first name.
102 103 104 |
# File 'lib/matlock/data.rb', line 102 def self.male_name?(str) return !str.nil? && !male_names[str.upcase].nil? end |
.male_names ⇒ Object
Retrieves a hash of male names.
16 |
# File 'lib/matlock/data.rb', line 16 def self.male_names; @male_names; end |
.surname?(str) ⇒ Boolean
Determines if a string is a surname.
78 79 80 81 82 83 84 85 86 |
# File 'lib/matlock/data.rb', line 78 def self.surname?(str) if !str.nil? str.upcase.split(/\-+/).each do |name| return true unless surnames[name].nil? end end return false end |
.surnames ⇒ Object
Retrieves a hash of surnames.
10 |
# File 'lib/matlock/data.rb', line 10 def self.surnames; @surnames; end |