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/match_index.rb,
lib/noble_names/core_ext/string.rb

Overview

:nodoc:

Defined Under Namespace

Modules: CoreExt, Data Classes: Configuration, MatchIndex

Constant Summary collapse

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



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

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.business_particle?(word) ⇒ Boolean

Checks weither a word is in the business particle list

Parameters:

  • word (String)

    The word in question.

Returns:

  • (Boolean)

    result true if word is a business-particle false otherwise



74
75
76
# File 'lib/noble_names.rb', line 74

def self.business_particle?(word)
  Data.business_particles.in_particle_list? word
end

.capitalize(word) ⇒ String

Upcases the first small letters in each word, seperated by hyphons. The word is also not capitalized if it already contains a capitalized letter. This is to allow Business Names to have custom capitalization. But beware, words seperated by spaces stay small.

Examples:

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

Returns:

  • (String)

    the capitalized word.



37
38
39
40
41
42
43
44
45
# File 'lib/noble_names.rb', line 37

def self.capitalize(word)
  if word =~ /[A-Z]|Ä|Ö|Ü/
    word
  else
    word.gsub first_small_letters do |letter|
      upcase(letter)
    end
  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:



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

def self.configure
  yield(configuration)
end

.correct_business_particles(words) ⇒ Array

Corrects only the business particle and leaves the other words alone.

Examples:

A Business Name

correct_business_particles([
  'cool', 'and', 'hip', 'gmbh'
])                    #=> ['cool', 'and', 'hip', 'GmbH']

Parameters:

  • words (Array)

    An array of words to be checked.

Returns:

  • (Array)

    words An array of corrected words.



86
87
88
89
90
91
92
93
94
95
# File 'lib/noble_names.rb', line 86

def self.correct_business_particles(words)
  words.map! do |word|
    if business_particle?(word)
      word
        .replace(Data.business_particles.particles[word.downcase])
    else
      noble_capitalize(word)
    end
  end
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



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

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

.initializeObject

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



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

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.



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

def self.noble_capitalize(word)
  prefix = Data.nobility_prefixes.prefix?(word)
  if Data.nobility_particles.in_particle_list?(word)
    word.downcase
  elsif prefix
    capitalize(prefix) + capitalize(word.gsub(prefix, ''))
  else
    capitalize(word)
  end
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.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/noble_names.rb', line 51

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