hanami-utils 1.3.8 → 2.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -43,7 +43,7 @@ module Hanami
43
43
  end
44
44
  # rubocop:enable Style/ClassVars
45
45
 
46
- # Parses the given JSON paylod
46
+ # Parse the given JSON paylod
47
47
  #
48
48
  # @param payload [String] a JSON payload
49
49
  #
@@ -9,7 +9,7 @@ require "hanami/utils"
9
9
  require "hanami/utils/string"
10
10
 
11
11
  unless defined?(Boolean)
12
- # Defines top level constant Boolean, so it can be easily used by other libraries
12
+ # Define top level constant Boolean, so it can be easily used by other libraries
13
13
  # in coercions DSLs
14
14
  #
15
15
  # @since 0.3.0
@@ -327,7 +327,7 @@ module Hanami
327
327
  # # big complex represented as a string
328
328
  # input = Complex(2, 3)
329
329
  # Hanami::Utils::Kernel.Integer(input) # => TypeError
330
- def self.Integer(arg)
330
+ def self.Integer(arg) # rubocop:disable Metrics/MethodLength
331
331
  super(arg)
332
332
  rescue ArgumentError, TypeError, NoMethodError
333
333
  begin
@@ -418,6 +418,7 @@ module Hanami
418
418
  # input = BasicObject.new
419
419
  # Hanami::Utils::Kernel.BigDecimal(input) # => TypeError
420
420
  #
421
+ # rubocop:disable Metrics/MethodLength
421
422
  def self.BigDecimal(arg, precision = ::Float::DIG)
422
423
  case arg
423
424
  when NilClass # This is only needed by Ruby 2.6
@@ -434,6 +435,7 @@ module Hanami
434
435
  rescue NoMethodError
435
436
  raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal"
436
437
  end
438
+ # rubocop:enable Metrics/MethodLength
437
439
 
438
440
  # Coerces the argument to be a Float.
439
441
  #
@@ -547,7 +549,7 @@ module Hanami
547
549
  # # big complex represented as a string
548
550
  # input = Complex(2, 3)
549
551
  # Hanami::Utils::Kernel.Float(input) # => TypeError
550
- def self.Float(arg)
552
+ def self.Float(arg) # rubocop:disable Metrics/MethodLength
551
553
  super(arg)
552
554
  rescue ArgumentError, TypeError
553
555
  begin
@@ -736,8 +738,7 @@ module Hanami
736
738
  # require 'hanami/utils/kernel'
737
739
  #
738
740
  # Hanami::Utils::Kernel.DateTime(3483943)
739
- # # => Time.at(3483943).to_datetime
740
- # # #<DateTime: 1970-02-10T08:45:43+01:00 ((2440628j,27943s,0n),+3600s,2299161j)>
741
+ # # => Time.at(3483943).to_datetime #<DateTime: 1970-02-10T08:45:43+01:00 ((2440628j,27943s,0n),+3600s,2299161j)>
741
742
  #
742
743
  # Hanami::Utils::Kernel.DateTime(DateTime.now)
743
744
  # # => #<DateTime: 2014-04-18T09:33:49+02:00 ((2456766j,27229s,690849000n),+7200s,2299161j)>
@@ -897,7 +898,7 @@ module Hanami
897
898
  # # Missing #respond_to?
898
899
  # input = BasicObject.new
899
900
  # Hanami::Utils::Kernel.Boolean(input) # => TypeError
900
- def self.Boolean(arg)
901
+ def self.Boolean(arg) # rubocop:disable Metrics/MethodLength
901
902
  case arg
902
903
  when Numeric
903
904
  arg.to_i == BOOLEAN_TRUE_INTEGER
@@ -1022,7 +1023,7 @@ module Hanami
1022
1023
  raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol"
1023
1024
  end
1024
1025
 
1025
- # Checks if the given argument is a string representation of a number
1026
+ # Check if the given argument is a string representation of a number
1026
1027
  #
1027
1028
  # @param arg [Object] the input
1028
1029
  #
@@ -1043,11 +1044,11 @@ module Hanami
1043
1044
  # @since 0.4.3
1044
1045
  # @api private
1045
1046
  def self.inspect_type_error(arg)
