hanami-utils 1.3.2 → 1.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/README.md +2 -3
- data/hanami-utils.gemspec +19 -16
- data/lib/hanami-utils.rb +3 -1
- data/lib/hanami/interactor.rb +57 -31
- data/lib/hanami/logger.rb +12 -12
- data/lib/hanami/logger/colorizer.rb +10 -10
- data/lib/hanami/logger/filter.rb +89 -10
- data/lib/hanami/logger/formatter.rb +4 -4
- data/lib/hanami/utils.rb +12 -10
- data/lib/hanami/utils/basic_object.rb +23 -6
- data/lib/hanami/utils/blank.rb +7 -5
- data/lib/hanami/utils/callbacks.rb +6 -2
- data/lib/hanami/utils/class.rb +8 -8
- data/lib/hanami/utils/class_attribute.rb +4 -2
- data/lib/hanami/utils/deprecation.rb +3 -1
- data/lib/hanami/utils/duplicable.rb +2 -0
- data/lib/hanami/utils/escape.rb +275 -271
- data/lib/hanami/utils/file_list.rb +3 -1
- data/lib/hanami/utils/files.rb +15 -2
- data/lib/hanami/utils/hash.rb +24 -22
- data/lib/hanami/utils/inflector.rb +134 -117
- data/lib/hanami/utils/io.rb +2 -0
- data/lib/hanami/utils/json.rb +5 -3
- data/lib/hanami/utils/kernel.rb +21 -20
- data/lib/hanami/utils/load_paths.rb +6 -4
- data/lib/hanami/utils/path_prefix.rb +6 -4
- data/lib/hanami/utils/query_string.rb +1 -1
- data/lib/hanami/utils/shell_color.rb +9 -9
- data/lib/hanami/utils/string.rb +53 -54
- data/lib/hanami/utils/version.rb +3 -1
- metadata +24 -10
data/lib/hanami/utils/io.rb
CHANGED
data/lib/hanami/utils/json.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
begin
|
2
|
-
require
|
4
|
+
require "multi_json"
|
3
5
|
rescue LoadError
|
4
|
-
require
|
6
|
+
require "json"
|
5
7
|
end
|
6
8
|
|
7
9
|
module Hanami
|
@@ -41,7 +43,7 @@ module Hanami
|
|
41
43
|
end
|
42
44
|
# rubocop:enable Style/ClassVars
|
43
45
|
|
44
|
-
#
|
46
|
+
# Parses the given JSON paylod
|
45
47
|
#
|
46
48
|
# @param payload [String] a JSON payload
|
47
49
|
#
|
data/lib/hanami/utils/kernel.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "set"
|
4
|
+
require "date"
|
5
|
+
require "time"
|
6
|
+
require "pathname"
|
7
|
+
require "bigdecimal"
|
8
|
+
require "hanami/utils"
|
9
|
+
require "hanami/utils/string"
|
8
10
|
|
9
11
|
unless defined?(Boolean)
|
10
|
-
#
|
12
|
+
# Defines top level constant Boolean, so it can be easily used by other libraries
|
11
13
|
# in coercions DSLs
|
12
14
|
#
|
13
15
|
# @since 0.3.0
|
@@ -30,7 +32,7 @@ module Hanami
|
|
30
32
|
|
31
33
|
# @since 0.8.0
|
32
34
|
# @api private
|
33
|
-
BOOLEAN_FALSE_STRING =
|
35
|
+
BOOLEAN_FALSE_STRING = "0"
|
34
36
|
|
35
37
|
# @since 0.8.0
|
36
38
|
# @api private
|
@@ -325,7 +327,7 @@ module Hanami
|
|
325
327
|
# # big complex represented as a string
|
326
328
|
# input = Complex(2, 3)
|
327
329
|
# Hanami::Utils::Kernel.Integer(input) # => TypeError
|
328
|
-
def self.Integer(arg)
|
330
|
+
def self.Integer(arg)
|
329
331
|
super(arg)
|
330
332
|
rescue ArgumentError, TypeError, NoMethodError
|
331
333
|
begin
|
@@ -416,7 +418,6 @@ module Hanami
|
|
416
418
|
# input = BasicObject.new
|
417
419
|
# Hanami::Utils::Kernel.BigDecimal(input) # => TypeError
|
418
420
|
#
|
419
|
-
# rubocop:disable Metrics/MethodLength
|
420
421
|
def self.BigDecimal(arg, precision = ::Float::DIG)
|
421
422
|
case arg
|
422
423
|
when NilClass # This is only needed by Ruby 2.6
|
@@ -433,7 +434,6 @@ module Hanami
|
|
433
434
|
rescue NoMethodError
|
434
435
|
raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal"
|
435
436
|
end
|
436
|
-
# rubocop:enable Metrics/MethodLength
|
437
437
|
|
438
438
|
# Coerces the argument to be a Float.
|
439
439
|
#
|
@@ -547,7 +547,7 @@ module Hanami
|
|
547
547
|
# # big complex represented as a string
|
548
548
|
# input = Complex(2, 3)
|
549
549
|
# Hanami::Utils::Kernel.Float(input) # => TypeError
|
550
|
-
def self.Float(arg)
|
550
|
+
def self.Float(arg)
|
551
551
|
super(arg)
|
552
552
|
rescue ArgumentError, TypeError
|
553
553
|
begin
|
@@ -736,7 +736,8 @@ module Hanami
|
|
736
736
|
# require 'hanami/utils/kernel'
|
737
737
|
#
|
738
738
|
# Hanami::Utils::Kernel.DateTime(3483943)
|
739
|
-
# # => Time.at(3483943).to_datetime
|
739
|
+
# # => Time.at(3483943).to_datetime
|
740
|
+
# # #<DateTime: 1970-02-10T08:45:43+01:00 ((2440628j,27943s,0n),+3600s,2299161j)>
|
740
741
|
#
|
741
742
|
# Hanami::Utils::Kernel.DateTime(DateTime.now)
|
742
743
|
# # => #<DateTime: 2014-04-18T09:33:49+02:00 ((2456766j,27229s,690849000n),+7200s,2299161j)>
|
@@ -896,7 +897,7 @@ module Hanami
|
|
896
897
|
# # Missing #respond_to?
|
897
898
|
# input = BasicObject.new
|
898
899
|
# Hanami::Utils::Kernel.Boolean(input) # => TypeError
|
899
|
-
def self.Boolean(arg)
|
900
|
+
def self.Boolean(arg)
|
900
901
|
case arg
|
901
902
|
when Numeric
|
902
903
|
arg.to_i == BOOLEAN_TRUE_INTEGER
|
@@ -1012,7 +1013,7 @@ module Hanami
|
|
1012
1013
|
# Hanami::Utils::Kernel.Symbol(input) # => TypeError
|
1013
1014
|
def self.Symbol(arg)
|
1014
1015
|
case arg
|
1015
|
-
when
|
1016
|
+
when "" then raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol"
|
1016
1017
|
when ->(a) { a.respond_to?(:to_sym) } then arg.to_sym
|
1017
1018
|
else
|
1018
1019
|
raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol"
|
@@ -1021,7 +1022,7 @@ module Hanami
|
|
1021
1022
|
raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol"
|
1022
1023
|
end
|
1023
1024
|
|
1024
|
-
#
|
1025
|
+
# Checks if the given argument is a string representation of a number
|
1025
1026
|
#
|
1026
1027
|
# @param arg [Object] the input
|
1027
1028
|
#
|
@@ -1042,11 +1043,11 @@ module Hanami
|
|
1042
1043
|
# @since 0.4.3
|
1043
1044
|
# @api private
|
1044
1045
|
def self.inspect_type_error(arg)
|
1045
|
-
(arg.respond_to?(:inspect) ? arg.inspect : arg.to_s)
|
1046
|
+
(arg.respond_to?(:inspect) ? arg.inspect : arg.to_s) + " "
|
1046
1047
|
rescue NoMethodError
|
1047
1048
|
# missing the #respond_to? method, fall back to returning the class' name
|
1048
1049
|
begin
|
1049
|
-
arg.class.name
|
1050
|
+
arg.class.name + " instance "
|
1050
1051
|
rescue NoMethodError
|
1051
1052
|
# missing the #class method, can't fall back to anything better than nothing
|
1052
1053
|
# Callers will have to guess from their code
|
@@ -1055,7 +1056,7 @@ module Hanami
|
|
1055
1056
|
end
|
1056
1057
|
|
1057
1058
|
class << self
|
1058
|
-
private :inspect_type_error
|
1059
|
+
private :inspect_type_error
|
1059
1060
|
end
|
1060
1061
|
end
|
1061
1062
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/utils/kernel"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Utils
|
@@ -48,7 +50,7 @@ module Hanami
|
|
48
50
|
@paths = original.instance_variable_get(:@paths).dup
|
49
51
|
end
|
50
52
|
|
51
|
-
# Iterates
|
53
|
+
# Iterates through the collection and yields the given block.
|
52
54
|
# It skips duplications and raises an error in case one of the paths
|
53
55
|
# doesn't exist.
|
54
56
|
#
|
@@ -115,7 +117,7 @@ module Hanami
|
|
115
117
|
self
|
116
118
|
end
|
117
119
|
|
118
|
-
|
120
|
+
alias_method :<<, :push
|
119
121
|
|
120
122
|
# It freezes the object by preventing further modifications.
|
121
123
|
#
|
@@ -156,7 +158,7 @@ module Hanami
|
|
156
158
|
|
157
159
|
private
|
158
160
|
|
159
|
-
#
|
161
|
+
# Allows subclasses to define their own policy to discover the realpath
|
160
162
|
# of the given path.
|
161
163
|
#
|
162
164
|
# @since 0.2.0
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/utils/string"
|
4
|
+
require "hanami/utils/kernel"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
module Utils
|
@@ -11,7 +13,7 @@ module Hanami
|
|
11
13
|
#
|
12
14
|
# @since 0.3.1
|
13
15
|
# @api private
|
14
|
-
DEFAULT_SEPARATOR =
|
16
|
+
DEFAULT_SEPARATOR = "/"
|
15
17
|
|
16
18
|
# Initialize the path prefix
|
17
19
|
#
|
@@ -133,7 +135,7 @@ module Hanami
|
|
133
135
|
# @see #relative
|
134
136
|
def relative!
|
135
137
|
@string.gsub!(/(?<!:)#{separator * 2}/, separator)
|
136
|
-
@string[/\A#{separator}|^/] =
|
138
|
+
@string[/\A#{separator}|^/] = ""
|
137
139
|
|
138
140
|
self
|
139
141
|
end
|
@@ -18,22 +18,22 @@ module Hanami
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
#
|
21
|
+
# Escapes codes for terminals to output strings in colors
|
22
22
|
#
|
23
23
|
# @since 1.2.0
|
24
24
|
# @api private
|
25
25
|
COLORS = ::Hash[
|
26
|
-
black:
|
27
|
-
red:
|
28
|
-
green:
|
29
|
-
yellow:
|
30
|
-
blue:
|
26
|
+
black: 30,
|
27
|
+
red: 31,
|
28
|
+
green: 32,
|
29
|
+
yellow: 33,
|
30
|
+
blue: 34,
|
31
31
|
magenta: 35,
|
32
|
-
cyan:
|
33
|
-
gray:
|
32
|
+
cyan: 36,
|
33
|
+
gray: 37,
|
34
34
|
].freeze
|
35
35
|
|
36
|
-
#
|
36
|
+
# Colorizes output
|
37
37
|
# 8 colors available: black, red, green, yellow, blue, magenta, cyan, and gray
|
38
38
|
#
|
39
39
|
# @param input [#to_s] the string to colorize
|
data/lib/hanami/utils/string.rb
CHANGED
@@ -1,30 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/utils/inflector"
|
4
|
+
require "transproc"
|
5
|
+
require "concurrent/map"
|
4
6
|
|
5
7
|
module Hanami
|
6
8
|
module Utils
|
7
9
|
# String on steroids
|
8
10
|
#
|
9
11
|
# @since 0.1.0
|
10
|
-
class String
|
12
|
+
class String
|
11
13
|
# Empty string for #classify
|
12
14
|
#
|
13
15
|
# @since 0.6.0
|
14
16
|
# @api private
|
15
|
-
EMPTY_STRING =
|
17
|
+
EMPTY_STRING = ""
|
16
18
|
|
17
19
|
# Separator between Ruby namespaces
|
18
20
|
#
|
19
21
|
# @since 0.1.0
|
20
22
|
# @api private
|
21
|
-
NAMESPACE_SEPARATOR =
|
23
|
+
NAMESPACE_SEPARATOR = "::"
|
22
24
|
|
23
25
|
# Separator for #classify
|
24
26
|
#
|
25
27
|
# @since 0.3.0
|
26
28
|
# @api private
|
27
|
-
CLASSIFY_SEPARATOR =
|
29
|
+
CLASSIFY_SEPARATOR = "_"
|
28
30
|
|
29
31
|
# Regexp for #tokenize
|
30
32
|
#
|
@@ -36,52 +38,53 @@ module Hanami
|
|
36
38
|
#
|
37
39
|
# @since 0.3.0
|
38
40
|
# @api private
|
39
|
-
TOKENIZE_SEPARATOR =
|
41
|
+
TOKENIZE_SEPARATOR = "|"
|
40
42
|
|
41
43
|
# Separator for #underscore
|
42
44
|
#
|
43
45
|
# @since 0.3.0
|
44
46
|
# @api private
|
45
|
-
UNDERSCORE_SEPARATOR =
|
47
|
+
UNDERSCORE_SEPARATOR = "/"
|
46
48
|
|
47
49
|
# gsub second parameter used in #underscore
|
48
50
|
#
|
49
51
|
# @since 0.3.0
|
50
52
|
# @api private
|
51
|
-
UNDERSCORE_DIVISION_TARGET = '\1_\2'
|
53
|
+
UNDERSCORE_DIVISION_TARGET = '\1_\2'
|
52
54
|
|
53
55
|
# Separator for #titleize
|
54
56
|
#
|
55
57
|
# @since 0.4.0
|
56
58
|
# @api private
|
57
|
-
TITLEIZE_SEPARATOR =
|
59
|
+
TITLEIZE_SEPARATOR = " "
|
58
60
|
|
59
61
|
# Separator for #capitalize
|
60
62
|
#
|
61
63
|
# @since 0.5.2
|
62
64
|
# @api private
|
63
|
-
CAPITALIZE_SEPARATOR =
|
65
|
+
CAPITALIZE_SEPARATOR = " "
|
64
66
|
|
65
67
|
# Separator for #dasherize
|
66
68
|
#
|
67
69
|
# @since 0.4.0
|
68
70
|
# @api private
|
69
|
-
DASHERIZE_SEPARATOR =
|
71
|
+
DASHERIZE_SEPARATOR = "-"
|
70
72
|
|
71
73
|
# Regexp for #classify
|
72
74
|
#
|
73
75
|
# @since 0.3.4
|
74
76
|
# @api private
|
75
|
-
CLASSIFY_WORD_SEPARATOR = /#{CLASSIFY_SEPARATOR}|#{NAMESPACE_SEPARATOR}|#{UNDERSCORE_SEPARATOR}|#{DASHERIZE_SEPARATOR}/.freeze
|
77
|
+
CLASSIFY_WORD_SEPARATOR = /#{CLASSIFY_SEPARATOR}|#{NAMESPACE_SEPARATOR}|#{UNDERSCORE_SEPARATOR}|#{DASHERIZE_SEPARATOR}/.freeze # rubocop:disable Layout/LineLength
|
76
78
|
|
77
79
|
@__transformations__ = Concurrent::Map.new
|
78
80
|
|
79
81
|
extend Transproc::Registry
|
80
82
|
extend Transproc::Composer
|
81
83
|
|
82
|
-
#
|
84
|
+
# Applies the given transformation(s) to `input`
|
83
85
|
#
|
84
|
-
# It performs a pipeline of transformations, by applying the given
|
86
|
+
# It performs a pipeline of transformations, by applying the given
|
87
|
+
# functions from `Hanami::Utils::String` and `::String`.
|
85
88
|
# The transformations are applied in the given order.
|
86
89
|
#
|
87
90
|
# It doesn't mutate the input, unless you use destructive methods from `::String`
|
@@ -129,8 +132,6 @@ module Hanami
|
|
129
132
|
# Hanami::Utils::String.transform("Cherry", -> { "blossom" }))
|
130
133
|
# # => ArgumentError: wrong number of arguments (given 1, expected 0)
|
131
134
|
#
|
132
|
-
# rubocop:disable Metrics/AbcSize
|
133
|
-
# rubocop:disable Metrics/MethodLength
|
134
135
|
def self.transform(input, *transformations)
|
135
136
|
fn = @__transformations__.fetch_or_store(transformations.hash) do
|
136
137
|
compose do |fns|
|
@@ -142,7 +143,7 @@ module Hanami
|
|
142
143
|
elsif input.respond_to?(transformation)
|
143
144
|
t(:bind, input, ->(i) { i.public_send(transformation, *args) })
|
144
145
|
else
|
145
|
-
raise NoMethodError.new(%(undefined method `#{transformation.inspect}' for #{input.inspect}:#{input.class}))
|
146
|
+
raise NoMethodError.new(%(undefined method `#{transformation.inspect}' for #{input.inspect}:#{input.class})) # rubocop:disable Layout/LineLength
|
146
147
|
end
|
147
148
|
end
|
148
149
|
end
|
@@ -150,8 +151,6 @@ module Hanami
|
|
150
151
|
|
151
152
|
fn.call(input)
|
152
153
|
end
|
153
|
-
# rubocop:enable Metrics/MethodLength
|
154
|
-
# rubocop:enable Metrics/AbcSize
|
155
154
|
|
156
155
|
# Extracted from `transproc` source code
|
157
156
|
#
|
@@ -164,7 +163,7 @@ module Hanami
|
|
164
163
|
binding.instance_exec(value, &fun)
|
165
164
|
end
|
166
165
|
|
167
|
-
#
|
166
|
+
# Returns a titleized version of the string
|
168
167
|
#
|
169
168
|
# @param input [::String] the input
|
170
169
|
#
|
@@ -181,7 +180,7 @@ module Hanami
|
|
181
180
|
underscore(string).split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
|
182
181
|
end
|
183
182
|
|
184
|
-
#
|
183
|
+
# Returns a capitalized version of the string
|
185
184
|
#
|
186
185
|
# @param input [::String] the input
|
187
186
|
#
|
@@ -208,7 +207,7 @@ module Hanami
|
|
208
207
|
tail.unshift(head.capitalize).join(CAPITALIZE_SEPARATOR)
|
209
208
|
end
|
210
209
|
|
211
|
-
#
|
210
|
+
# Returns a CamelCase version of the string
|
212
211
|
#
|
213
212
|
# @param input [::String] the input
|
214
213
|
#
|
@@ -232,7 +231,7 @@ module Hanami
|
|
232
231
|
words.zip(delimiters).join
|
233
232
|
end
|
234
233
|
|
235
|
-
#
|
234
|
+
# Returns a downcased and underscore separated version of the string
|
236
235
|
#
|
237
236
|
# Revised version of `ActiveSupport::Inflector.underscore` implementation
|
238
237
|
# @see https://github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
|
@@ -257,7 +256,7 @@ module Hanami
|
|
257
256
|
string.downcase
|
258
257
|
end
|
259
258
|
|
260
|
-
#
|
259
|
+
# Returns a downcased and dash separated version of the string
|
261
260
|
#
|
262
261
|
# @param input [::String] the input
|
263
262
|
#
|
@@ -278,7 +277,7 @@ module Hanami
|
|
278
277
|
underscore(string).split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR)
|
279
278
|
end
|
280
279
|
|
281
|
-
#
|
280
|
+
# Returns the string without the Ruby namespace of the class
|
282
281
|
#
|
283
282
|
# @param input [::String] the input
|
284
283
|
#
|
@@ -296,7 +295,7 @@ module Hanami
|
|
296
295
|
::String.new(input.to_s).split(NAMESPACE_SEPARATOR).last
|
297
296
|
end
|
298
297
|
|
299
|
-
#
|
298
|
+
# Returns the top level namespace name
|
300
299
|
#
|
301
300
|
# @param input [::String] the input
|
302
301
|
#
|
@@ -314,7 +313,7 @@ module Hanami
|
|
314
313
|
::String.new(input.to_s).split(NAMESPACE_SEPARATOR).first
|
315
314
|
end
|
316
315
|
|
317
|
-
#
|
316
|
+
# Returns a pluralized version of self.
|
318
317
|
#
|
319
318
|
# @param input [::String] the input
|
320
319
|
#
|
@@ -334,7 +333,7 @@ module Hanami
|
|
334
333
|
Inflector.pluralize(string)
|
335
334
|
end
|
336
335
|
|
337
|
-
#
|
336
|
+
# Returns a singularized version of self.
|
338
337
|
#
|
339
338
|
# @param input [::String] the input
|
340
339
|
#
|
@@ -354,7 +353,7 @@ module Hanami
|
|
354
353
|
Inflector.singularize(string)
|
355
354
|
end
|
356
355
|
|
357
|
-
#
|
356
|
+
# Replaces the rightmost match of `pattern` with `replacement`
|
358
357
|
#
|
359
358
|
# If the pattern cannot be matched, it returns the original string.
|
360
359
|
#
|
@@ -375,7 +374,7 @@ module Hanami
|
|
375
374
|
# # => 'authors/books#index'
|
376
375
|
def self.rsub(input, pattern, replacement)
|
377
376
|
string = ::String.new(input.to_s)
|
378
|
-
if i = string.rindex(pattern)
|
377
|
+
if i = string.rindex(pattern)
|
379
378
|
s = string.dup
|
380
379
|
s[i] = replacement
|
381
380
|
s
|
@@ -396,7 +395,7 @@ module Hanami
|
|
396
395
|
@string = string.to_s
|
397
396
|
end
|
398
397
|
|
399
|
-
#
|
398
|
+
# Returns a titleized version of the string
|
400
399
|
#
|
401
400
|
# @return [Hanami::Utils::String] the transformed string
|
402
401
|
#
|
@@ -412,7 +411,7 @@ module Hanami
|
|
412
411
|
self.class.new underscore.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
|
413
412
|
end
|
414
413
|
|
415
|
-
#
|
414
|
+
# Returns a capitalized version of the string
|
416
415
|
#
|
417
416
|
# @return [Hanami::Utils::String] the transformed string
|
418
417
|
#
|
@@ -444,7 +443,7 @@ module Hanami
|
|
444
443
|
)
|
445
444
|
end
|
446
445
|
|
447
|
-
#
|
446
|
+
# Returns a CamelCase version of the string
|
448
447
|
#
|
449
448
|
# @return [Hanami::Utils::String] the transformed string
|
450
449
|
#
|
@@ -467,7 +466,7 @@ module Hanami
|
|
467
466
|
self.class.new words.zip(delimiters).join
|
468
467
|
end
|
469
468
|
|
470
|
-
#
|
469
|
+
# Returns a downcased and underscore separated version of the string
|
471
470
|
#
|
472
471
|
# Revised version of `ActiveSupport::Inflector.underscore` implementation
|
473
472
|
# @see https://github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
|
@@ -491,7 +490,7 @@ module Hanami
|
|
491
490
|
self.class.new new_string
|
492
491
|
end
|
493
492
|
|
494
|
-
#
|
493
|
+
# Returns a downcased and dash separated version of the string
|
495
494
|
#
|
496
495
|
# @return [Hanami::Utils::String] the transformed string
|
497
496
|
#
|
@@ -513,7 +512,7 @@ module Hanami
|
|
513
512
|
self.class.new underscore.split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR)
|
514
513
|
end
|
515
514
|
|
516
|
-
#
|
515
|
+
# Returns the string without the Ruby namespace of the class
|
517
516
|
#
|
518
517
|
# @return [Hanami::Utils::String] the transformed string
|
519
518
|
#
|
@@ -532,7 +531,7 @@ module Hanami
|
|
532
531
|
self.class.new split(NAMESPACE_SEPARATOR).last
|
533
532
|
end
|
534
533
|
|
535
|
-
#
|
534
|
+
# Returns the top level namespace name
|
536
535
|
#
|
537
536
|
# @return [Hanami::Utils::String] the transformed string
|
538
537
|
#
|
@@ -573,9 +572,8 @@ module Hanami
|
|
573
572
|
# 'Hanami::Utils'
|
574
573
|
# 'Hanami::App'
|
575
574
|
#
|
576
|
-
# rubocop:disable Metrics/MethodLength
|
577
575
|
def tokenize
|
578
|
-
if match = TOKENIZE_REGEXP.match(@string)
|
576
|
+
if match = TOKENIZE_REGEXP.match(@string)
|
579
577
|
pre = match.pre_match
|
580
578
|
post = match.post_match
|
581
579
|
tokens = match[1].split(TOKENIZE_SEPARATOR)
|
@@ -588,9 +586,8 @@ module Hanami
|
|
588
586
|
|
589
587
|
nil
|
590
588
|
end
|
591
|
-
# rubocop:enable Metrics/MethodLength
|
592
589
|
|
593
|
-
#
|
590
|
+
# Returns a pluralized version of self.
|
594
591
|
#
|
595
592
|
# @return [Hanami::Utils::String] the pluralized string.
|
596
593
|
#
|
@@ -603,7 +600,7 @@ module Hanami
|
|
603
600
|
self.class.new Inflector.pluralize(self)
|
604
601
|
end
|
605
602
|
|
606
|
-
#
|
603
|
+
# Returns a singularized version of self.
|
607
604
|
#
|
608
605
|
# @return [Hanami::Utils::String] the singularized string.
|
609
606
|
#
|
@@ -636,7 +633,7 @@ module Hanami
|
|
636
633
|
@string
|
637
634
|
end
|
638
635
|
|
639
|
-
|
636
|
+
alias_method :to_str, :to_s
|
640
637
|
|
641
638
|
# Equality
|
642
639
|
#
|
@@ -648,9 +645,9 @@ module Hanami
|
|
648
645
|
to_s == other
|
649
646
|
end
|
650
647
|
|
651
|
-
|
648
|
+
alias_method :eql?, :==
|
652
649
|
|
653
|
-
#
|
650
|
+
# Splits the string with the given pattern
|
654
651
|
#
|
655
652
|
# @return [Array<::String>]
|
656
653
|
#
|
@@ -662,7 +659,7 @@ module Hanami
|
|
662
659
|
@string.split(pattern, limit)
|
663
660
|
end
|
664
661
|
|
665
|
-
#
|
662
|
+
# Replaces the given pattern with the given replacement
|
666
663
|
#
|
667
664
|
# @return [::String]
|
668
665
|
#
|
@@ -678,7 +675,7 @@ module Hanami
|
|
678
675
|
end
|
679
676
|
end
|
680
677
|
|
681
|
-
#
|
678
|
+
# Iterates through the string, matching the pattern.
|
682
679
|
# Either return all those patterns, or pass them to the block.
|
683
680
|
#
|
684
681
|
# @return [Array<::String>]
|
@@ -691,7 +688,7 @@ module Hanami
|
|
691
688
|
@string.scan(pattern, &blk)
|
692
689
|
end
|
693
690
|
|
694
|
-
#
|
691
|
+
# Replaces the rightmost match of `pattern` with `replacement`
|
695
692
|
#
|
696
693
|
# If the pattern cannot be matched, it returns the original string.
|
697
694
|
#
|
@@ -717,7 +714,7 @@ module Hanami
|
|
717
714
|
# puts result
|
718
715
|
# # => #<Hanami::Utils::String:0x007fdb41232ed0 @string="authors/books#index">
|
719
716
|
def rsub(pattern, replacement)
|
720
|
-
if i = rindex(pattern)
|
717
|
+
if i = rindex(pattern)
|
721
718
|
s = @string.dup
|
722
719
|
s[i] = replacement
|
723
720
|
self.class.new s
|
@@ -726,21 +723,23 @@ module Hanami
|
|
726
723
|
end
|
727
724
|
end
|
728
725
|
|
729
|
-
#
|
726
|
+
# Overrides Ruby's method_missing in order to provide ::String interface
|
730
727
|
#
|
731
728
|
# @api private
|
732
729
|
# @since 0.3.0
|
733
730
|
#
|
734
731
|
# @raise [NoMethodError] If doesn't respond to the given method
|
735
732
|
def method_missing(method_name, *args, &blk)
|
736
|
-
|
733
|
+
unless respond_to?(method_name)
|
734
|
+
raise NoMethodError.new(%(undefined method `#{method_name}' for "#{@string}":#{self.class}))
|
735
|
+
end
|
737
736
|
|
738
737
|
s = @string.__send__(method_name, *args, &blk)
|
739
738
|
s = self.class.new(s) if s.is_a?(::String)
|
740
739
|
s
|
741
740
|
end
|
742
741
|
|
743
|
-
#
|
742
|
+
# Overrides Ruby's respond_to_missing? in order to support ::String interface
|
744
743
|
#
|
745
744
|
# @api private
|
746
745
|
# @since 0.3.0
|