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.
- checksums.yaml +5 -5
- data/.travis.yml +3 -3
- data/CHANGELOG.md +60 -24
- data/Gemfile +3 -0
- data/README.md +21 -58
- data/Rakefile +0 -14
- data/lib/mini_sanity.rb +4 -6
- data/lib/mini_sanity/assert.rb +163 -0
- data/lib/mini_sanity/change.rb +107 -0
- data/lib/mini_sanity/error.rb +18 -8
- data/lib/mini_sanity/match.rb +57 -0
- data/lib/mini_sanity/results.rb +70 -0
- data/lib/mini_sanity/version.rb +1 -1
- data/mini_sanity.gemspec +11 -12
- metadata +14 -73
- data/lib/mini_sanity/array.rb +0 -63
- data/lib/mini_sanity/enumerable.rb +0 -52
- data/lib/mini_sanity/object.rb +0 -216
- data/lib/mini_sanity/pathname.rb +0 -135
- data/lib/mini_sanity/string.rb +0 -143
- data/lib/mini_sanity/util.rb +0 -3
- data/lib/mini_sanity/util/enumerable.rb +0 -31
- data/lib/mini_sanity/util/regexp.rb +0 -27
- data/lib/mini_sanity/util/string.rb +0 -77
data/lib/mini_sanity/util.rb
DELETED
@@ -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
|