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
-
.camelize(input) ⇒ Object
Transform string to camel case.
-
.classify(input) ⇒ Object
Transform a file path to a constant name.
-
.constantize(input) ⇒ Object
Get a constant value by its name.
-
.demodulize(input) ⇒ Object
Remove namespaces from a constant name.
-
.detect_backend ⇒ Object
private
Set up first available backend.
-
.inflector ⇒ Object
private
Inflector accessor.
-
.pluralize(input) ⇒ Object
Get a plural form of a word.
-
.realize_backend(path, backend_factory) ⇒ Object
private
Try to activate a backend.
-
.select_backend(name = nil) ⇒ Object
Set preferred backend.
-
.singularize(input) ⇒ Object
Get a singlular form of a word.
-
.underscore(input) ⇒ Object
Transform string to snake case.
Class Method Details
.camelize(input) ⇒ Object
Transform string to camel case
72 73 74 |
# File 'lib/dry/core/inflector.rb', line 72 def self.camelize(input) inflector.camelize(input) end |
.classify(input) ⇒ Object
Transform a file path to a constant name
138 139 140 |
# File 'lib/dry/core/inflector.rb', line 138 def self.classify(input) inflector.classify(input) end |
.constantize(input) ⇒ Object
Get a constant value by its name
127 128 129 |
# File 'lib/dry/core/inflector.rb', line 127 def self.constantize(input) inflector.constantize(input) end |
.demodulize(input) ⇒ Object
Remove namespaces from a constant name
116 117 118 |
# File 'lib/dry/core/inflector.rb', line 116 def self.demodulize(input) inflector.demodulize(input) end |
.detect_backend ⇒ 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.
Set up first available backend
37 38 39 40 41 42 43 44 45 |
# 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 ⇒ 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.
Inflector accessor. Lazily initializes a backend
61 62 63 |
# File 'lib/dry/core/inflector.rb', line 61 def self.inflector defined?(@inflector) ? @inflector : select_backend end |
.pluralize(input) ⇒ Object
Get a plural form of a word
105 106 107 |
# File 'lib/dry/core/inflector.rb', line 105 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
26 27 28 29 30 31 32 |
# 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) ⇒ Object
Set preferred backend
50 51 52 53 54 55 56 |
# File 'lib/dry/core/inflector.rb', line 50 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
94 95 96 |
# File 'lib/dry/core/inflector.rb', line 94 def self.singularize(input) inflector.singularize(input) end |
.underscore(input) ⇒ Object
Transform string to snake case
83 84 85 |
# File 'lib/dry/core/inflector.rb', line 83 def self.underscore(input) inflector.underscore(input) end |