Module: NobleNames

Defined in:
lib/noble_names.rb,
lib/noble_names/data.rb,
lib/noble_names/config.rb,
lib/noble_names/version.rb,
lib/noble_names/initializer.rb,
lib/noble_names/core_ext/string.rb

Overview

:nodoc:

Defined Under Namespace

Modules: CoreExt, Data Classes: Configuration

Constant Summary collapse

SUPPORTED_LANGUAGES =
[:german, :english, :french, :spanish, :portuguese].freeze
VERSION =
'0.1.3'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



9
10
11
# File 'lib/noble_names/config.rb', line 9

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.capitalize(word) ⇒ String

Upcases the first small letters in each word, seperated by hyphons. But beware, words seperated by spaces stay small.

Examples:

capitalize('hans-ebert')  #=> 'Hans-Ebert'
capitalize('john')        #=> 'John'
capitalize('john james')  #=> 'John james'

Returns:

  • (String)

    the capitalized word.



32
33
34
35
36
# File 'lib/noble_names.rb', line 32

def self.capitalize(word)
  word.gsub first_small_letters do |letter|
    upcase(letter)
  end
end

.configure {|configuration| ... } ⇒ Object

Here you can configure how the module behaves.

Examples:

Only use german

NobleNames.configure do |config|
  config.languages = :german
end

Use multiple languages

NobleNames.configure do |config|
  config.languages = [:german, :spanish]
end

Yields:



23
24
25
# File 'lib/noble_names/config.rb', line 23

def self.configure
  yield(configuration)
end

.first_small_lettersRegexp

A Regex literal to find the first letter of a string as well as the first letter after a hyphon.

Returns:

  • (Regexp)

    first_small_letters the regexp in question



57
58
59
# File 'lib/noble_names.rb', line 57

def self.first_small_letters
  /((\A.|(?<=\-).))/
end

.in_particle_list?(word) ⇒ Boolean

Checks weither a word is in the nobility particle list.

Parameters:

  • word (String)

    the word that is checked.

Returns:

  • (Boolean)

    true if word is in the particle_list, false otherwise.



65
66
67
# File 'lib/noble_names.rb', line 65

def self.in_particle_list?(word)
  Data.particles.include? word
end

.initializeObject

This is wrapped in a method so it isn't immediatly evaluated when its loaded



7
8
9
10
11
# File 'lib/noble_names/initializer.rb', line 7

def self.initialize
  String.class_eval do
    include NobleNames::CoreExt::String
  end
end

.noble_capitalize(word) ⇒ String

Capitalizes a word if it needs to be capitalized.

Parameters:

  • word (String)

    the word that needs to be capitalized.

Returns:

  • (String)

    word the word either capitalized or not.



13
14
15
16
17
18
19
20
21
22
# File 'lib/noble_names.rb', line 13

def self.noble_capitalize(word)
  prefix = prefix?(word)
  if in_particle_list?(word)
    word.downcase
  elsif prefix
    capitalize(prefix) + capitalize(word.gsub(prefix, ''))
  else
    capitalize(word)
  end
end

.prefix?(word) ⇒ String

Checks weither a word has a prefix as defined in data/prefixes.yml and returns it.

Examples:

prefix?('james mcdormer')           #=> 'mc'

Parameters:

  • word (String)

    the word that needs to be checked.

Returns:

  • (String)

    pre the Prefix of the word. nil if it has none.



76
77
78
79
80
81
# File 'lib/noble_names.rb', line 76

def self.prefix?(word)
  Data.prefixes.each do |pre|
    return pre if (word =~ Regexp.new(pre)) == 0
  end
  nil
end

.upcase(letter) ⇒ String

Upcases a letter even if it is a german mutated vowel.

Examples:

upcase('t')         #=> 'T'

Returns:

  • (String)

    letter the upcased letter.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/noble_names.rb', line 42

def self.upcase(letter)
  match = letter.match(/ä|ö|ü/)
  if match
    case match.to_s
    when 'ä' then 'Ä'
    when 'ö' then 'Ö'
    when 'ü' then 'Ü'
    end
  else letter.upcase
  end
end