Class: Matlock::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/matlock/data.rb

Class Method Summary collapse

Class Method Details

.common_wordsObject

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.

Returns:

  • (Boolean)


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_namesObject

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.

Returns:

  • (Boolean)


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_namesObject

Retrieves a hash of first names.



13
# File 'lib/matlock/data.rb', line 13

def self.first_names; @first_names; end

.loadObject

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.expand_path(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.

Parameters:

  • filename (String)

    the name of the file to load.

Returns:

  • a hash lookup of names.



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.

Returns:

  • (Boolean)


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_namesObject

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.

Returns:

  • (Boolean)


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

.surnamesObject

Retrieves a hash of surnames.



10
# File 'lib/matlock/data.rb', line 10

def self.surnames; @surnames; end