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]
- COMMON_ABBREVIATIONS =
Common abbrevations used in namespaces
Hash[File.readlines(File.join(Config::DATA_DIR,'abbreviations.txt')).map { |abbrev| abbrev.chomp! [abbrev.downcase, abbrev]
- COMMON_NAMESPACES =
Common project prefixes and namespaces
YAML.load_file(File.join(Config::DATA_DIR,'common_namespaces.yml'))
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.
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.
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.
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.
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.
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.
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.
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 |