erratum 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/erratum/configuration.rb +21 -4
  4. data/lib/erratum/error.rb +4 -3
  5. data/lib/erratum/errors/{authentication_errors/duplicate_authentication_error.rb → authentication/duplicate_authentication.rb} +3 -3
  6. data/lib/erratum/errors/{authentication_errors/invalid_token_error.rb → authentication/invalid_token.rb} +3 -3
  7. data/lib/erratum/errors/{authentication_errors/invalid_username_or_password_error.rb → authentication/invalid_username_or_password.rb} +3 -3
  8. data/lib/erratum/errors/{authorization_error.rb → authentication.rb} +1 -1
  9. data/lib/erratum/errors/{authorization_errors/forbidden_error.rb → authorization/forbidden.rb} +3 -3
  10. data/lib/erratum/errors/{authentication_error.rb → authorization.rb} +1 -1
  11. data/lib/erratum/errors/{crud_errors/association_error.rb → crud/invalid_association.rb} +3 -3
  12. data/lib/erratum/errors/{crud_errors/resource_not_found_error.rb → crud/resource_not_found.rb} +3 -3
  13. data/lib/erratum/errors/{crud_errors/resource_persistence_error.rb → crud/resource_persistence.rb} +3 -3
  14. data/lib/erratum/errors/{crud_error.rb → crud.rb} +1 -1
  15. data/lib/erratum/errors/{request_errors/parameter_missing_error.rb → request/parameter_missing.rb} +3 -3
  16. data/lib/erratum/errors/{request_errors/unpermitted_parameters_error.rb → request/unpermitted_parameters.rb} +3 -3
  17. data/lib/erratum/errors/runtime_error.rb +11 -0
  18. data/lib/erratum/rescuable_resource.rb +2 -2
  19. data/lib/erratum/verifiable_resource.rb +2 -2
  20. data/lib/erratum/version.rb +1 -1
  21. data/lib/erratum.rb +81 -31
  22. data.tar.gz.sig +0 -0
  23. metadata +30 -17
  24. metadata.gz.sig +0 -0
  25. data/lib/erratum/resource_naming.rb +0 -42
  26. data/lib/erratum/utilities/string.rb +0 -147
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcdf88a3d21177e27a3d39502aef4a053a0df0a4
4
- data.tar.gz: fe5e579cc5677ceaa659a4153be16bd933de5b52
3
+ metadata.gz: a64ec04a4883f093019192fde51fd407c734c7d7
4
+ data.tar.gz: f747c1132538479444c7d4d674d777d35b144d6d
5
5
  SHA512:
6
- metadata.gz: 993a890335e84d5277add854ca7cd60824e1a828116de2970cf8b239eb5e31b6b07c90526bb92cf6907a538ad1867ae3d2c81584effade2296ad1094694224c4
7
- data.tar.gz: 35c74a023b357c73d21f7930f931a8592e7b6aa4ccfb4f65883350866ae995b489f2e20a1ec09b40f1ae1531e57b4ccc959f9ff568414eec8747e2ca166ec8ac
6
+ metadata.gz: c2e4c60ab35531a59e889d2b3648c0b6c2b8e28080d252c47d2aee34d403baa98474d56e8ed75200a5b3fbb4154479f82f3405b726c3ca1eba252f137eac9de3
7
+ data.tar.gz: f8c9aa8f46c7fc4307d5d3821c7eaa84489918b9f10cb42269f0997716474160ef3330ab0ce0386dd9b8326d7f38300c5faace01f5af7ebcbc82a76df6298c78
checksums.yaml.gz.sig CHANGED
Binary file
@@ -6,16 +6,33 @@ module Erratum
6
6
  class Configuration
7
7
  include Singleton
8
8
 
9
- attr_writer :url_mappings
9
+ DEFAULT_ERROR_MAPPINGS = {
10
+ 'ActionController::ParameterMissing' => '::Erratum::Errors::ParameterMissing',
11
+ 'ActionController::UnpermittedParameters' => '::Erratum::Errors::UnpermittedParameters',
12
+ 'ActiveRecord::InvalidForeignKey' => '::Erratum::Errors::InvalidAssociation',
13
+ 'ActiveRecord::RecordInvalid' => '::Erratum::Errors::ResourcePersistence',
14
+ 'ActiveRecord::RecordNotFound' => '::Erratum::Errors::ResourceNotFound',
15
+ 'ActiveRecord::RecordNotSaved' => '::Erratum::Errors::ResourcePersistence',
16
+ }.freeze
10
17
 
