dry-inflector 0.1.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +54 -6
- data/LICENSE +20 -0
- data/README.md +19 -98
- data/dry-inflector.gemspec +20 -16
- data/lib/dry/inflector/acronyms.rb +1 -1
- data/lib/dry/inflector/inflections/defaults.rb +17 -13
- data/lib/dry/inflector/inflections.rb +18 -7
- data/lib/dry/inflector/version.rb +1 -1
- data/lib/dry/inflector.rb +62 -28
- data/lib/dry-inflector.rb +3 -0
- metadata +24 -35
- data/LICENSE.md +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74866c6fc9734bbf35266473131ad1b0cdf08a16a541f1b33d9c51d4787debe4
|
4
|
+
data.tar.gz: 18f73495b51877a6bfe2759df34f5a968f649b19200a7ed8981dc81eec3ea89f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e66e59ef5fa5b4ef6a12f108e20a9674e305ad808a850e4bde85b97f06b13b2f4b1c02763c3ccf0170afdcd30bb9b8c78f4413dfb0b40552971bf8b3ccf8d0e6
|
7
|
+
data.tar.gz: 8154dc57bbd78a19b35fe43bc611d94309d982583d2fdbe5265ef63e8e1ea698050e19790bf8124c959949ec3e4a070467c218a64d793f5c1e2dbd4db4751a3a
|
data/CHANGELOG.md
CHANGED
@@ -1,23 +1,71 @@
|
|
1
|
-
|
1
|
+
<!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->
|
2
2
|
|
3
|
-
|
3
|
+
## 0.3.0 2022-07-12
|
4
|
+
|
5
|
+
|
6
|
+
### Added
|
7
|
+
|
8
|
+
- Add CSV as default acronym (via #43) (@waiting-for-dev)
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- Extra dashes are now omitted when converting to camelcase (via #40) (@postmodern)
|
13
|
+
|
14
|
+
[Compare v0.2.1...v0.3.0](https://github.com/dry-rb/dry-inflector/compare/v0.2.1...v0.3.0)
|
15
|
+
|
16
|
+
## 0.2.1 2021-06-30
|
17
|
+
|
18
|
+
|
19
|
+
### Added
|
20
|
+
|
21
|
+
- Add default acronyms: API and CSRF (#35) (@jodosha)
|
22
|
+
|
23
|
+
### Fixed
|
24
|
+
|
25
|
+
- Fix singularizing -us suffix (issue #33 via #38) (@cllns)
|
26
|
+
|
27
|
+
|
28
|
+
[Compare v0.2.0...v0.2.1](https://github.com/dry-rb/dry-inflector/compare/v0.2.0...v0.2.1)
|
29
|
+
|
30
|
+
## 0.2.0 2019-10-13
|
31
|
+
|
32
|
+
|
33
|
+
### Added
|
34
|
+
|
35
|
+
- [Abinoam P. Marques Jr. & Andrii Savchenko] Introduced `Dry::Inflector#camelize_upper` and `Dry::Inflector#camelize_lower`. `Dry::Inflector#camelize` is now an alias for `Dry::Inflector#camelize_upper`. ```ruby inflector.camelize_upper("data_mapper") # => "DataMapper"' inflector.camelize_lower("data_mapper") # => "dataMapper"' ```
|
36
|
+
|
37
|
+
### Fixed
|
38
|
+
|
39
|
+
- [ecnal] Fixed singularization rules for words like "alias" or "status"
|
40
|
+
|
41
|
+
|
42
|
+
[Compare v0.1.2...v0.2.0](https://github.com/dry-rb/dry-inflector/compare/v0.1.2...v0.2.0)
|
43
|
+
|
44
|
+
## 0.1.2 2018-04-25
|
4
45
|
|
5
|
-
## v0.1.2 - to be released
|
6
46
|
|
7
47
|
### Added
|
8
48
|
|
9
49
|
- [Gustavo Caso & Nikita Shilnikov] Added support for acronyms
|
10
50
|
|
11
|
-
[Compare v0.1.1...master](https://github.com/dry-rb/dry-inflector/compare/v0.1.1...master)
|
12
51
|
|
13
|
-
|
52
|
+
[Compare v0.1.1...v0.1.2](https://github.com/dry-rb/dry-inflector/compare/v0.1.1...v0.1.2)
|
53
|
+
|
54
|
+
## 0.1.1 2017-11-18
|
55
|
+
|
56
|
+
|
14
57
|
### Fixed
|
58
|
+
|
15
59
|
- [Luca Guidi & Abinoam P. Marques Jr.] Ensure `Dry::Inflector#ordinalize` to work for all the numbers from 0 to 100
|
16
60
|
|
61
|
+
|
17
62
|
[Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-inflector/compare/v0.1.0...v0.1.1)
|
18
63
|
|
19
|
-
##
|
64
|
+
## 0.1.0 2017-11-17
|
65
|
+
|
66
|
+
|
20
67
|
### Added
|
68
|
+
|
21
69
|
- [Luca Guidi] Introduced `Dry::Inflector#pluralize`
|
22
70
|
- [Luca Guidi] Introduced `Dry::Inflector#singularize`
|
23
71
|
- [Luca Guidi] Introduced `Dry::Inflector#camelize`
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015-2021 dry-rb team
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,109 +1,30 @@
|
|
1
|
-
|
2
|
-
[gem]: https://
|
3
|
-
[
|
4
|
-
[
|
5
|
-
[
|
6
|
-
[coveralls]: https://coveralls.io/r/dry-rb/dry-inflector
|
1
|
+
<!--- this file is synced from dry-rb/template-gem project -->
|
2
|
+
[gem]: https://rubygems.org/gems/dry-inflector
|
3
|
+
[actions]: https://github.com/dry-rb/dry-inflector/actions
|
4
|
+
[codacy]: https://www.codacy.com/gh/dry-rb/dry-inflector
|
5
|
+
[chat]: https://dry-rb.zulipchat.com
|
7
6
|
[inchpages]: http://inch-ci.org/github/dry-rb/dry-inflector
|
8
7
|
|
9
|
-
# dry-inflector [![Join the chat at https://
|
8
|
+
# dry-inflector [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
|
10
9
|
|
11
10
|
[![Gem Version](https://badge.fury.io/rb/dry-inflector.svg)][gem]
|
12
|
-
[![
|
13
|
-
[![
|
14
|
-
[![
|
15
|
-
[![
|
16
|
-
[![Inline docs](http://inch-ci.org/github/dry-rb/dry-inflector.svg?branch=master)][inchpages]
|
11
|
+
[![CI Status](https://github.com/dry-rb/dry-inflector/workflows/ci/badge.svg)][actions]
|
12
|
+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/00339f2fb77840149f999cac6110c8a8)][codacy]
|
13
|
+
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/00339f2fb77840149f999cac6110c8a8)][codacy]
|
14
|
+
[![Inline docs](http://inch-ci.org/github/dry-rb/dry-inflector.svg?branch=main)][inchpages]
|
17
15
|
|
18
|
-
|
16
|
+
## Links
|
19
17
|
|
20
|
-
|
18
|
+
* [User documentation](https://dry-rb.org/gems/dry-inflector)
|
19
|
+
* [API documentation](http://rubydoc.info/gems/dry-inflector)
|
21
20
|
|
22
|
-
|
21
|
+
## Supported Ruby versions
|
23
22
|
|
24
|
-
|
25
|
-
gem 'dry-inflector'
|
26
|
-
```
|
23
|
+
This library officially supports the following Ruby versions:
|
27
24
|
|
28
|
-
|
25
|
+
* MRI `>= 2.7.0`
|
26
|
+
* jruby `>= 9.3` (postponed until 2.7 is supported)
|
29
27
|
|
30
|
-
|
31
|
-
$ bundle
|
32
|
-
```
|
28
|
+
## License
|
33
29
|
|
34
|
-
|
35
|
-
|
36
|
-
```shell
|
37
|
-
$ gem install dry-inflector
|
38
|
-
```
|
39
|
-
|
40
|
-
## Usage
|
41
|
-
|
42
|
-
### Basic usage
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
require "dry/inflector"
|
46
|
-
|
47
|
-
inflector = Dry::Inflector.new
|
48
|
-
|
49
|
-
inflector.pluralize("book") # => "books"
|
50
|
-
inflector.singularize("books") # => "book"
|
51
|
-
|
52
|
-
inflector.camelize("dry/inflector") # => "Dry::Inflector"
|
53
|
-
inflector.classify("books") # => "Book"
|
54
|
-
inflector.tableize("Book") # => "books"
|
55
|
-
|
56
|
-
inflector.dasherize("dry_inflector") # => "dry-inflector"
|
57
|
-
inflector.underscore("dry-inflector") # => "dry_inflector"
|
58
|
-
|
59
|
-
inflector.demodulize("Dry::Inflector") # => "Inflector"
|
60
|
-
|
61
|
-
inflector.humanize("dry_inflector") # => "Dry inflector"
|
62
|
-
inflector.humanize("author_id") # => "Author"
|
63
|
-
|
64
|
-
inflector.ordinalize(1) # => "1st"
|
65
|
-
inflector.ordinalize(2) # => "2nd"
|
66
|
-
inflector.ordinalize(3) # => "3rd"
|
67
|
-
inflector.ordinalize(10) # => "10th"
|
68
|
-
inflector.ordinalize(23) # => "23rd"
|
69
|
-
```
|
70
|
-
|
71
|
-
### Custom inflection rules
|
72
|
-
|
73
|
-
```ruby
|
74
|
-
require "dry/inflector"
|
75
|
-
|
76
|
-
inflector = Dry::Inflector.new do |inflections|
|
77
|
-
inflections.plural "virus", "viruses" # specify a rule for #pluralize
|
78
|
-
inflections.singular "thieves", "thief" # specify a rule for #singularize
|
79
|
-
inflections.uncountable "dry-inflector" # add an exception for an uncountable word
|
80
|
-
end
|
81
|
-
|
82
|
-
inflector.pluralize("virus") # => "viruses"
|
83
|
-
inflector.singularize("thieves") # => "thief"
|
84
|
-
|
85
|
-
inflector.pluralize("dry-inflector") # => "dry-inflector"
|
86
|
-
```
|
87
|
-
|
88
|
-
## Credits
|
89
|
-
|
90
|
-
This gem is the cumulative effort of the Ruby community.
|
91
|
-
It started with the extlib inflecto originated from [active_support](https://github.com/rails/rails), then dm-core inflector originated from [extlib](https://github.com/datamapper/extlib).
|
92
|
-
Later, [`inflecto`](https://github.com/mbj/inflecto) was extracted from [dm-core](https://github.com/datamapper/dm-core) as a standalone inflector.
|
93
|
-
Now, we resurrect `inflecto` and merged [`flexus`](https://github.com/Ptico/flexus), with some inflection rules from [`hanami-utils`](https://github.com/hanami/utils).
|
94
|
-
|
95
|
-
This is `dry-inflector`.
|
96
|
-
|
97
|
-
## Development
|
98
|
-
|
99
|
-
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
100
|
-
|
101
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
102
|
-
|
103
|
-
## Contributing
|
104
|
-
|
105
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/dry-rb/dry-inflector.
|
106
|
-
|
107
|
-
## Copyright
|
108
|
-
|
109
|
-
Copyright © The Dry, Rails, Merb, Datamapper, Inflecto, Flexus, and Hanami teams - Released under the MIT License
|
30
|
+
See `LICENSE` file.
|
data/dry-inflector.gemspec
CHANGED
@@ -1,32 +1,36 @@
|
|
1
|
-
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
3
|
+
# this file is synced from dry-rb/template-gem project
|
4
|
+
|
5
|
+
lib = File.expand_path("lib", __dir__)
|
5
6
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
7
|
require "dry/inflector/version"
|
7
8
|
|
8
9
|
Gem::Specification.new do |spec|
|
9
10
|
spec.name = "dry-inflector"
|
10
|
-
spec.version = Dry::Inflector::VERSION
|
11
11
|
spec.authors = ["Luca Guidi", "Andrii Savchenko", "Abinoam P. Marques Jr."]
|
12
12
|
spec.email = ["me@lucaguidi.com", "andrey@aejis.eu", "abinoam@gmail.com"]
|
13
|
-
|
14
|
-
spec.summary = "DRY Inflector"
|
15
|
-
spec.description = "String inflections for dry-rb"
|
16
|
-
spec.homepage = "http://dry-rb.org"
|
17
13
|
spec.license = "MIT"
|
14
|
+
spec.version = Dry::Inflector::VERSION.dup
|
15
|
+
|
16
|
+
spec.summary = "String inflections for dry-rb"
|
17
|
+
spec.description = spec.summary
|
18
|
+
spec.homepage = "https://dry-rb.org/gems/dry-inflector"
|
19
|
+
spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-inflector.gemspec", "lib/**/*"]
|
20
|
+
spec.bindir = "bin"
|
21
|
+
spec.executables = []
|
22
|
+
spec.require_paths = ["lib"]
|
18
23
|
|
19
24
|
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
25
|
+
spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-inflector/blob/master/CHANGELOG.md"
|
26
|
+
spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-inflector"
|
27
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-inflector/issues"
|
20
28
|
|
21
|
-
spec.
|
22
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
-
spec.require_paths = ["lib"]
|
29
|
+
spec.required_ruby_version = ">= 2.7.0"
|
24
30
|
|
25
|
-
|
26
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
31
|
+
# to update dependencies edit project.yml
|
27
32
|
|
28
|
-
spec.add_development_dependency "bundler"
|
29
|
-
spec.add_development_dependency "rake"
|
30
|
-
spec.add_development_dependency "rspec"
|
31
|
-
spec.add_development_dependency "rubocop", "~> 0.50.0"
|
33
|
+
spec.add_development_dependency "bundler"
|
34
|
+
spec.add_development_dependency "rake"
|
35
|
+
spec.add_development_dependency "rspec"
|
32
36
|
end
|
@@ -9,7 +9,6 @@ module Dry
|
|
9
9
|
# @api private
|
10
10
|
#
|
11
11
|
# rubocop:disable Metrics/AbcSize
|
12
|
-
# rubocop:disable Metrics/MethodLength
|
13
12
|
module Defaults
|
14
13
|
# @since 0.1.0
|
15
14
|
# @api private
|
@@ -35,7 +34,7 @@ module Dry
|
|
35
34
|
inflect.plural(/(?<!b)um\z/i, '\1a')
|
36
35
|
inflect.plural(/([ti])a\z/i, '\1a')
|
37
36
|
inflect.plural(/sis\z/i, "ses")
|
38
|
-
inflect.plural(/(.*)(?:([^f]))
|
37
|
+
inflect.plural(/(.*)(?:([^f]))fe*\z/i, '\1\2ves')
|
39
38
|
inflect.plural(/(hive|proof)\z/i, '\1s') # TODO: proof can be moved in the above regexp
|
40
39
|
inflect.plural(/([^aeiouy]|qu)y\z/i, '\1ies')
|
41
40
|
inflect.plural(/(x|ch|ss|sh)\z/i, '\1es')
|
@@ -57,8 +56,9 @@ module Dry
|
|
57
56
|
inflect.singular(/s\z/i, "")
|
58
57
|
inflect.singular(/(n)ews\z/i, '\1ews')
|
59
58
|
inflect.singular(/([ti])a\z/i, '\1um')
|
60
|
-
inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses\z/i,
|
61
|
-
|
59
|
+
inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)\z/i,
|
60
|
+
'\1\2sis')
|
61
|
+
inflect.singular(/(^analy)(sis|ses)\z/i, '\1sis')
|
62
62
|
inflect.singular(/([^f])ves\z/i, '\1fe')
|
63
63
|
inflect.singular(/(hive)s\z/i, '\1')
|
64
64
|
inflect.singular(/(tive)s\z/i, '\1')
|
@@ -69,12 +69,12 @@ module Dry
|
|
69
69
|
inflect.singular(/(ss)\z/i, '\1')
|
70
70
|
inflect.singular(/(x|ch|ss|sh)es\z/i, '\1')
|
71
71
|
inflect.singular(/([m|l])ice\z/i, '\1ouse')
|
72
|
-
inflect.singular(/(
|
72
|
+
inflect.singular(/(us)(es)?\z/i, '\1')
|
73
73
|
inflect.singular(/(o)es\z/i, '\1')
|
74
74
|
inflect.singular(/(shoe)s\z/i, '\1')
|
75
|
-
inflect.singular(/(cris|ax|test)es\z/i, '\1is')
|
76
|
-
inflect.singular(/(octop|vir)i\z/i, '\1us')
|
77
|
-
inflect.singular(/(alias|status)es
|
75
|
+
inflect.singular(/(cris|ax|test)(is|es)\z/i, '\1is')
|
76
|
+
inflect.singular(/(octop|vir)(us|i)\z/i, '\1us')
|
77
|
+
inflect.singular(/(alias|status)(es)?\z/i, '\1')
|
78
78
|
inflect.singular(/^(ox)en/i, '\1')
|
79
79
|
inflect.singular(/(vert|ind)ices\z/i, '\1ex')
|
80
80
|
inflect.singular(/(matr)ices\z/i, '\1ix')
|
@@ -87,30 +87,34 @@ module Dry
|
|
87
87
|
def self.irregular(inflect)
|
88
88
|
inflect.irregular("person", "people")
|
89
89
|
inflect.irregular("man", "men")
|
90
|
-
|
90
|
+
|
91
|
+
# NOTE: this is here only to override the previous rule
|
92
|
+
inflect.irregular("human", "humans")
|
91
93
|
inflect.irregular("child", "children")
|
92
94
|
inflect.irregular("sex", "sexes")
|
93
95
|
inflect.irregular("foot", "feet")
|
94
96
|
inflect.irregular("tooth", "teeth")
|
95
97
|
inflect.irregular("goose", "geese")
|
96
|
-
|
98
|
+
|
99
|
+
# FIXME: this is here because I need to fix the "um" regexp
|
100
|
+
inflect.irregular("forum", "forums")
|
97
101
|
end
|
98
102
|
|
99
103
|
# @since 0.1.0
|
100
104
|
# @api private
|
101
105
|
def self.uncountable(inflect)
|
102
|
-
inflect.uncountable(%w[hovercraft moose deer milk rain Swiss grass equipment information
|
106
|
+
inflect.uncountable(%w[hovercraft moose deer milk rain Swiss grass equipment information
|
107
|
+
rice money species series fish sheep jeans])
|
103
108
|
end
|
104
109
|
|
105
110
|
# @since 0.1.2
|
106
111
|
# @api private
|
107
112
|
def self.acronyms(inflect)
|
108
|
-
inflect.acronym(*%w[JSON HTTP OpenSSL HMAC])
|
113
|
+
inflect.acronym(*%w[JSON HTTP OpenSSL HMAC CSRF API CSV])
|
109
114
|
end
|
110
115
|
|
111
116
|
private_class_method :plural, :singular, :irregular, :uncountable, :acronyms
|
112
117
|
end
|
113
|
-
# rubocop:enable Metrics/MethodLength
|
114
118
|
# rubocop:enable Metrics/AbcSize
|
115
119
|
end
|
116
120
|
end
|
@@ -87,7 +87,9 @@ module Dry
|
|
87
87
|
#
|
88
88
|
# Specifies a new pluralization rule and its replacement.
|
89
89
|
# The rule can either be a string or a regular expression.
|
90
|
-
#
|
90
|
+
#
|
91
|
+
# The replacement should always be a string that may include
|
92
|
+
# references to the matched data from the rule.
|
91
93
|
#
|
92
94
|
# @param rule [String, Regexp] the rule
|
93
95
|
# @param replacement [String] the replacement
|
@@ -108,7 +110,9 @@ module Dry
|
|
108
110
|
#
|
109
111
|
# Specifies a new singularization rule and its replacement.
|
110
112
|
# The rule can either be a string or a regular expression.
|
111
|
-
#
|
113
|
+
#
|
114
|
+
# The replacement should always be a string that may include
|
115
|
+
# references to the matched data from the rule.
|
112
116
|
#
|
113
117
|
# @param rule [String, Regexp] the rule
|
114
118
|
# @param replacement [String] the replacement
|
@@ -127,7 +131,9 @@ module Dry
|
|
127
131
|
|
128
132
|
# Add a custom pluralization rule
|
129
133
|
#
|
130
|
-
# Specifies a new irregular that applies to both pluralization
|
134
|
+
# Specifies a new irregular that applies to both pluralization
|
135
|
+
# and singularization at the same time.
|
136
|
+
#
|
131
137
|
# This can only be used for strings, not regular expressions.
|
132
138
|
# You simply pass the irregular in singular and plural form.
|
133
139
|
#
|
@@ -194,9 +200,14 @@ module Dry
|
|
194
200
|
|
195
201
|
# Add a custom humanize rule
|
196
202
|
#
|
197
|
-
# Specifies a humanized form of a string by a regular expression rule or
|
198
|
-
#
|
199
|
-
#
|
203
|
+
# Specifies a humanized form of a string by a regular expression rule or
|
204
|
+
# by a string mapping.
|
205
|
+
#
|
206
|
+
# When using a regular expression based replacement, the normal humanize
|
207
|
+
# formatting is called after the replacement.
|
208
|
+
#
|
209
|
+
# When a string is used, the human form should be specified as desired
|
210
|
+
# (example: `"The name"`, not `"the_name"`)
|
200
211
|
#
|
201
212
|
# @param rule [String, Regexp] the rule
|
202
213
|
# @param replacement [String] the replacement
|
@@ -227,7 +238,7 @@ module Dry
|
|
227
238
|
# @api private
|
228
239
|
def add_irregular(rule, replacement, target)
|
229
240
|
head, *tail = rule.chars.to_a
|
230
|
-
rule(/(#{head})#{tail.join}\z/i,
|
241
|
+
rule(/(#{head})#{tail.join}\z/i, "\\1#{replacement[1..]}", target)
|
231
242
|
end
|
232
243
|
|
233
244
|
# Add a new rule
|
data/lib/dry/inflector.rb
CHANGED
@@ -34,30 +34,41 @@ module Dry
|
|
34
34
|
@inflections = Inflections.build(&blk)
|
35
35
|
end
|
36
36
|
|
37
|
-
#
|
37
|
+
# Lower camelize a string
|
38
38
|
#
|
39
39
|
# @param input [String,Symbol] the input
|
40
|
-
# @return [String] the camelized string
|
40
|
+
# @return [String] the lower camelized string
|
41
41
|
#
|
42
|
-
# @since 0.1.
|
42
|
+
# @since 0.1.3
|
43
43
|
#
|
44
44
|
# @example
|
45
45
|
# require "dry/inflector"
|
46
46
|
#
|
47
47
|
# inflector = Dry::Inflector.new
|
48
|
-
# inflector.
|
49
|
-
def
|
50
|
-
input
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
48
|
+
# inflector.camelize_lower("data_mapper") # => "dataMapper"
|
49
|
+
def camelize_lower(input)
|
50
|
+
internal_camelize(input, false)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Upper camelize a string
|
54
|
+
#
|
55
|
+
# @param input [String,Symbol] the input
|
56
|
+
# @return [String] the upper camelized string
|
57
|
+
#
|
58
|
+
# @since 0.1.3
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# require "dry/inflector"
|
62
|
+
#
|
63
|
+
# inflector = Dry::Inflector.new
|
64
|
+
# inflector.camelize_upper("data_mapper") # => "DataMapper"
|
65
|
+
# inflector.camelize_upper("dry/inflector") # => "Dry::Inflector"
|
66
|
+
def camelize_upper(input)
|
67
|
+
internal_camelize(input, true)
|
59
68
|
end
|
60
69
|
|
70
|
+
alias_method :camelize, :camelize_upper
|
71
|
+
|
61
72
|
# Find a constant with the name specified in the argument string
|
62
73
|
#
|
63
74
|
# The name is assumed to be the one of a top-level constant,
|
@@ -75,7 +86,7 @@ module Dry
|
|
75
86
|
# inflector.constantize("Module") # => Module
|
76
87
|
# inflector.constantize("Dry::Inflector") # => Dry::Inflector
|
77
88
|
def constantize(input)
|
78
|
-
Object.const_get(input)
|
89
|
+
Object.const_get(input, false)
|
79
90
|
end
|
80
91
|
|
81
92
|
# Classify a string
|
@@ -91,7 +102,7 @@ module Dry
|
|
91
102
|
# inflector = Dry::Inflector.new
|
92
103
|
# inflector.classify("books") # => "Book"
|
93
104
|
def classify(input)
|
94
|
-
camelize(singularize(input.to_s.
|
105
|
+
camelize(singularize(input.to_s.split(".").last))
|
95
106
|
end
|
96
107
|
|
97
108
|
# Dasherize a string
|
@@ -142,12 +153,12 @@ module Dry
|
|
142
153
|
def humanize(input)
|
143
154
|
input = input.to_s
|
144
155
|
result = inflections.humans.apply_to(input)
|
145
|
-
result.
|
156
|
+
result.delete_suffix!("_id")
|
146
157
|
result.tr!("_", " ")
|
147
|
-
match = /(
|
148
|
-
separator = match ? match[
|
158
|
+
match = /(\W)/.match(result)
|
159
|
+
separator = match ? match[0] : DEFAULT_SEPARATOR
|
149
160
|
result.split(separator).map.with_index { |word, index|
|
150
|
-
inflections.acronyms.apply_to(word, index.zero?)
|
161
|
+
inflections.acronyms.apply_to(word, capitalize: index.zero?)
|
151
162
|
}.join(separator)
|
152
163
|
end
|
153
164
|
|
@@ -181,10 +192,10 @@ module Dry
|
|
181
192
|
# inflector.ordinalize(3) # => "3rd"
|
182
193
|
# inflector.ordinalize(10) # => "10th"
|
183
194
|
# inflector.ordinalize(23) # => "23rd"
|
184
|
-
def ordinalize(number)
|
195
|
+
def ordinalize(number)
|
185
196
|
abs_value = number.abs
|
186
197
|
|
187
|
-
if ORDINALIZE_TH
|
198
|
+
if ORDINALIZE_TH[abs_value % 100]
|
188
199
|
"#{number}th"
|
189
200
|
else
|
190
201
|
case abs_value % 10
|
@@ -212,6 +223,7 @@ module Dry
|
|
212
223
|
def pluralize(input)
|
213
224
|
input = input.to_s
|
214
225
|
return input if uncountable?(input)
|
226
|
+
|
215
227
|
inflections.plurals.apply_to(input)
|
216
228
|
end
|
217
229
|
|
@@ -231,6 +243,7 @@ module Dry
|
|
231
243
|
def singularize(input)
|
232
244
|
input = input.to_s
|
233
245
|
return input if uncountable?(input)
|
246
|
+
|
234
247
|
inflections.singulars.apply_to(input)
|
235
248
|
end
|
236
249
|
|
@@ -247,7 +260,7 @@ module Dry
|
|
247
260
|
# inflector = Dry::Inflector.new
|
248
261
|
# inflector.tableize("Book") # => "books"
|
249
262
|
def tableize(input)
|
250
|
-
input = input.to_s.gsub(
|
263
|
+
input = input.to_s.gsub("::", "_")
|
251
264
|
pluralize(underscore(input))
|
252
265
|
end
|
253
266
|
|
@@ -268,7 +281,7 @@ module Dry
|
|
268
281
|
input.gsub!(inflections.acronyms.regex) do
|
269
282
|
m1 = Regexp.last_match(1)
|
270
283
|
m2 = Regexp.last_match(2)
|
271
|
-
"#{m1 ?
|
284
|
+
"#{m1 ? "_" : ""}#{m2.downcase}"
|
272
285
|
end
|
273
286
|
input.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
274
287
|
input.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
@@ -285,21 +298,42 @@ module Dry
|
|
285
298
|
# @since 0.1.0
|
286
299
|
# @api private
|
287
300
|
def uncountable?(input)
|
288
|
-
|
301
|
+
input.match?(/\A[[:space:]]*\z/) || inflections.uncountables.include?(input.downcase)
|
289
302
|
end
|
290
303
|
|
304
|
+
# @return [String]
|
305
|
+
#
|
306
|
+
# @since 0.2.0
|
307
|
+
# @api public
|
308
|
+
def to_s
|
309
|
+
"#<Dry::Inflector>"
|
310
|
+
end
|
311
|
+
alias_method :inspect, :to_s
|
312
|
+
|
291
313
|
private
|
292
314
|
|
293
315
|
# @since 0.1.0
|
294
316
|
# @api private
|
295
|
-
ORDINALIZE_TH =
|
317
|
+
ORDINALIZE_TH = {11 => true, 12 => true, 13 => true}.freeze
|
296
318
|
|
297
319
|
# @since 0.1.2
|
298
320
|
# @api private
|
299
321
|
DEFAULT_SEPARATOR = " "
|
300
322
|
|
301
|
-
# @since 0.1.0
|
302
|
-
# @api private
|
303
323
|
attr_reader :inflections
|
324
|
+
|
325
|
+
# @since 0.1.3
|
326
|
+
# @api private
|
327
|
+
def internal_camelize(input, upper)
|
328
|
+
input = input.to_s.dup
|
329
|
+
input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match, capitalize: upper) }
|
330
|
+
input.gsub!(%r{(?:[_-]|(/))([a-z\d]*)}i) do
|
331
|
+
m1 = Regexp.last_match(1)
|
332
|
+
m2 = Regexp.last_match(2)
|
333
|
+
"#{m1}#{inflections.acronyms.apply_to(m2)}"
|
334
|
+
end
|
335
|
+
input.gsub!("/", "::")
|
336
|
+
input
|
337
|
+
end
|
304
338
|
end
|
305
339
|
end
|
metadata
CHANGED
@@ -1,73 +1,59 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-inflector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
8
8
|
- Andrii Savchenko
|
9
9
|
- Abinoam P. Marques Jr.
|
10
10
|
autorequire:
|
11
|
-
bindir:
|
11
|
+
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2022-07-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
|
-
- - "
|
19
|
+
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
21
|
+
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
|
-
- - "
|
26
|
+
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '
|
28
|
+
version: '0'
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: rake
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
32
32
|
requirements:
|
33
|
-
- - "
|
33
|
+
- - ">="
|
34
34
|
- !ruby/object:Gem::Version
|
35
|
-
version: '
|
35
|
+
version: '0'
|
36
36
|
type: :development
|
37
37
|
prerelease: false
|
38
38
|
version_requirements: !ruby/object:Gem::Requirement
|
39
39
|
requirements:
|
40
|
-
- - "
|
40
|
+
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '
|
42
|
+
version: '0'
|
43
43
|
- !ruby/object:Gem::Dependency
|
44
44
|
name: rspec
|
45
45
|
requirement: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
|
-
- - "
|
47
|
+
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: '
|
49
|
+
version: '0'
|
50
50
|
type: :development
|
51
51
|
prerelease: false
|
52
52
|
version_requirements: !ruby/object:Gem::Requirement
|
53
53
|
requirements:
|
54
|
-
- - "
|
54
|
+
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: '
|
57
|
-
- !ruby/object:Gem::Dependency
|
58
|
-
name: rubocop
|
59
|
-
requirement: !ruby/object:Gem::Requirement
|
60
|
-
requirements:
|
61
|
-
- - "~>"
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
version: 0.50.0
|
64
|
-
type: :development
|
65
|
-
prerelease: false
|
66
|
-
version_requirements: !ruby/object:Gem::Requirement
|
67
|
-
requirements:
|
68
|
-
- - "~>"
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
version: 0.50.0
|
56
|
+
version: '0'
|
71
57
|
description: String inflections for dry-rb
|
72
58
|
email:
|
73
59
|
- me@lucaguidi.com
|
@@ -78,20 +64,24 @@ extensions: []
|
|
78
64
|
extra_rdoc_files: []
|
79
65
|
files:
|
80
66
|
- CHANGELOG.md
|
81
|
-
- LICENSE
|
67
|
+
- LICENSE
|
82
68
|
- README.md
|
83
69
|
- dry-inflector.gemspec
|
70
|
+
- lib/dry-inflector.rb
|
84
71
|
- lib/dry/inflector.rb
|
85
72
|
- lib/dry/inflector/acronyms.rb
|
86
73
|
- lib/dry/inflector/inflections.rb
|
87
74
|
- lib/dry/inflector/inflections/defaults.rb
|
88
75
|
- lib/dry/inflector/rules.rb
|
89
76
|
- lib/dry/inflector/version.rb
|
90
|
-
homepage:
|
77
|
+
homepage: https://dry-rb.org/gems/dry-inflector
|
91
78
|
licenses:
|
92
79
|
- MIT
|
93
80
|
metadata:
|
94
81
|
allowed_push_host: https://rubygems.org
|
82
|
+
changelog_uri: https://github.com/dry-rb/dry-inflector/blob/master/CHANGELOG.md
|
83
|
+
source_code_uri: https://github.com/dry-rb/dry-inflector
|
84
|
+
bug_tracker_uri: https://github.com/dry-rb/dry-inflector/issues
|
95
85
|
post_install_message:
|
96
86
|
rdoc_options: []
|
97
87
|
require_paths:
|
@@ -100,16 +90,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
90
|
requirements:
|
101
91
|
- - ">="
|
102
92
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
93
|
+
version: 2.7.0
|
104
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
95
|
requirements:
|
106
96
|
- - ">="
|
107
97
|
- !ruby/object:Gem::Version
|
108
98
|
version: '0'
|
109
99
|
requirements: []
|
110
|
-
|
111
|
-
rubygems_version: 2.7.6
|
100
|
+
rubygems_version: 3.1.6
|
112
101
|
signing_key:
|
113
102
|
specification_version: 4
|
114
|
-
summary:
|
103
|
+
summary: String inflections for dry-rb
|
115
104
|
test_files: []
|
data/LICENSE.md
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright © The Dry, Rails, Merb, Datamapper, Inflecto, Flexus, and Hanami teams
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|