ellipsized 0.2.1 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbad7a5c29c4eef2b37c75324de9750fa344640d0997e4bf3269c39031d48031
4
- data.tar.gz: a0eee5de3a754f5abf342fa949b45ac378f416f0b208be741457bc61a1e2969e
3
+ metadata.gz: 8e4185354881717b62aaf4170c011968eb50b5a7dd4fcc0947cb0a7192677c69
4
+ data.tar.gz: d36e583d3c9c31f57b00766e6441be34bcb93a4073fa3f38bba82d4170d25f94
5
5
  SHA512:
6
- metadata.gz: 203bcbacf09f2e295b875003e117215562d6d6f7fec75de1d47055f9f975874a53c46c500d4cfe9539d9d03bde2458c60b64ebac71e7348cfe1658f89d67b89e
7
- data.tar.gz: bc98ff7b40d005c9779a309635eeff534d9c203974dbc19201bfef1e116be704f5d2f2fadb0db7bc0a7ebf9103ca4c3e0f670a498358a97450988d3fba275f87
6
+ metadata.gz: adf0564a1ba5e3cbd27e4627a61e82e32f1d6f065c4b6a51de22c37b8942b1da8b0d0e8106334e327f49b4d1f39073038cc899f798b7e24deb381ad984ffc0f6
7
+ data.tar.gz: 2832a6bfa7f841f49fd83e4ca7a2150809cf7ffa800a914214f6dbebed5da9411e11433a955832f510039a4aaf1e85fb575f549a82c562e7634f658db21378ca
data/.rubocop.yml CHANGED
@@ -19,10 +19,12 @@ Layout/EndOfLine:
19
19
  Style/EvalWithLocation:
20
20
  Enabled: false
21
21
  Metrics/AbcSize:
22
- Max: 40
22
+ Max: 50
23
+ Metrics/ClassLength:
24
+ Max: 112
23
25
  Metrics/CyclomaticComplexity:
24
- Max: 14
26
+ Max: 22
25
27
  Metrics/PerceivedComplexity:
26
- Max: 12
28
+ Max: 18
27
29
  Metrics/MethodLength:
28
- Max: 30
30
+ Max: 38
data/ellipsized.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
10
10
  s.required_ruby_version = '>=3.2'
11
11
  s.name = 'ellipsized'
12
- s.version = '0.2.1'
12
+ s.version = '0.3.0'
13
13
  s.license = 'MIT'
14
14
  s.summary = 'A simple Ruby gem that adds a .ellipsized() method to String'
15
15
  s.description =
data/lib/ellipsized.rb CHANGED
@@ -14,10 +14,26 @@ class String
14
14
  # maximum length. The original string is returned if it is already shorter than
15
15
  # or equal to the maximum length.
16
16
  #
17
- # @param [Integer] max The maximum length of the resulting string
18
- # @param [String] gap The string to use as a gap (default: '...')
19
- # @param [Symbol] align The alignment can be :left, :center, or :right
20
- # (default: :center)
17
+ # @param [Array] args The mix of possible arguments passed to the method:
18
+ # [Integer] max The maximum length of the resulting string (default: 64)
19
+ # [String] gap The string to use as a gap (default: '...')
20
+ # [Symbol] align The alignment can be :left, :center, or :right
21
+ # (default: :center)
22
+ # Since the three arguments have different types, we can distinguish them by
23
+ # type. Based on that, the following permutations can be defined, 3! = 6:
24
+ # s.ellipsized(10, '...', :right)
25
+ # s.ellipsized(10, :right, '...')
26
+ # s.ellipsized('...', 10, :right)
27
+ # s.ellipsized('...', :right, 10)
28
+ # s.ellipsized(:right, '...', 10)
29
+ # s.ellipsized(:right, 10, '...')
30
+ # Keep in mind, any argument may be omitted:
31
+ # s.ellipsized(10, '...', :right)
32
+ # s.ellipsized(10, :right)
33
+ # s.ellipsized('...')
34
+ # s.ellipsized(10)
35
+ # s.ellipsized(:right)
36
+ # All of the above are valid use cases.
21
37
  # @return [String] The truncated string with gap in the middle if necessary
22
38
  #
23
39
  # @example Basic usage with default parameters
@@ -41,16 +57,36 @@ class String
41
57
  # "Short".ellipsized # => "Short"
42
58
  # "xyz".ellipsized(0) # => ""
43
59
  # "xyz".ellipsized(2, '...') # => "xy"
44
- def ellipsized(max = 64, gap = '...', align = :center)
45
- raise "Max length must be an Integer, while #{max.class.name} provided" unless max.is_a?(Integer)
46
- raise "Max length (#{max}) is negative" if max.negative?
47
- raise "The gap doesn't implement to_s()" unless gap.respond_to?(:to_s)
48
- raise "Unsupported align: #{align}" unless %i[left center right].include?(align)
60
+ def ellipsized(*args)
61
+ raise "Unsupported number of arguments: #{args.length}" if args.length > 3
62
+
63
+ max = gap = align = nil
64
+ args.each do |arg|
65
+ raise "Unsupported argument type: #{arg}" unless [
66
+ Integer,
67
+ String,
68
+ Symbol
69
+ ].include?(arg.class)
70
+
71
+ case arg
72
+ when Integer
73
+ max = arg
74
+ raise "Max length (#{max}) is negative" if max.negative?
75
+ return '' if max.zero?
76
+ when String
77
+ gap = arg
78
+ when Symbol
79
+ align = arg
80
+ raise "Unsupported align: #{align}" unless
81
+ %i[left center right].include?(align) # rubocop:disable Performance/CollectionLiteralInLoop
82
+ end
83
+ end
84
+ max ||= 64
85
+ gap ||= '...'
86
+ align ||= :center
87
+
49
88
  return '' if empty?
50
89
  return self if length <= max
51
- return '' if max.zero?
52
-
53
- gap = gap.to_s
54
90
  return self[0..max - 1] if gap.length >= max
55
91
 
56
92
  case align
@@ -120,4 +120,18 @@ class TestEllipsized < Minitest::Test
120
120
  'This story is very long to fit into a small window'.ellipsized(20, '.. skip ..', :right)
121
121
  )
122
122
  end
123
+
124
+ def test_arguments_permutations
125
+ assert_equal('app...na', 'apple and banana'.ellipsized(8))
126
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, '...'))
127
+ assert_equal('app...na', 'apple and banana'.ellipsized('...', 8))
128
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, :center))
129
+ assert_equal('app...na', 'apple and banana'.ellipsized(:center, 8))
130
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, '...', :center))
131
+ assert_equal('app...na', 'apple and banana'.ellipsized(8, :center, '...'))
132
+ assert_equal('app...na', 'apple and banana'.ellipsized('...', 8, :center))
133
+ assert_equal('app...na', 'apple and banana'.ellipsized('...', :center, 8))
134
+ assert_equal('app...na', 'apple and banana'.ellipsized(:center, 8, '...'))
135
+ assert_equal('app...na', 'apple and banana'.ellipsized(:center, '...', 8))
136
+ end
123
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ellipsized
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko