Module: Normatron::Filters::CamelizeFilter
- Extended by:
- Helpers
- Defined in:
- lib/normatron/filters/camelize_filter.rb
Overview
Converts strings to UpperCamelCase by default and to lowerCamelCase if the :lower
argument is given.
It will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold:
"SSLError".underscore.camelize # => "SslError"
This filter has a similar behavior to ActiveSupport::Inflector#camelize, but it affects UTF-8 characters too.
Class Method Summary collapse
-
.call(input, camel = :upper) ⇒ String
Performs input conversion according to filter requirements.
Methods included from Helpers
acronym_regex, acronyms, evaluate_regexp, inflections, mb_send
Class Method Details
.call(input, camel = :upper) ⇒ String
Performs input conversion according to filter requirements.
This method returns the object itself when the first argument is not a String.
46 47 48 49 50 51 52 53 |
# File 'lib/normatron/filters/camelize_filter.rb', line 46 def self.call(input, camel = :upper) return input unless input.kind_of?(String) string = mb_send(:downcase, input) string.sub!(/^[^_|\/]+/) { camel == :upper ? acronyms[$&] || mb_send(:capitalize, $&) : $& } string.gsub!(/(?:(\/)|_)([^\/|_]+)/) { "#{$1}#{acronyms[$2] || mb_send(:capitalize, $2)}" } string.gsub("/", "::") end |