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

STANDARD_UPACE_SUPPORT =

Records whether String#upcase supports UTF-8 characters.

(RUBY_VERSION.to_f >= 2.4)
SUPPORTED_LANGUAGES =
i[german english french spanish portuguese dutch].freeze
VERSION =
'1.0.0'.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



55
56
57
# File 'lib/noble_names.rb', line 55

def 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.



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

def 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.



67
68
69
70
71
72
73
74
75
76
# File 'lib/noble_names.rb', line 67

def 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

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/noble_names.rb', line 18

def 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