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).
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
-
#module_of(word) ⇒ String
Guesses the module name for a word within a project name.
-
#modules_of(name) ⇒ Array<String>
Guesses the module names from a project name.
-
#names_in(name) ⇒ Array<String>
Splits the project name into individual names.
-
#namespace_dirs_of(name) ⇒ Array<String>
Guesses the namespace directories within
lib/
for a project. -
#namespace_of(name) ⇒ String
Guesses the full namespace for a project.
-
#namespace_path_of(name) ⇒ String
Guesses the namespace directory within
lib/
for a project. -
#underscore(name) ⇒ String
Converts a camel-case name to an underscored file name.
Instance Method Details
#module_of(word) ⇒ String
Guesses the module name for a word within a project name.
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.
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.
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.
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.
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.
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.
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 |