module Dry::Core::Inflector

Helper module providing thin interface around an inflection backend.

Constants

BACKENDS

List of supported backends

Public Class Methods

camelize(input) click to toggle source

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
classify(input) click to toggle source

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
constantize(input) click to toggle source

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
demodulize(input) click to toggle source

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
detect_backend() click to toggle source

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() click to toggle source

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
pluralize(input) click to toggle source

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
realize_backend(path, backend_factory) click to toggle source

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
select_backend(name = nil) click to toggle source

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
singularize(input) click to toggle source

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
underscore(input) click to toggle source

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