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]
NAMESPACE_ACRONYMS =

Common acronyms used in namespaces

Since:

  • 0.9.0

%w[
  ffi yard i18n
  http https ftp smtp imap pop3 ssh ssl tcp udp dns rpc
  url uri www css html xhtml xml xsl json yaml csv
  posix unix bsd
  cpp asm
]
COMMON_NAMESPACES =

Common project prefixes and namespaces

Since:

  • 0.9.0

{
  'rubygems' => 'Gem',
  'ar'       => 'ActiveRecord',
  'dm'       => 'DataMapper',
  'js'       => 'JavaScript',
  'msgpack'  => 'MsgPack',
  'github'   => 'GitHub',
  'rdoc'     => 'RDoc'
}

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



81
82
83
84
85
86
87
88
89
# File 'lib/ore/naming.rb', line 81

def module_of(word)
  if COMMON_NAMESPACES.has_key?(word)
    COMMON_NAMESPACES[word]
  elsif NAMESPACE_ACRONYMS.include?(word)
    word.upcase
  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



100
101
102
103
104
# File 'lib/ore/naming.rb', line 100

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



64
65
66
67
68
# File 'lib/ore/naming.rb', line 64

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



143
144
145
# File 'lib/ore/naming.rb', line 143

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



115
116
117
# File 'lib/ore/naming.rb', line 115

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



156
157
158
# File 'lib/ore/naming.rb', line 156

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



128
129
130
131
132
# File 'lib/ore/naming.rb', line 128

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