Class: String
- Defined in:
- lib/mini_sanity/string.rb,
lib/mini_sanity/util/string.rb
Instance Method Summary collapse
-
#assert_empty!(name = nil) ⇒ self
Checks that the String is empty, and returns the String unmodified.
-
#assert_length!(length, name = nil) ⇒ self
Checks that the String matches a given length, and returns the String unmodified.
-
#assert_match!(regexp, name = nil) ⇒ self
Checks that the String matches a given regular expression, and returns the String unmodified.
-
#change(pattern, replacement = nil, &replacement_block) ⇒ String
Like String#sub, but raises an exception if no substitution was performed.
-
#change!(pattern, replacement = nil, &replacement_block) ⇒ String
Like String#sub!, but raises an exception if no substitution was performed.
-
#match!(pattern, pos = 0) ⇒ MatchData
Like String#match, but raises an exception if the match fails.
-
#refute_empty!(name = nil) ⇒ self
Checks that the String is not empty, and returns the String unmodified.
-
#refute_length!(length, name = nil) ⇒ self
Checks that the String does not match a given length, and returns the String unmodified.
-
#refute_match!(regexp, name = nil) ⇒ self
Checks that the String does not match a given regular expression, and returns the String unmodified.
Instance Method Details
#assert_empty!(name = nil) ⇒ self
Checks that the String is empty, and returns the String unmodified. If the String fails this check, an exception is raised.
15 16 17 18 19 20 21 22 |
# File 'lib/mini_sanity/string.rb', line 15 def assert_empty!(name = nil) if !self.empty? raise MiniSanity::Error.new(name, "empty #{self.class}", self.inspect) end self end |
#assert_length!(length, name = nil) ⇒ self
Checks that the String matches a given length, and returns the String unmodified. If the String fails this check, an exception is raised.
61 62 63 64 65 66 67 68 |
# File 'lib/mini_sanity/string.rb', line 61 def assert_length!(length, name = nil) if !(length === self.length) raise MiniSanity::Error.new(name, "#{self.class} having #{length} characters", self.inspect) end self end |
#assert_match!(regexp, name = nil) ⇒ self
Checks that the String matches a given regular expression, and returns the String unmodified. If the String fails this check, an exception is raised.
110 111 112 113 114 115 116 117 |
# File 'lib/mini_sanity/string.rb', line 110 def assert_match!(regexp, name = nil) if regexp !~ self raise MiniSanity::Error.new(name, "#{self.class} matching #{regexp.inspect}", self.inspect) end self end |
#change(pattern, replacement = nil, &replacement_block) ⇒ String
Like String#sub, but raises an exception if no substitution was performed.
73 74 75 |
# File 'lib/mini_sanity/util/string.rb', line 73 def change(pattern, replacement = nil, &replacement_block) self.dup.change!(pattern, replacement, &replacement_block) end |
#change!(pattern, replacement = nil, &replacement_block) ⇒ String
Like String#sub!, but raises an exception if no substitution was performed.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mini_sanity/util/string.rb', line 42 def change!(pattern, replacement = nil, &replacement_block) result = if replacement self.sub!(pattern, replacement) else self.sub!(pattern, &replacement_block) end if !result raise MiniSanity::Error.new(nil, "String matching #{pattern.inspect}", self.inspect) end result end |
#match!(pattern, pos = 0) ⇒ MatchData
Like String#match, but raises an exception if the match fails.
17 18 19 20 21 22 23 24 25 |
# File 'lib/mini_sanity/util/string.rb', line 17 def match!(pattern, pos = 0) result = self.match(pattern, pos) if result.nil? raise MiniSanity::Error.new(nil, "String matching #{pattern.inspect}#{" from position #{pos}" if pos != 0}", self.inspect) end result end |
#refute_empty!(name = nil) ⇒ self
Checks that the String is not empty, and returns the String unmodified. If the String fails this check, an exception is raised.
36 37 38 39 40 41 42 43 |
# File 'lib/mini_sanity/string.rb', line 36 def refute_empty!(name = nil) if self.empty? raise MiniSanity::Error.new(name, "non-empty #{self.class}", self.inspect) end self end |
#refute_length!(length, name = nil) ⇒ self
Checks that the String does not match a given length, and returns the String unmodified. If the String fails this check, an exception is raised.
86 87 88 89 90 91 92 93 |
# File 'lib/mini_sanity/string.rb', line 86 def refute_length!(length, name = nil) if length === self.length raise MiniSanity::Error.new(name, "#{self.class} not having #{length} characters", self.inspect) end self end |
#refute_match!(regexp, name = nil) ⇒ self
Checks that the String does not match a given regular expression, and returns the String unmodified. If the String fails this check, an exception is raised.
134 135 136 137 138 139 140 141 |
# File 'lib/mini_sanity/string.rb', line 134 def refute_match!(regexp, name = nil) if regexp =~ self raise MiniSanity::Error.new(name, "#{self.class} not matching #{regexp.inspect}", self.inspect) end self end |