1046
- (arg.respond_to?(:inspect) ? arg.inspect : arg.to_s) + " "
1047
- rescue NoMethodError
1047
+ (arg.respond_to?(:inspect) ? arg.inspect : arg.to_s) << " "
1048
+ rescue NoMethodError => _
1048
1049
  # missing the #respond_to? method, fall back to returning the class' name
1049
1050
  begin
1050
- arg.class.name + " instance "
1051
+ arg.class.name << " instance "
1051
1052
  rescue NoMethodError
1052
1053
  # missing the #class method, can't fall back to anything better than nothing
1053
1054
  # Callers will have to guess from their code
@@ -1056,7 +1057,7 @@ module Hanami
1056
1057
  end
1057
1058
 
1058
1059
  class << self
1059
- private :inspect_type_error
1060
+ private :inspect_type_error # rubocop:disable Style/AccessModifierDeclarations
1060
1061
  end
1061
1062
  end
1062
1063
  end
@@ -50,7 +50,7 @@ module Hanami
50
50
  @paths = original.instance_variable_get(:@paths).dup
51
51
  end
52
52
 
53
- # Iterates through the collection and yields the given block.
53
+ # Iterates thru the collection and yields the given block.
54
54
  # It skips duplications and raises an error in case one of the paths
55
55
  # doesn't exist.
56
56
  #
@@ -117,7 +117,7 @@ module Hanami
117
117
  self
118
118
  end
119
119
 
120
- alias_method :<<, :push
120
+ alias << push
121
121
 
122
122
  # It freezes the object by preventing further modifications.
123
123
  #
@@ -158,7 +158,7 @@ module Hanami
158
158
 
159
159
  private
160
160
 
161
- # Allows subclasses to define their own policy to discover the realpath
161
+ # Allow subclasses to define their own policy to discover the realpath
162
162
  # of the given path.
163
163
  #
164
164
  # @since 0.2.0
@@ -12,7 +12,7 @@ module Hanami
12
12
  # @api private
13
13
  HASH_SEPARATOR = ","
14
14
 
15
- # Serializes input into a query string
15
+ # Serialize input into a query string
16
16
  #
17
17
  # @param input [Object] the input
18
18
  #
@@ -18,22 +18,22 @@ module Hanami
18
18
  end
19
19
  end
20
20
 
21
- # Escapes codes for terminals to output strings in colors
21
+ # Escape 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: 30,
27
- red: 31,
28
- green: 32,
29
- yellow: 33,
30
- blue: 34,
26
+ black: 30,
27
+ red: 31,
28
+ green: 32,
29
+ yellow: 33,
30
+ blue: 34,
31
31
  magenta: 35,
32
- cyan: 36,
33
- gray: 37,
32
+ cyan: 36,
33
+ gray: 37,
34
34
  ].freeze
35
35
 
36
- # Colorizes output
36
+ # Colorize 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
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "hanami/utils/inflector"
4
3
  require "transproc"
5
4
  require "concurrent/map"
6
5
 
@@ -9,7 +8,7 @@ module Hanami
9
8
  # String on steroids
10
9
  #
11
10
  # @since 0.1.0
12
- class String
11
+ class String # rubocop:disable Metrics/ClassLength
13
12
  # Empty string for #classify
14
13
  #
15
14
  # @since 0.6.0
@@ -74,17 +73,16 @@ module Hanami
74
73
  #
75
74
  # @since 0.3.4
76
75
  # @api private
77
- CLASSIFY_WORD_SEPARATOR = /#{CLASSIFY_SEPARATOR}|#{NAMESPACE_SEPARATOR}|#{UNDERSCORE_SEPARATOR}|#{DASHERIZE_SEPARATOR}/.freeze # rubocop:disable Layout/LineLength
76
+ CLASSIFY_WORD_SEPARATOR = /#{CLASSIFY_SEPARATOR}|#{NAMESPACE_SEPARATOR}|#{UNDERSCORE_SEPARATOR}|#{DASHERIZE_SEPARATOR}/.freeze
78
77
 
79
78
  @__transformations__ = Concurrent::Map.new
80
79
 
81
80
  extend Transproc::Registry
82
81
  extend Transproc::Composer
83
82
 
84
- # Applies the given transformation(s) to `input`
83
+ # Apply the given transformation(s) to `input`
85
84
  #
