module Dry::Core::Inflector
Helper module providing thin interface around an inflection backend.
Constants
- BACKENDS
List of supported backends
Public Class Methods
Transform string to camel case
@example
Dry::Core::Inflector.camelize('foo_bar') # => 'FooBar'
@param [String] input input string @return Transformed string
# File lib/dry/core/inflector.rb, line 70 def self.camelize(input) inflector.camelize(input) end
Transform a file path to a constant name
@example
Dry::Core::Inflector.classify('foo/bar') # => 'Foo::Bar'
@param [String] input input string @return Constant name
# File lib/dry/core/inflector.rb, line 136 def self.classify(input) inflector.classify(input) end
Get a constant value by its name
@example
Dry::Core::Inflector.constantize('Foo::Bar') # => Foo::Bar
@param [String] input input constant name @return Constant value
# File lib/dry/core/inflector.rb, line 125 def self.constantize(input) inflector.constantize(input) end
Remove namespaces from a constant name
@example
Dry::Core::Inflector.demodulize('Deeply::Nested::Name') # => 'Name'
@param [String] input input string @return Unnested constant name
# File lib/dry/core/inflector.rb, line 114 def self.demodulize(input) inflector.demodulize(input) end
Set up first available backend
@api private
# File lib/dry/core/inflector.rb, line 37 def self.detect_backend BACKENDS.inject(nil) do |backend, (_, (path, factory))| backend || realize_backend(path, factory) end || raise(LoadError, "No inflector library could be found: "\ "please install either the `inflecto` or `activesupport` gem.") end
Inflector
accessor. Lazily initializes a backend
@api private
# File lib/dry/core/inflector.rb, line 59 def self.inflector defined?(@inflector) ? @inflector : select_backend end
Get a plural form of a word
@example
Dry::Core::Inflector.pluralize('string') # => 'strings'
@param [String] input input string @return Transformed string
# File lib/dry/core/inflector.rb, line 103 def self.pluralize(input) inflector.pluralize(input) end
Try to activate a backend
@api private
# File lib/dry/core/inflector.rb, line 26 def self.realize_backend(path, backend_factory) require path rescue LoadError nil else backend_factory.call end
Set preferred backend
@param [Symbol] name backend name (:activesupport or :inflecto)
# File lib/dry/core/inflector.rb, line 48 def self.select_backend(name = nil) if name && !BACKENDS.key?(name) raise NameError, "Invalid inflector library selection: '#{name}'" end @inflector = name ? realize_backend(*BACKENDS[name]) : detect_backend end
Get a singlular form of a word
@example
Dry::Core::Inflector.singularize('chars') # => 'char'
@param [String] input input string @return Transformed string
# File lib/dry/core/inflector.rb, line 92 def self.singularize(input) inflector.singularize(input) end
Transform string to snake case
@example
Dry::Core::Inflector.underscore('FooBar') # => 'foo_bar'
@param [String] input input string @return Transformed string
# File lib/dry/core/inflector.rb, line 81 def self.underscore(input) inflector.underscore(input) end