Module: Ore::Naming

Included in:
Generator
Defined in:
lib/ore/naming.rb

Overview

Provides methods for guessing the namespaces and directories of projects. Naming uses the naming conventions of project names defined by the Ruby Packaging Standard (RPS).

Since:

  • 0.9.0

Constant Summary collapse

BIN_DIR =

The directory which contains executables for a project

Since:

  • 0.9.0

'bin'
LIB_DIR =

The directory which contains the code for a project

Since:

  • 0.9.0

'lib'
EXT_DIR =

The directory which contains C extension code for a project

Since:

  • 0.9.0

'ext'
DATA_DIR =

The directory which contains data files for a project

Since:

  • 0.9.0

'data'
TEST_DIR =

The directory which contains unit-tests for a project

Since:

  • 0.9.0

'test'
SPEC_DIR =

The directory which contains spec-tests for a project

Since:

  • 0.9.0

'spec'
PKG_DIR =

The directory which contains built packages

Since:

  • 0.9.0

'pkg'
IGNORE_NAMESPACES =

Words used in project names, but never in directory names

Since:

  • 0.9.0

%w[core ruby rb java]
COMMON_ABBREVIATIONS =

Common abbrevations used in namespaces

Since:

  • 0.9.0

Hash[File.readlines(File.join(Config::DATA_DIR,'abbreviations.txt')).map { |abbrev|
  abbrev.chomp!
  [abbrev.downcase, abbrev]
}]
COMMON_NAMESPACES =

Common project prefixes and namespaces

Since:

  • 0.9.0

YAML.load_file(File.join(Config::DATA_DIR,'common_namespaces.yml'))

Instance Method Summary collapse

Instance Method Details

#module_of(word) ⇒ String

Guesses the module name for a word within a project name.

Parameters:

  • word (String)

    The word within a project name.

Returns:

  • (String)

    The module name.

Since:

  • 0.1.1


74
75
76
77
78
79
80
81
82
# File 'lib/ore/naming.rb', line 74

def module_of(word)
  if COMMON_NAMESPACES.has_key?(word)
    COMMON_NAMESPACES[word]
  elsif COMMON_ABBREVIATIONS.has_key?(word)
    COMMON_ABBREVIATIONS[word]
  else
    word.capitalize
  end
end

#modules_of(name) ⇒ Array<String>

Guesses the module names from a project name.

Parameters:

  • name (String)

    The name of the project.

Returns:

  • (Array<String>)

    The module names for a project.

Since:

  • 0.9.0


93
94
95
96
97
# File 'lib/ore/naming.rb', line 93

def modules_of(name)
  names_in(name).map do |words|
    words.split('_').map { |word| module_of(word) }.join
  end
end

#names_in(name) ⇒ Array<String>

Splits the project name into individual names.

Parameters:

  • name (String)

    The name to split.

Returns:

  • (Array<String>)

    The individual names of the project name.

Since:

  • 0.9.0


57
58
59
60
61
# File 'lib/ore/naming.rb', line 57

def names_in(name)
  name.split('-').reject do |word|
    IGNORE_NAMESPACES.include?(word)
  end
end

#namespace_dirs_of(name) ⇒ Array<String>

Guesses the namespace directories within lib/ for a project.

Parameters:

  • name (String)

    The name of the project.

Returns:

  • (Array<String>)

    The namespace directories for the project.

Since:

  • 0.9.0


136
137
138
# File 'lib/ore/naming.rb', line 136

def namespace_dirs_of(name)
  names_in(name).map { |word| underscore(word) }
end

#namespace_of(name) ⇒ String

Guesses the full namespace for a project.

Parameters:

  • name (String)

    The name of the project.

Returns:

  • (String)

    The full module namespace for a project.

Since:

  • 0.9.0


108
109
110
# File 'lib/ore/naming.rb', line 108

def namespace_of(name)
  modules_of(name).join('::')
end

#namespace_path_of(name) ⇒ String

Guesses the namespace directory within lib/ for a project.

Parameters:

  • name (String)

    The name of the project.

Returns:

  • (String)

    The namespace directory for the project.

Since:

  • 0.9.0


149
150
151
# File 'lib/ore/naming.rb', line 149

def namespace_path_of(name)
  File.join(namespace_dirs_of(name))
end

#underscore(name) ⇒ String

Converts a camel-case name to an underscored file name.

Parameters:

  • name (String)

    The name to underscore.

Returns:

  • (String)

    The underscored version of the name.

Since:

  • 0.9.0


121
122
123
124
125
# File 'lib/ore/naming.rb', line 121

def underscore(name)
  name.gsub(/[^A-Z_][A-Z][^A-Z_]/) { |cap|
    cap[0,1] + '_' + cap[1..-1]
  }.downcase
end