86
- # It performs a pipeline of transformations, by applying the given
87
- # functions from `Hanami::Utils::String` and `::String`.
85
+ # It performs a pipeline of transformations, by applying the given functions from `Hanami::Utils::String` and `::String`.
88
86
  # The transformations are applied in the given order.
89
87
  #
90
88
  # It doesn't mutate the input, unless you use destructive methods from `::String`
@@ -132,6 +130,8 @@ module Hanami
132
130
  # Hanami::Utils::String.transform("Cherry", -> { "blossom" }))
133
131
  # # => ArgumentError: wrong number of arguments (given 1, expected 0)
134
132
  #
133
+ # rubocop:disable Metrics/AbcSize
134
+ # rubocop:disable Metrics/MethodLength
135
135
  def self.transform(input, *transformations)
136
136
  fn = @__transformations__.fetch_or_store(transformations.hash) do
137
137
  compose do |fns|
@@ -143,7 +143,7 @@ module Hanami
143
143
  elsif input.respond_to?(transformation)
144
144
  t(:bind, input, ->(i) { i.public_send(transformation, *args) })
145
145
  else
146
- raise NoMethodError.new(%(undefined method `#{transformation.inspect}' for #{input.inspect}:#{input.class})) # rubocop:disable Layout/LineLength
146
+ raise NoMethodError.new(%(undefined method `#{transformation.inspect}' for #{input.inspect}:#{input.class}))
147
147
  end
148
148
  end
149
149
  end
@@ -151,6 +151,8 @@ module Hanami
151
151
 
152
152
  fn.call(input)
153
153
  end
154
+ # rubocop:enable Metrics/MethodLength
155
+ # rubocop:enable Metrics/AbcSize
154
156
 
155
157
  # Extracted from `transproc` source code
156
158
  #
@@ -163,7 +165,7 @@ module Hanami
163
165
  binding.instance_exec(value, &fun)
164
166
  end
165
167
 
166
- # Returns a titleized version of the string
168
+ # Return a titleized version of the string
167
169
  #
168
170
  # @param input [::String] the input
169
171
  #
@@ -180,7 +182,7 @@ module Hanami
180
182
  underscore(string).split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
181
183
  end
182
184
 
183
- # Returns a capitalized version of the string
185
+ # Return a capitalized version of the string
184
186
  #
185
187
  # @param input [::String] the input
186
188
  #
@@ -207,7 +209,7 @@ module Hanami
207
209
  tail.unshift(head.capitalize).join(CAPITALIZE_SEPARATOR)
208
210
  end
209
211
 
210
- # Returns a CamelCase version of the string
212
+ # Return a CamelCase version of the string
211
213
  #
212
214
  # @param input [::String] the input
213
215
  #
@@ -231,7 +233,7 @@ module Hanami
231
233
  words.zip(delimiters).join
232
234
  end
233
235
 
234
- # Returns a downcased and underscore separated version of the string
236
+ # Return a downcased and underscore separated version of the string
235
237
  #
236
238
  # Revised version of `ActiveSupport::Inflector.underscore` implementation
237
239
  # @see https://github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
@@ -256,7 +258,7 @@ module Hanami
256
258
  string.downcase
257
259
  end
258
260
 
259
- # Returns a downcased and dash separated version of the string
261
+ # Return a downcased and dash separated version of the string
260
262
  #
261
263
  # @param input [::String] the input
262
264
  #
@@ -277,7 +279,7 @@ module Hanami
277
279
  underscore(string).split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR)
278
280
  end
279
281
 
280
- # Returns the string without the Ruby namespace of the class
282
+ # Return the string without the Ruby namespace of the class
281
283
  #
282
284
  # @param input [::String] the input
283
285
  #
@@ -295,7 +297,7 @@ module Hanami
295
297
  ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).last
296
298
  end
297
299
 
298
- # Returns the top level namespace name
300
+ # Return the top level namespace name
299
301
  #
300
302
  # @param input [::String] the input
301
303
  #
@@ -313,47 +315,7 @@ module Hanami
313
315
  ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).first
314
316
  end
315
317
 
