Module: Dry::Core::Inflector

Defined in:
lib/dry/core/inflector.rb

Overview

Helper module providing thin interface around an inflection backend.

Constant Summary collapse

BACKENDS =

List of supported backends

{
  activesupport: [
    'active_support/inflector',
    proc { ::ActiveSupport::Inflector }
  ],
  dry_inflector: [
    'dry/inflector',
    proc { Dry::Inflector.new }
  ],
  inflecto: [
    'inflecto',
    proc { ::Inflecto }
  ]
}.freeze

Class Method Summary collapse

Class Method Details

.camelize(input) ⇒ Object

Transform string to camel case

Examples:

Dry::Core::Inflector.camelize('foo_bar') # => 'FooBar'

Parameters:

  • input (String)

    input string

Returns:

  • Transformed string



67
68
69
# File 'lib/dry/core/inflector.rb', line 67

def self.camelize(input)
  inflector.camelize(input)
end

.classify(input) ⇒ Object

Transform a file path to a constant name

Examples:

Dry::Core::Inflector.classify('foo/bar') # => 'Foo::Bar'

Parameters:

  • input (String)

    input string

Returns:

  • Constant name



133
134
135
# File 'lib/dry/core/inflector.rb', line 133

def self.classify(input)
  inflector.classify(input)
end

.constantize(input) ⇒ Object

Get a constant value by its name

Examples:

Dry::Core::Inflector.constantize('Foo::Bar') # => Foo::Bar

Parameters:

  • input (String)

    input constant name

Returns:

  • Constant value



122
123
124
# File 'lib/dry/core/inflector.rb', line 122

def self.constantize(input)
  inflector.constantize(input)
end

.demodulize(input) ⇒ Object

Remove namespaces from a constant name

Examples:

Dry::Core::Inflector.demodulize('Deeply::Nested::Name') # => 'Name'

Parameters:

  • input (String)

    input string

Returns:

  • Unnested constant name



111
112
113
# File 'lib/dry/core/inflector.rb', line 111

def self.demodulize(input)
  inflector.demodulize(input)
end

.detect_backendObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set up first available backend



35
36
37
38
39
40
41
# File 'lib/dry/core/inflector.rb', line 35

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

.inflectorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inflector accessor. Lazily initializes a backend



56
57
58
# File 'lib/dry/core/inflector.rb', line 56

def self.inflector
  defined?(@inflector) ? @inflector : select_backend
end

.pluralize(input) ⇒ Object

Get a plural form of a word

Examples:

Dry::Core::Inflector.pluralize('string') # => 'strings'

Parameters:

  • input (String)

    input string

Returns:

  • Transformed string



100
101
102
# File 'lib/dry/core/inflector.rb', line 100

def self.pluralize(input)
  inflector.pluralize(input)
end

.realize_backend(path, backend_factory) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Try to activate a backend



24
25
26
27
28
29
30
# File 'lib/dry/core/inflector.rb', line 24

def self.realize_backend(path, backend_factory)
  require path
rescue LoadError
  nil
else
  backend_factory.call
end

.select_backend(name = nil) ⇒ Object

Set preferred backend

Parameters:

  • name (Symbol) (defaults to: nil)

    backend name (:activesupport or :inflecto)



46
47
48
49
50
51
# File 'lib/dry/core/inflector.rb', line 46

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) ⇒ Object

Get a singlular form of a word

Examples:

Dry::Core::Inflector.singularize('chars') # => 'char'

Parameters:

  • input (String)

    input string

Returns:

  • Transformed string



89
90
91
# File 'lib/dry/core/inflector.rb', line 89

def self.singularize(input)
  inflector.singularize(input)
end

.underscore(input) ⇒ Object

Transform string to snake case

Examples:

Dry::Core::Inflector.underscore('FooBar') # => 'foo_bar'

Parameters:

  • input (String)

    input string

Returns:

  • Transformed string



78
79
80
# File 'lib/dry/core/inflector.rb', line 78

def self.underscore(input)
  inflector.underscore(input)
end