mini_sanity 1.1.0 → 2.0.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.
@@ -1,3 +0,0 @@
1
- require_relative "util/enumerable"
2
- require_relative "util/regexp"
3
- require_relative "util/string"
@@ -1,31 +0,0 @@
1
- module Enumerable
2
-
3
- # Like {https://ruby-doc.org/core/Enumerable.html#method-i-first
4
- # +Enumerable#first+}, but raises an exception if the Enumerable does
5
- # not contain the requested number of elements.
6
- #
7
- # @example
8
- # [7, 8, 9].first # == 7
9
- # [7, 8, 9].first(1) # == [7]
10
- # [7, 8, 9].first(2) # == [7, 8]
11
- # [7, 8, 9].first(4) # raises exception
12
- # [].first # raises exception
13
- #
14
- # @param n [Integer]
15
- # requested number of elements
16
- # @return [Object, Array]
17
- # @raise [MiniSanity::Error]
18
- # if the Enumerable does not contain the requested number of elements
19
- def first!(n = nil)
20
- result = n.nil? ? self.first : self.first(n)
21
-
22
- if (result.nil? && !self.any?{ true }) || (!n.nil? && result.length < n)
23
- raise MiniSanity::Error.new(nil,
24
- "Enumerable having at least #{n || 1} elements",
25
- self.inspect)
26
- end
27
-
28
- result
29
- end
30
-
31
- end
@@ -1,27 +0,0 @@
1
- class Regexp
2
-
3
- # Like {https://ruby-doc.org/core/Regexp.html#method-i-match
4
- # +Regexp#match+}, but raises an exception if the match fails.
5
- #
6
- # @example
7
- # /^([^@]+)@(.+)$/.match!("user@example.com") # === MatchData
8
- # /^([^@]+)@(.+)$/.match!("@user") # raises exception
9
- #
10
- # @param str [String]
11
- # string to search
12
- # @param pos [Integer]
13
- # position in +str+ to begin the search
14
- # @return [MatchData]
15
- # @raise [MiniSanity::Error]
16
- # if the Regexp does not match +str+
17
- def match!(str, pos = 0)
18
- result = self.match(str, pos)
19
- if result.nil?
20
- raise MiniSanity::Error.new(nil,
21
- "String matching #{self.inspect}#{" from position #{pos}" if pos != 0}",
22
- str.inspect)
23
- end
24
- result
25
- end
26
-
27
- end
@@ -1,77 +0,0 @@
1
- class String
2
-
3
- # Like {https://ruby-doc.org/core/String.html#method-i-match
4
- # +String#match+}, but raises an exception if the match fails.
5
- #
6
- # @example
7
- # "user@example.com".match!(/^([^@]+)@(.+)$/) # === MatchData
8
- # "@user".match!(/^([^@]+)@(.+)$/) # raises exception
9
- #
10
- # @param pattern [Regexp]
11
- # pattern to search for
12
- # @param pos [Integer]
13
- # position in the String to begin the search
14
- # @return [MatchData]
15
- # @raise [MiniSanity::Error]
16
- # if +pattern+ does not match the String
17
- def match!(pattern, pos = 0)
18
- result = self.match(pattern, pos)
19
- if result.nil?
20
- raise MiniSanity::Error.new(nil,
21
- "String matching #{pattern.inspect}#{" from position #{pos}" if pos != 0}",
22
- self.inspect)
23
- end
24
- result
25
- end
26
-
27
- # Like {https://ruby-doc.org/core/String.html#method-i-sub-21
28
- # +String#sub!+}, but raises an exception if no substitution was
29
- # performed.
30
- #
31
- # @example
32
- # "author: YOUR_NAME".change!(/\bYOUR_NAME\b/, "Me") # == "author: Me"
33
- # "author: TODO".change!(/\bYOUR_NAME\b/, "Me") # raises exception
34
- #
35
- # @param pattern [Regexp, String]
36
- # pattern to search for
37
- # @param replacement [String, Hash, &block]
38
- # substitution value (see +String#sub!+ documentation for full details)
39
- # @return [String]
40
- # @raise [MiniSanity::Error]
41
- # if no substitution was performed
42
- def change!(pattern, replacement = nil, &replacement_block)
43
- result = if replacement
44
- self.sub!(pattern, replacement)
45
- else
46
- self.sub!(pattern, &replacement_block)
47
- end
48
-
49
- if !result
50
- raise MiniSanity::Error.new(nil,
51
- "String matching #{pattern.inspect}",
52
- self.inspect)
53
- end
54
-
55
- result
56
- end
57
-
58
- # Like {https://ruby-doc.org/core/String.html#method-i-sub
59
- # +String#sub+}, but raises an exception if no substitution was
60
- # performed.
61
- #
62
- # @example
63
- # "author: YOUR_NAME".change(/\bYOUR_NAME\b/, "Me") # == "author: Me"
64
- # "author: TODO".change(/\bYOUR_NAME\b/, "Me") # raises exception
65
- #
66
- # @param pattern [Regexp, String]
67
- # pattern to search for
68
- # @param replacement [String, Hash, &block]
69
- # substitution value (see +String#sub+ documentation for full details)
70
- # @return [String]
71
- # @raise [MiniSanity::Error]
72
- # if no substitution was performed
73
- def change(pattern, replacement = nil, &replacement_block)
74
- self.dup.change!(pattern, replacement, &replacement_block)
75
- end
76
-
77
- end