316
- # Returns a pluralized version of self.
317
- #
318
- # @param input [::String] the input
319
- #
320
- # @return [::String] the pluralized string.
321
- #
322
- # @since 1.1.0
323
- #
324
- # @see Hanami::Utils::Inflector
325
- # @deprecated
326
- #
327
- # @example
328
- # require 'hanami/utils/string'
329
- #
330
- # Hanami::Utils::String.pluralize('book') # => 'books'
331
- def self.pluralize(input)
332
- string = ::String.new(input.to_s)
333
- Inflector.pluralize(string)
334
- end
335
-
336
- # Returns a singularized version of self.
337
- #
338
- # @param input [::String] the input
339
- #
340
- # @return [::String] the singularized string.
341
- #
342
- # @since 1.1.0
343
- # @deprecated
344
- #
345
- # @see Hanami::Utils::Inflector
346
- #
347
- # @example
348
- # require 'hanami/utils/string'
349
- #
350
- # Hanami::Utils::String.singularize('books') # => 'book'
351
- def self.singularize(input)
352
- string = ::String.new(input.to_s)
353
- Inflector.singularize(string)
354
- end
355
-
356
- # Replaces the rightmost match of `pattern` with `replacement`
318
+ # Replace the rightmost match of `pattern` with `replacement`
357
319
  #
358
320
  # If the pattern cannot be matched, it returns the original string.
359
321
  #
@@ -374,7 +336,7 @@ module Hanami
374
336
  # # => 'authors/books#index'
375
337
  def self.rsub(input, pattern, replacement)
376
338
  string = ::String.new(input.to_s)
377
- if i = string.rindex(pattern)
339
+ if i = string.rindex(pattern) # rubocop:disable Lint/AssignmentInCondition
378
340
  s = string.dup
379
341
  s[i] = replacement
380
342
  s
@@ -395,7 +357,7 @@ module Hanami
395
357
  @string = string.to_s
396
358
  end
397
359
 
398
- # Returns a titleized version of the string
360
+ # Return a titleized version of the string
399
361
  #
400
362
  # @return [Hanami::Utils::String] the transformed string
401
363
  #
@@ -411,7 +373,7 @@ module Hanami
411
373
  self.class.new underscore.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
412
374
  end
413
375
 
414
- # Returns a capitalized version of the string
376
+ # Return a capitalized version of the string
415
377
  #
416
378
  # @return [Hanami::Utils::String] the transformed string
417
379
  #
@@ -443,7 +405,7 @@ module Hanami
443
405
  )
444
406
  end
445
407
 
446
- # Returns a CamelCase version of the string
408
+ # Return a CamelCase version of the string
447
409
  #
448
410
  # @return [Hanami::Utils::String] the transformed string
449
411
  #
@@ -466,7 +428,7 @@ module Hanami
466
428
  self.class.new words.zip(delimiters).join
467
429
  end
468
430
 
469
- # Returns a downcased and underscore separated version of the string
431
+ # Return a downcased and underscore separated version of the string
470
432
  #
471
433
  # Revised version of `ActiveSupport::Inflector.underscore` implementation
472
434
  # @see https://github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
@@ -490,7 +452,7 @@ module Hanami
490
452
  self.class.new new_string
491
453
  end
492
454
 
493
- # Returns a downcased and dash separated version of the string
455
+ # Return a downcased and dash separated version of the string
494
456
  #
495
457
  # @return [Hanami::Utils::String] the transformed string
496
458
  #
@@ -512,7 +474,7 @@ module Hanami
512
474
  self.class.new underscore.split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR)
513
475
  end
514
476
 
515
- # Returns the string without the Ruby namespace of the class
477
+ # Return the string without the Ruby namespace of the class
516
478
  #
517
479
  # @return [Hanami::Utils::String] the transformed string
518
480
  #
@@ -531,7 +493,7 @@ module Hanami
531
493
  self.class.new split(NAMESPACE_SEPARATOR).last
532
494
  end
533
495
 
534
- # Returns the top level namespace name
496
+ # Return the top level namespace name
535
497
  #
536
498
  # @return [Hanami::Utils::String] the transformed string
537
499
  #
@@ -572,8 +534,9 @@ module Hanami
572
534
  # 'Hanami::Utils'
573
535
  # 'Hanami::App'
574
536
  #
537
+ # rubocop:disable Metrics/MethodLength
575
538
  def tokenize
576
- if match = TOKENIZE_REGEXP.match(@string)
539
+ if match = TOKENIZE_REGEXP.match(@string) # rubocop:disable Lint/AssignmentInCondition
577
540
  pre = match.pre_match
578
541
  post = match.post_match
