Module: Rutter::Naming
- Defined in:
- lib/rutter/naming.rb
Overview
Conveniences for inflecting and working with names in Rutter.
Class Method Summary collapse
-
.classify(string) ⇒ String
Return a CamelCase version of the string.
-
.cleanpath(path) ⇒ String
Normalize the given path.
-
.join(*part, sep: "/") ⇒ String
Join the given arguments with the separator.
-
.route_name(string) ⇒ Symbol
Normalize the given string/symbol to a valid route name.
-
.underscore(string) ⇒ String
Return a downcased and underscore separated version of the string.
Class Method Details
.classify(string) ⇒ String
Return a CamelCase version of the string.
Revised version of Hanami::Utils::String.classify
implementation.
44 45 46 47 48 49 |
# File 'lib/rutter/naming.rb', line 44 def classify(string) words = underscore(string).split(%r{_|::|\/|\-}).map!(&:capitalize) delimiters = underscore(string).scan(%r{_|::|\/|\-}) delimiters.map! { |delimiter| delimiter == "_" ? "" : "::" } words.zip(delimiters).join end |
.cleanpath(path) ⇒ String
Normalize the given path.
73 74 75 |
# File 'lib/rutter/naming.rb', line 73 def cleanpath(path) Pathname.new("/#{path}").cleanpath.to_s end |
.join(part) ⇒ String .join(part) ⇒ String
Join the given arguments with the separator.
90 91 92 93 |
# File 'lib/rutter/naming.rb', line 90 def join(*part, sep: "/") part.reject { |s| s.nil? || s == "" } .join(sep) end |
.route_name(string) ⇒ Symbol
Normalize the given string/symbol to a valid route name.
58 59 60 61 62 63 64 |
# File 'lib/rutter/naming.rb', line 58 def route_name(string) string = underscore(string) string.tr!("/", "_") string.gsub!(/[_]{2,}/, "_") string.gsub!(/\A_|_\z/, "") string.to_sym end |
.underscore(string) ⇒ String
Return a downcased and underscore separated version of the string.
Revised version of Hanami::Utils::String.underscore
implementation.
21 22 23 24 25 26 27 28 29 |
# File 'lib/rutter/naming.rb', line 21 def underscore(string) string = +string.to_s string.gsub!("::", "/") string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') string.gsub!(/([a-z\d])([A-Z])/, '\1_\2') string.gsub!(/[[:space:]]|\-/, '\1_\2') string.downcase! string end |