Module: Ore::Naming

Included in:
Inferences, Paths, Project, Versions::VersionConstant
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).

Constant Summary collapse

@@bin_dir =

The directory which contains executables for a project

'bin'
@@lib_dir =

The directory which contains the code for a project

'lib'
@@ext_dir =

The directory which contains C extension code for a project

'ext'
@@data_dir =

The directory which contains data files for a project

'data'
@@test_dir =

The directory which contains unit-tests for a project

'test'
@@spec_dir =

The directory which contains spec-tests for a project

'spec'
@@pkg_dir =

The directory which contains built packages

'pkg'
@@ignore_namespaces =

Words used in project names, but never in directory names

%w[core ruby rb java]
@@namespace_acronyms =

Common acronyms used in namespaces

%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

{
  '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



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

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.



98
99
100
101
102
# File 'lib/ore/naming.rb', line 98

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.



62
63
64
65
66
# File 'lib/ore/naming.rb', line 62

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.



141
142
143
# File 'lib/ore/naming.rb', line 141

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.



113
114
115
# File 'lib/ore/naming.rb', line 113

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.



154
155
156
# File 'lib/ore/naming.rb', line 154

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.



126
127
128
129
130
# File 'lib/ore/naming.rb', line 126

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