579
542
  tokens = match[1].split(TOKENIZE_SEPARATOR)
@@ -586,32 +549,7 @@ module Hanami
586
549
 
587
550
  nil
588
551
  end
589
-
590
- # Returns a pluralized version of self.
591
- #
592
- # @return [Hanami::Utils::String] the pluralized string.
593
- #
594
- # @api private
595
- # @since 0.4.1
596
- # @deprecated
597
- #
598
- # @see Hanami::Utils::Inflector
599
- def pluralize
600
- self.class.new Inflector.pluralize(self)
601
- end
602
-
603
- # Returns a singularized version of self.
604
- #
605
- # @return [Hanami::Utils::String] the singularized string.
606
- #
607
- # @api private
608
- # @since 0.4.1
609
- # @deprecated
610
- #
611
- # @see Hanami::Utils::Inflector
612
- def singularize
613
- self.class.new Inflector.singularize(self)
614
- end
552
+ # rubocop:enable Metrics/MethodLength
615
553
 
616
554
  # Returns the hash of the internal string
617
555
  #
@@ -633,7 +571,7 @@ module Hanami
633
571
  @string
634
572
  end
635
573
 
636
- alias_method :to_str, :to_s
574
+ alias to_str to_s
637
575
 
638
576
  # Equality
639
577
  #
@@ -645,9 +583,9 @@ module Hanami
645
583
  to_s == other
646
584
  end
647
585
 
648
- alias_method :eql?, :==
586
+ alias eql? ==
649
587
 
650
- # Splits the string with the given pattern
588
+ # Split the string with the given pattern
651
589
  #
652
590
  # @return [Array<::String>]
653
591
  #
@@ -659,7 +597,7 @@ module Hanami
659
597
  @string.split(pattern, limit)
660
598
  end
661
599
 
662
- # Replaces the given pattern with the given replacement
600
+ # Replace the given pattern with the given replacement
663
601
  #
664
602
  # @return [::String]
665
603
  #
@@ -675,7 +613,7 @@ module Hanami
675
613
  end
676
614
  end
677
615
 
678
- # Iterates through the string, matching the pattern.
616
+ # Iterate through the string, matching the pattern.
679
617
  # Either return all those patterns, or pass them to the block.
680
618
  #
681
619
  # @return [Array<::String>]
@@ -688,7 +626,7 @@ module Hanami
688
626
  @string.scan(pattern, &blk)
689
627
  end
690
628
 
691
- # Replaces the rightmost match of `pattern` with `replacement`
629
+ # Replace the rightmost match of `pattern` with `replacement`
692
630
  #
693
631
  # If the pattern cannot be matched, it returns the original string.
694
632
  #
@@ -714,7 +652,7 @@ module Hanami
714
652
  # puts result
715
653
  # # => #<Hanami::Utils::String:0x007fdb41232ed0 @string="authors/books#index">
716
654
  def rsub(pattern, replacement)
717
- if i = rindex(pattern)
655
+ if i = rindex(pattern) # rubocop:disable Lint/AssignmentInCondition
718
656
  s = @string.dup
719
657
  s[i] = replacement
720
658
  self.class.new s
@@ -723,23 +661,21 @@ module Hanami
723
661
  end
724
662
  end
725
663
 
726
- # Overrides Ruby's method_missing in order to provide ::String interface
664
+ # Override Ruby's method_missing in order to provide ::String interface
727
665
  #
728
666
  # @api private
729
667
  # @since 0.3.0
730
668
  #
731
669
  # @raise [NoMethodError] If doesn't respond to the given method
732
670
  def method_missing(method_name, *args, &blk)
733
- unless respond_to?(method_name)
734
- raise NoMethodError.new(%(undefined method `#{method_name}' for "#{@string}":#{self.class}))
735
- end
671
+ raise NoMethodError.new(%(undefined method `#{method_name}' for "#{@string}":#{self.class})) unless respond_to?(method_name)
736
672
 
737
673
  s = @string.__send__(method_name, *args, &blk)
738
674
  s = self.class.new(s) if s.is_a?(::String)
739
675
  s
740
676
  end
741
677
 
742
- # Overrides Ruby's respond_to_missing? in order to support ::String interface
678
+ # Override Ruby's respond_to_missing? in order to support ::String interface
743
679
  #
744
680
  # @api private
745
681
  # @since 0.3.0