11
- def external_documentation_urls
12
- url_mappings['external_documentation_urls']
13
- end
18
+ attr_writer :url_mappings
14
19
 
15
20
  def developer_documentation_urls
16
21
  url_mappings['developer_documentation_urls']
17
22
  end
18
23
 
24
+ def error_mappings
25
+ @error_mappings ||= DEFAULT_ERROR_MAPPINGS.dup
26
+ end
27
+
28
+ def error_mappings=(other)
29
+ @error_mappings = DEFAULT_ERROR_MAPPINGS.merge(other)
30
+ end
31
+
32
+ def external_documentation_urls
33
+ url_mappings['external_documentation_urls']
34
+ end
35
+
19
36
  def to_h
20
37
  {
21
38
  'external_documentation_urls' => external_documentation_urls,
data/lib/erratum/error.rb CHANGED
@@ -2,10 +2,11 @@
2
2
 
3
3
  require 'json'
4
4
  require 'erratum/configurable'
5
- require 'erratum/utilities/string'
5
+ require 'apple_core/refinements/string'
6
6
 
7
7
  module Erratum
8
8
  module Error
9
+ using ::AppleCore::Refinements::String
9
10
  include Configurable
10
11
 
11
12
  module ClassMethods
@@ -70,8 +71,8 @@ module Error
70
71
  alias status http_status
71
72
 
72
73
  def code
73
- @code ||= Erratum::Utilities::String
74
- .underscore(self.class.name)
74
+ @code ||= self.class.name
75
+ .underscore
75
76
  .gsub(%r{\A[^/]+/}, '')
76
77
  .gsub(%r{/}, '.')
77
78
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/authentication_error'
4
+ require 'erratum/errors/authentication'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class DuplicateAuthenticationError < RuntimeError
8
+ class DuplicateAuthentication < RuntimeError
9
9
  include Error
10
- include AuthenticationError
10
+ include Errors::Authentication
11
11
 
12
12
  attr_accessor :provider,
13
13
  :provider_user_id,
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/authentication_error'
4
+ require 'erratum/errors/authentication'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class InvalidTokenError < RuntimeError
8
+ class InvalidToken < RuntimeError
9
9
  include Error
10
- include AuthenticationError
10
+ include Errors::Authentication
11
11
 
12
12
  attr_accessor :authentication_token
13
13
 
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/authentication_error'
4
+ require 'erratum/errors/authentication'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class InvalidUsernameOrPasswordError < RuntimeError
8
+ class InvalidUsernameOrPassword < RuntimeError
9
9
  include Error
10
- include AuthenticationError
10
+ include Errors::Authentication
11
11
 
12
12
  attr_accessor :username
13
13
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Erratum
4
4
  module Errors
5
- module AuthorizationError
5
+ module Authentication
6
6
  end
7
7
  end
8
8
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/authorization_error'
4
+ require 'erratum/errors/authorization'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class ForbiddenError < RuntimeError
8
+ class Forbidden < RuntimeError
9
9
  include Error
10
- include AuthorizationError
10
+ include Errors::Authorization
11
11
 
12
12
  NON_SPECIFIC_RESOURCE_ACTIONS = %w{index create}.freeze
13
13
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Erratum
4
4
  module Errors
5
- module AuthenticationError
5
+ module Authorization
6
6
  end
7
7
  end
8
8
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/crud_error'
4
+ require 'erratum/errors/crud'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class AssociationError < RuntimeError
8
+ class InvalidAssociation < RuntimeError
9
9
  include Error
10
- include CrudError
10
+ include Errors::Crud
11
11
 
12
12
  attr_accessor :association_name,
13
13
  :association_id,
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/crud_error'
4
+ require 'erratum/errors/crud'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class ResourceNotFoundError < RuntimeError
8
+ class ResourceNotFound < RuntimeError
9
9
  include Error
10
- include CrudError
10
+ include Errors::Crud
11
11
 
12
12
  def self.convert(original_error, overrides = {})
13
13
  initialization_parameters = {}
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/crud_error'
4
+ require 'erratum/errors/crud'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class ResourcePersistenceError < RuntimeError
8
+ class ResourcePersistence < RuntimeError
9
9
  include Error
10
- include CrudError
10
+ include Errors::Crud
11
11
 
12
12
  attr_accessor :pointer,
13
13
  :attribute,
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Erratum
4
4
  module Errors
5
- module CrudError
5
+ module Crud
6
6
  attr_accessor :action,
7
7
  :resource_id,
8
8
  :resource_name
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/crud_error'
4
+ require 'erratum/errors/crud'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class ParameterMissingError < RuntimeError
8
+ class ParameterMissing < RuntimeError
9
9
  include Error
10
- include CrudError
10
+ include Errors::Crud
11
11
 
12
12
  attr_accessor :parameter
13
13
 
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erratum/error'
4
- require 'erratum/errors/crud_error'
4
+ require 'erratum/errors/crud'
5
5
 
6
6
  module Erratum
7
7
  module Errors
8
- class UnpermittedParametersError < RuntimeError
8
+ class UnpermittedParameters < RuntimeError
9
9
  include Error
10
- include CrudError
10
+ include Errors::Crud
11
11
 
12
12
  attr_accessor :parameters
13
13
 
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erratum/error'
4
+
5
+ module Erratum
6
+ module Errors
7
+ class RuntimeError < RuntimeError
8
+ include Error
9
+ end
10
+ end
11
+ end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'erratum/resource_naming'
3
+ require 'apple_core/action_controller/resource_naming'
4
4
 
5
5
  module Erratum
6
6
  module RescuableResource
7
7
  def self.included(base)
8
- base.include ResourceNaming
8
+ base.include ::AppleCore::ActionController::ResourceNaming
9
9
 
10
10
  base.rescue_from 'ActiveRecord::RecordInvalid',
11
11
  'ActiveRecord::RecordNotSaved',
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'erratum/resource_naming'
3
+ require 'apple_core/action_controller/resource_naming'
4
4
 
5
5
  module Erratum
6
6
  module VerifiableResource
7
7
  def self.included(base)
8
- base.include ResourceNaming
8
+ base.include ::AppleCore::ActionController::ResourceNaming
9
9
 
10
10
  base.before_action except: %i{index create} do
11
11
  model = public_send(self.class.singular_resource_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Erratum
4
- VERSION = '3.0.0'
4
+ VERSION = '3.1.0'
5
5
  end
data/lib/erratum.rb CHANGED
@@ -1,49 +1,99 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'apple_core/refinements/hash'
4
+
3
5
  require 'erratum/configuration'
4
6
  require 'erratum/error'
5
- require 'erratum/errors/authentication_error'
6
- require 'erratum/errors/authentication_errors/duplicate_authentication_error'
7
- require 'erratum/errors/authentication_errors/invalid_token_error'
8
- require 'erratum/errors/authentication_errors/invalid_username_or_password_error'
9
- require 'erratum/errors/authorization_error'
10
- require 'erratum/errors/authorization_errors/forbidden_error'
11
- require 'erratum/errors/crud_error'
12
- require 'erratum/errors/crud_errors/association_error'
13
- require 'erratum/errors/crud_errors/resource_not_found_error'
14
- require 'erratum/errors/crud_errors/resource_persistence_error'
15
- require 'erratum/errors/request_errors/parameter_missing_error'
16
- require 'erratum/errors/request_errors/unpermitted_parameters_error'
7
+ require 'erratum/errors/authentication'
8
+ require 'erratum/errors/authentication/duplicate_authentication'
9
+ require 'erratum/errors/authentication/invalid_token'
10
+ require 'erratum/errors/authentication/invalid_username_or_password'
11
+ require 'erratum/errors/authorization'
12
+ require 'erratum/errors/authorization/forbidden'
13
+ require 'erratum/errors/crud'
14
+ require 'erratum/errors/crud/invalid_association'
15
+ require 'erratum/errors/crud/resource_not_found'
16
+ require 'erratum/errors/crud/resource_persistence'
17
+ require 'erratum/errors/request/parameter_missing'
18
+ require 'erratum/errors/request/unpermitted_parameters'
19
+ require 'erratum/errors/runtime_error'
17
20
  require 'erratum/rescuable_resource'
18
21
  require 'erratum/verifiable_resource'
19
22
  require 'erratum/version'
20
23
 
21
24
  module Erratum
22
- def self.fetch(error_type)
23
- Object.const_get("Erratum::Errors::#{error_type}")
24
- end
25
+ using ::AppleCore::Refinements::Hash
25
26
 
26
- def self.build(error_type, overrides = {})
27
- fetch(error_type).new(overrides)
28
- end
27
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
28
+ def self.build(original_error, overrides = {})
29
+ overrides = overrides.symbolize_keys
30
+ original_error_name = error_name(original_error)
31
+ build_error_name = mapped_error_name(original_error_name)
32
+ build_error_class = error_class(build_error_name)
33
+
34
+ if original_error.is_a?(Class) &&
35
+ original_error < ::Erratum::Error
36
+
37
+ original_error.new(overrides)
38
+ elsif original_error.is_a?(String)
39
+ build_error_class.new(overrides)
40
+ elsif original_error.is_a?(::Erratum::Error)
41
+ original_source = original_error.source.symbolize_keys
29
42
 
30
- def self.convert(original_error, overrides = {})
31
- case original_error.class.name
32
- when 'ActiveRecord::InvalidForeignKey'
33
- fetch('AssociationError').convert(original_error, overrides)
34
- when 'ActiveRecord::RecordNotFound'
35
- fetch('ResourceNotFoundError').convert(original_error, overrides)
36
- when 'ActiveRecord::RecordInvalid',
37
- 'ActiveRecord::RecordNotSaved'
38
- fetch('ResourcePersistenceError').convert(original_error, overrides)
39
- when 'ActionController::ParameterMissing'
40
- fetch('ParameterMissingError').convert(original_error, overrides)
41
- when 'ActionController::UnpermittedParameters'
42
- fetch('UnpermittedParametersError').convert(original_error, overrides)
43
+ build_error_class.new(original_source.merge(overrides))
44
+ elsif build_error_class.respond_to?(:convert)
45
+ build_error_class.convert(original_error, overrides)
46
+ elsif build_error_class < Exception &&
47
+ build_error_class == original_error.class
48
+ ::Erratum::Errors::RuntimeError.wrap(original_error)
49
+ else
50
+ ::Erratum::Errors::RuntimeError.new(
51
+ message: "#{build_error_class.name}: #{original_error.inspect}",
52
+ )
43
53
  end
44
54
  end
55
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
45
56
 
46
57
  def self.raise(error_type, **args)
47
58
  Kernel.fail build(error_type, **args)
48
59
  end
60
+
61
+ def self.fail(error_type, **args)
62
+ Kernel.fail build(error_type, **args)
63
+ end
64
+
65
+ alias fail raise
66
+
67
+ def self.error_name(error)
68
+ if error.is_a?(String)
69
+ error.gsub(/\A::/, '')
70
+ elsif error.is_a? Class
71
+ error.name
72
+ else
73
+ error.class.name
74
+ end
75
+ end
76
+
77
+ def self.mapped_error_name(name)
78
+ configuration
79
+ .error_mappings
80
+ .fetch(name, name)
81
+ .gsub(/\A::/, '')
82
+ end
83
+
84
+ def self.error_class(name)
85
+ if const_defined?("::#{name}")
86
+ Object.const_get(name)
87
+ else
88
+ Object.const_get("Erratum::Errors::#{name}")
89
+ end
90
+ rescue NameError => error
91
+ Kernel.raise(
92
+ error.exception(<<~HEREDOC.chomp.tr("\n", ' ')),
93
+ Erratum could not find an error class with either the name "#{name}" or
94
+ with the name "Erratum::Errors::#{name}". Please verify the spelling and
95
+ that the class is loaded.
96
+ HEREDOC
97
+ )
98
+ end
49
99
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erratum
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thegranddesign
@@ -31,8 +31,22 @@ cert_chain:
31
31
  Y2GAoHKstmfIVhc4XHOPpmTd2o/C29O9oaRgjrkfQEhF/KvJ/PhoV5hvokzsCyI5
32
32
  iUeXPfvrGD/itYIBCgk+fnzyQQ4QtE5hTQaWQ3o2
33
33
  -----END CERTIFICATE-----
34
- date: 2018-04-24 00:00:00.000000000 Z
34
+ date: 2018-04-27 00:00:00.000000000 Z
35
35
  dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: apple_core
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.0'
36
50
  - !ruby/object:Gem::Dependency
37
51
  name: rspec
38
52
  requirement: !ruby/object:Gem::Requirement
@@ -114,24 +128,23 @@ files:
114
128
  - lib/erratum/configurable.rb
115
129
  - lib/erratum/configuration.rb
116
130
  - lib/erratum/error.rb
117
- - lib/erratum/errors/authentication_error.rb
118
- - lib/erratum/errors/authentication_errors/duplicate_authentication_error.rb
119
- - lib/erratum/errors/authentication_errors/invalid_token_error.rb
120
- - lib/erratum/errors/authentication_errors/invalid_username_or_password_error.rb
121
- - lib/erratum/errors/authorization_error.rb
122
- - lib/erratum/errors/authorization_errors/forbidden_error.rb
123
- - lib/erratum/errors/crud_error.rb
124
- - lib/erratum/errors/crud_errors/association_error.rb
125
- - lib/erratum/errors/crud_errors/resource_not_found_error.rb
126
- - lib/erratum/errors/crud_errors/resource_persistence_error.rb
127
- - lib/erratum/errors/request_errors/parameter_missing_error.rb
128
- - lib/erratum/errors/request_errors/unpermitted_parameters_error.rb
131
+ - lib/erratum/errors/authentication.rb
132
+ - lib/erratum/errors/authentication/duplicate_authentication.rb
133
+ - lib/erratum/errors/authentication/invalid_token.rb
134
+ - lib/erratum/errors/authentication/invalid_username_or_password.rb
135
+ - lib/erratum/errors/authorization.rb
136
+ - lib/erratum/errors/authorization/forbidden.rb
137
+ - lib/erratum/errors/crud.rb
138
+ - lib/erratum/errors/crud/invalid_association.rb
139
+ - lib/erratum/errors/crud/resource_not_found.rb
140
+ - lib/erratum/errors/crud/resource_persistence.rb
141
+ - lib/erratum/errors/request/parameter_missing.rb
142
+ - lib/erratum/errors/request/unpermitted_parameters.rb
143
+ - lib/erratum/errors/runtime_error.rb
129
144
  - lib/erratum/rescuable_resource.rb
130
- - lib/erratum/resource_naming.rb
131
- - lib/erratum/utilities/string.rb
132
145
  - lib/erratum/verifiable_resource.rb
133
146
  - lib/erratum/version.rb
134
- homepage: https://github.com/thekompanee/erratum
147
+ homepage: https://example.com
135
148
  licenses:
136
149
  - MIT
137
150
  metadata:
metadata.gz.sig CHANGED
Binary file
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'erratum/utilities/string'
4
-
5
- module Erratum
6
- module ResourceNaming
7
- CONTROLLER_RESOURCE_NAME_PATTERN = /\A((.*?::)?.*?)(\w+)Controller\z/
8
-
9
- def plural_underscored_base_resource_name
10
- @plural_underscored_base_resource_name ||= \
11
- ::Erratum::Utilities::String.pluralize(
12
- ::Erratum::Utilities::String.underscore(
13
- plural_resource_base_class_name,
14
- ),
15
- )
16
- .downcase
17
- end
18
-
19
- def singular_underscored_base_resource_name
20
- @singular_underscored_base_resource_name ||= \
21
- ::Erratum::Utilities::String.singularize(
22
- ::Erratum::Utilities::String.underscore(
23
- plural_resource_base_class_name,
24
- ),
25
- )
26
- .downcase
27
- end
28
-
29
- def singular_resource_base_class_name
30
- @singular_resource_base_class_name ||= \
31
- ::Erratum::Utilities::String
32
- .singularize(plural_resource_base_class_name)
33
- end
34
-
35
- def plural_resource_base_class_name
36
- @plural_resource_base_class_name ||= \
37
- ::Erratum::Utilities::String.pluralize(
38
- name[CONTROLLER_RESOURCE_NAME_PATTERN, 3],
39
- )
40
- end
41
- end
42
- end
@@ -1,147 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Erratum
4
- module Utilities
5
- class String
6
- # rubocop:disable Style/PerlBackrefs
7
- def self.camelize(other, uppercase_first_letter = true)
8
- return unless other
9
- return other.camelize if defined?(ActiveSupport::Inflector)
10
-
11
- string = other.to_s
12
-
13
- string = if uppercase_first_letter
14
- string.sub(/^[a-z\d]*/, &:capitalize)
15
- else
16
- string.downcase
17
- end
18
-
19
- string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) do
20
- "#{$1}#{acronyms[$2] || $2.capitalize}"
21
- end
22
-
23
- string.gsub!('/', '::')
24
-
25
- string
26
- end
27
- # rubocop:enable Style/PerlBackrefs
28
-
29
- # rubocop:disable Metrics/MethodLength
30
- def self.pluralize(other)
31
- return unless other
32
- return other.pluralize if defined?(ActiveSupport::Inflector)
33
-
34
- result = other.to_s.dup
35
-
36
- inflections = {
37
- 'person' => 'people',
38
- 'man' => 'men',
39
- 'child' => 'children',
40
- 'sex' => 'sexes',
41
- 'move' => 'moves',
42
- 'zombie' => 'zombies',
43
- /(quiz)$/i => '\1zes',
44
- /^(oxen)$/i => '\1',
45
- /^(ox)$/i => '\1en',
46
- /^(m|l)ice$/i => '\1ice',
47
- /^(m|l)ouse$/i => '\1ice',
48
- /(matr|vert|ind)(?:ix|ex)$/i => '\1ices',
49
- /(x|ch|ss|sh)$/i => '\1es',
50
- /([^aeiouy]|qu)y$/i => '\1ies',
51
- /(hive)$/i => '\1s',
52
- /(?:([^f])fe|([lr])f)$/i => '\1\2ves',
53
- /sis$/i => 'ses',
54
- /([ti])a$/i => '\1a',
55
- /([ti])um$/i => '\1a',
56
- /(buffal|tomat)o$/i => '\1oes',
57
- /(bu)s$/i => '\1ses',
58
- /(alias|status)$/i => '\1es',
59
- /(octop|vir)i$/i => '\1i',
60
- /(octop|vir)us$/i => '\1i',
61
- /^(ax|test)is$/i => '\1es',
62
- /s$/i => 's',
63
- /$/ => 's',
64
- }
65
-
66
- inflections.each do |(rule, replacement)|
67
- break if result.sub!(rule, replacement)
68
- end
69
-
70
- result
71
- end
72
- # rubocop:enable Metrics/MethodLength
73
-
74
- # rubocop:disable Metrics/MethodLength, Metrics/AlignHash
75
- def self.singularize(other)
76
- return unless other
77
- return other.singularize if defined?(ActiveSupport::Inflector)
78
-
79
- result = other.to_s.dup
80
-
81
- inflections = {
82
- /(database)s$/i => '\1',
83
- /(quiz)zes$/i => '\1',
84
- /(matr)ices$/i => '\1ix',
85
- /(vert|ind)ices$/i => '\1ex',
86
- /^(ox)en/i => '\1',
87
- /(alias|status)(es)?$/i => '\1',
88
- /(octop|vir)(us|i)$/i => '\1us',
89
- /^(a)x[ie]s$/i => '\1xis',
90
- /(cris|test)(is|es)$/i => '\1is',
91
- /(shoe)s$/i => '\1',
92
- /(o)es$/i => '\1',
93
- /(bus)(es)?$/i => '\1',
94
- /^(m|l)ice$/i => '\1ouse',
95
- /(x|ch|ss|sh)es$/i => '\1',
96
- /(m)ovies$/i => '\1ovie',
97
- /(s)eries$/i => '\1eries',
98
- /([^aeiouy]|qu)ies$/i => '\1y',
99
- /([lr])ves$/i => '\1f',
100
- /(tive)s$/i => '\1',
101
- /(hive)s$/i => '\1',
102
- /([^f])ves$/i => '\1fe',
103
- /(^analy)(sis|ses)$/i => '\1sis',
104
- /
105
- (
106
- (a)naly |
107
- (b)a |
108
- (d)iagno |
109
- (p)arenthe |
110
- (p)rogno |
111
- (s)ynop |
112
- (t)he
113
- )
114
- (sis|ses)
115
- $
116
- /xi => '\1sis',
117
- /([ti])a$/i => '\1um',
118
- /(n)ews$/i => '\1ews',
119
- /(ss)$/i => '\1',
120
- /s$/i => '',
121
- }
122
-
123
- inflections.each do |(rule, replacement)|
124
- break if result.sub!(rule, replacement)
125
- end
126
-
127
- result
128
- end
129
- # rubocop:enable Metrics/MethodLength, Metrics/AlignHash
130
-
131
- def self.underscore(other)
132
- return unless other
133
- return other.underscore if defined?(ActiveSupport::Inflector)
134
-
135
- word = other.to_s.gsub('::', '/')
136
- word.gsub!(/(?:([A-Za-z\d])|^)(?=\b|[^a-z])/) do
137
- "#{Regexp.last_match(1)}#{Regexp.last_match(1) && ''}"
138
- end
139
- word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
140
- word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
141
- word.tr!('-', '_')
142
- word.downcase!
143
- word
144
- end
145
- end
146
- end
147
- end