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

.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

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



26
27
28
# File 'lib/noble_names.rb', line 26

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) ⇒ Object

Capitalizes a word if it needs to be capitalized.

Parameters:

  • word (String)

    the word that needs to be capitalized.

  • word (String)

    the word either capitalized or not.



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

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



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

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